An example of a socket programming under Linux

zhaozj2021-02-16  46

/ ******* Server program (Server.c) ************ / #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include

int main (int argc, char * argv []) {int sockfd, new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size, portnumber; "!? Hello Are You Fine / n" char hello [] =;

IF (argc! = 2) {fprintf (stderr, "usage:% s portnumber / a / n", argv [0]); exit (1);} if ((portNumber = ATOI (Argv [1])) < 0) {FPRINTF (stderr, "usage:% s portnumber / a / n", argv [0]); exit (1);}

/ * The server is started to establish a socket descriptor * / if ((Sockfd = socket (AF_INET, SOCK_STREAM, 0)) == - 1) {FPRINTF (stderr, "socket error:% s / n / a", stror (errno) )); Exit (1);

/ * Fill server sockaddr structure * / bzero (& server_addr, sizeof (struct sockaddr_in)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl (INADDR_ANY); server_addr.sin_port = htons (portnumber); / * bundled sockfd Descriptor * / if (Bind (Sockfd, (Struct SockAddr *) (& Server_ADDR), Sizeof (Struct SockAddr)) == - 1) {Fprintf (stderr, "Bind Error:% S / N / A", strerror (errno )); Exit (1);} / * listening to SOCKFD descriptor * / if (listen (SockFD, 5) == - 1) {FPRINTF (stderr, "listen error:% s / n / a", strerror (errno )); Exit (1);

While (1) {/ * server block until the client establishes the connection * / sin_size = sizeof (struct socddr_in); if ((new_fd = accept (sockfd, (struct socddr *) (& client_addr), & sin_size) == - 1 ) {FPRINTF (stderr, "accept error:% s / n / a", strrror (errno)); exit (1);}

FPRINTF (stderr, "serverr Get connection from% s / n", inet_ntoa (client_addr.sin_addr)); if (write (new_fd, hello, strlen (hello) == - 1) {fprintf (stderr, "WRITE Error: % S / N ", STRERROR (Errno)); EXIT (1);} / * This communication has ended * / close (new_fd); / * loop next * /} close (sockfd); exit (0);} Client program

/ ******* Client program client.c *********************** / #include #include #include #include #include #include #include #include #include

INT Main (int Argc, char * argv []) {int suffd; char buffer [1024]; struct sockaddr_in server_addr; struct hostent * host; int portnumber, nbytes;

IF (Argc! = 3) {FPRINTF (stderr, "usage:% s hostname portnumber / a / n", argv [0]); exit (1);} if ((Host = gethostByName)) == NULL) {fprintf (stderr, "gethostname error / n"); exit (1);}

IF ((PortNumber = ATOI (Argv [2])) <0) {fprintf (stderr, "usage:% s hostname portnumber / a / n", argv [0]); exit (1);} / * client program Start establishing SOCKFD descriptor * / if ((SockFD = socket (AF_INET, SOCK_STREAM, 0) == - 1) {FPRINTF (stderr, "socket error:% s / A / n", strrror (errno)); exit (1); }

/ * Fill the client server data * / bzero (& server_addr, sizeof (server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons (portnumber); server_addr.sin_addr = * ((struct in_addr *) host-> h_addr ); / * Client Program initiated connection request * / if (Sockfd, (Struct SockAddr *) (& Server_addr), SIZEOF (STRUCKADDR)) == - 1) {FPRINTF (stderr, "Connect Error:% S / A / N ", strerror (errno)); exit (1);}

/ * The connection is successful * / if ((Nbytes = Read (SockFD, Buffer, 1024)) == - 1) {Fprintf (stderr, "read error:% s / n", strrror (errno)); exit (1 }} Buffer [nbytes] = '/ 0'; Printf ("I Have ReceiVed:% S / N", Buffer; / * End Communication * / Close (SockFD); EXIT (0);} Makefile

######### Makefile ########### All: Server Client Server: Server.c GCC $ ^ -O $ @ client: client.c gcc $ ^ -o $ @

Two programs and clients will be generated after running Make.

Process: Server Socket -> Bind -> Listen -> Accept

Client Socket -> Connect

转载请注明原文地址:https://www.9cbs.com/read-19853.html

New Post(0)