Recently in the "UNIX Network Programming" (abbreviated UNP) and "Linux program Design", for the first time in UNP to get the example of the server, practice always a little headache, Because the author contains all of the declarations in the unp.h, resulting in the subsequent writing of the code will be dependent on the header file, and learn not to call the corresponding function, which should contain the exact header files.
Furthermore, after I downloaded the unp.h, the header file contains another dependency missing, so I refer to the header file containing the introductory example of the socket chapter in the Linux programming program. And the included or macro definitions that are still not found in the compilation are searched and pasted in the unp.h, so that no more custom header files like Unp.h are required to be included.
The following code runs under root permissions because the port number 13 used may not be accessible to non-root users.
UNP the original book in the server to get the machine time back to the client, but I was running, the string saved to the buffer area of the function snprintf () always caused a memory error and interrupted, so the purpose of the entry, instead of copying a word into buffer.
/*client.c*/#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<sys/un.h>#include<unistd.h>#include<stdlib.h>#include<netinet/inch.h>#defineMAXLINE 4096#definePORT 13#defineSA struct SOCKADDRintMainintargcChar*argv[]) { intSOCKFD, N; CharRecvline[maxline+1]; structsockaddr_in servaddr; if(ARGC! =2) {fprintf (stderr,"usage:client <ip>\n"); Exit (1); } if(SOCKFD = socket (af_inet, Sock_stream,0)) <0) {fprintf (stderr,"Socket error\n"); Exit (1); } bzero (&SERVADDR,sizeof(SERVADDR)); Servaddr.sin_family=af_inet; Servaddr.sin_port=htons (PORT); if(Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <=0) {fprintf (stderr,"Inet_pton error for%s\n", argv[1]); Exit (1); } if(Connect (SOCKFD, (SA *) &servaddr,sizeof(SERVADDR)) <0) {fprintf (stderr,"Connect error\n"); Exit (1); } while((n = Read (SOCKFD, Recvline, MAXLINE)) >0) {Recvline[n]=0; if(Fputs (recvline, stdout) = =EOF) {fprintf (stderr,"fputs error\n"); Exit (1); } } if(N <0) {fprintf (stderr,"Read error\n"); Exit (1); } exit (0);}
/*server.c*/#include<sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<sys/un.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<netinet/inch.h>#defineMAXLINE 4096#defineListenq 1024#definePORT 13#defineSA struct SOCKADDRintMainintargcChar*argv[]) { intLISTENFD, CONNFD; structsockaddr_in servaddr; CharBuff[maxline+1]; time_t T; LISTENFD= Socket (Af_inet, Sock_stream,0); Bzero (&SERVADDR,sizeof(SERVADDR)); Servaddr.sin_family=af_inet; Servaddr.sin_addr.s_addr=htonl (Inaddr_any); Servaddr.sin_port=htons (PORT); Bind (LISTENFD, (SA*) &servaddr,sizeof(SERVADDR)); Listen (LISTENFD, Listenq); while(1) {CONNFD= Accept (LISTENFD, (SA *) NULL, and NULL); /*write something to buff[]*/strcpy (Buff,"This is a message\n"); Write (CONNFD, buff, strlen (buff)); Close (CONNFD); } exit (0);}
To compile the file:
1 gcc client.c-o client2gcc server.c-o Server
To run the server in a window:
1 $./server
Run the client in another window to display a word "This is a message":
1 127.0. 0.1
UNIX network programming Getting Started Client server example