UNIX network programming Getting Started Client server example

Source: Internet
Author: User
Tags socket error htons

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 (&AMP;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 (&AMP;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

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.