C Language network Programming-standard step (improved version) _c language

Source: Internet
Author: User
Tags htons

Server.c

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/errno.h>

#define PORT 4444
#define BACKLOG 5
#define MAX_FD 256

void setnonblocking (int);

int main (int argc, char *argv[]) {
int sock_fd, NEW_FD, New_fd2, EPFD, Nfds;
struct sockaddr_in server_addr, client_addr;
int sin_size;
int nbytes;
int on = 1;
Char buffer[1024];
struct epoll_event ev, EVENTS[20];

EPFD = Epoll_create (MAX_FD);

if ((SOCK_FD = socket (af_inet, sock_stream, 0)) = = 1) {
Perror ("socket");
Exit (1);
}

SetSockOpt (SOCK_FD, Sol_socket, so_reuseaddr, &on, sizeof (on));
Setnonblocking (SOCK_FD);

EV.DATA.FD = SOCK_FD;
ev.events = Epollin | Epollet;
Epoll_ctl (EPFD, Epoll_ctl_add, SOCK_FD, &ev);

memset (&server_addr, 0, sizeof (struct sockaddr_in));
server_addr.sin_family = af_inet;
SERVER_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
Server_addr.sin_port = htons (port);

if (bind (SOCK_FD, struct sockaddr *) (&AMP;SERVER_ADDR), sizeof (struct sockaddr)) = = 1) {
Perror ("bind");
Exit (1);
}

if (Listen (SOCK_FD, BACKLOG) = = 1) {
Perror ("Listen");
Exit (1);
}

printf ("Server start ... \ n");

sin_size = sizeof (struct sockaddr_in);

int i;

while (1) {
Nfds = epoll_wait (EPFD, events, 20, 500);
for (i = 0; i < Nfds; i++) {
if (events[i].data.fd = = sock_fd) {
if ((new_fd = Accept (sock_fd, (struct sockaddr *) (&AMP;CLIENT_ADDR), &sin_size)) = = = 1) {
Perror ("accept");
Exit (1);
}
printf ("Server Get connection from%s\n", Inet_ntoa (CLIENT_ADDR.SIN_ADDR));
Setnonblocking (NEW_FD);
EV.DATA.FD = NEW_FD;
ev.events = Epollin | Epollet;
Epoll_ctl (EPFD, Epoll_ctl_add, NEW_FD, &ev);

else if (events[i].events & Epollin) {
if ((new_fd2 = EVENTS[I].DATA.FD) < 0) continue;
if ((nbytes = read (new_fd2, buffer, 1024)) = = 1) {
Perror ("read");
Exit (1);
else if (nbytes = 0) {
Close (NEW_FD2);
EVENTS[I].DATA.FD =-1;
}
printf ("Server read from%s\n", Inet_ntoa (CLIENT_ADDR.SIN_ADDR));
EV.DATA.FD = NEW_FD2;
ev.events = Epollout | Epollet;
Epoll_ctl (EPFD, Epoll_ctl_mod, NEW_FD2, &ev);

else if (events[i].events & epollout) {
NEW_FD2 = EVENTS[I].DATA.FD;
Write (New_fd2, "Server Received", 16);
EV.DATA.FD = NEW_FD2;
ev.events = Epollin | Epollet;
Epoll_ctl (EPFD, Epoll_ctl_mod, NEW_FD2, &ev);
}
}
}

return 0;
}

void setnonblocking (int fd) {
int opts;
if ((opts = Fcntl (FD, F_GETFL)) < 0) {
Perror ("Fcntl (FD, F_GETFL");
Exit (1);
}

opts = opts | O_nonblock;
if (Fcntl (FD, F_SETFL, opts) < 0) {
Perror ("Fcntl (FD, F_SETFL, opts");
Exit (1);
}
}

Client.c

Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 4444

int main (int argc, char *argv[]) {
int sock_fd;
struct sockaddr_in server_addr;
struct Hostent *host;
Char buffer[1024];

if (ARGC < 2) {
Perror ("Need hostname");
Exit (1);
}

if ((host = gethostbyname (argv[1])) = = NULL) {
Perror ("gethostbyname");
Exit (1);
}

if ((SOCK_FD = socket (af_inet, sock_stream, 0)) = = 1) {
Perror ("socket");
Exit (1);
}

memset (&server_addr, 0, sizeof (struct sockaddr_in));
server_addr.sin_family = af_inet;
Server_addr.sin_port = htons (port);
server_addr.sin_addr = * (struct in_addr *) host->h_addr);

If Connect (sock_fd, (struct sockaddr *) (&AMP;SERVER_ADDR), sizeof (struct sockaddr)) = = 1) {
Perror ("Connect");
Exit (1);
}

while (1) {
printf ("Please input something:\n");
Fgets (buffer, 1024, stdin);
Write (sock_fd, buffer, strlen (buffer));
Read (sock_fd, buffer, 1024);
printf ("From server:%s\n", buffer);
}
Close (SOCK_FD);

return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.