"Network programming Notes" Simple TCP protocol Socket programming (C language Edition Server and client)

Source: Internet
Author: User
Tags socket error htons

is a generic socket programming diagram .

Establishment of the server:

Gets the native name used to parse the IP address of the machine, then the IP address is successfully bound, then the server is listening, waiting for the client to connect. That is, the next step is the TCP/IP three handshake operation, the operation succeeds the server and the client can communicate. Here is the comment code:

1 voidSocketconnect ()2 {3     Charserver_name[ -];4 wsadata wsadata;5     structHostent *HP;6     structsockaddr_in addrserver;7     8WSAStartup (0x202, &wsadata);9     TenSOCKFD = socket (af_inet, Sock_stream,0);//Socket OneGetHostName (server_name, -); AHP = gethostbyname (server_name);//get the native name -printf"machine Name:%s\n", server_name); -  thememset (&addrserver,0,sizeof(Addrserver));//Initialize -memcpy (& (ADDRSERVER.SIN_ADDR), hp->h_addr, hp->h_length);//set the IP address of the server -addrserver.sin_family = af_inet;//Set Version -Addrserver.sin_port = htons ((unsigned Short) server_port);//set the port number +     //bind .... Bind IP address and port number -     if(Socket_error = = Bind (SOCKFD, (LPSOCKADDR) &addrserver,sizeof(Addrserver))) +     { Aprintf"Bind Socket error!\n"); at         //exit (1); -     } -     Else -     { -printf"bind socket success...\n"); -     } inListen (SOCKFD,5);//listening ... Monitor -printf"Server listening status ... \ n"); to  +ZeroMemory ((void*) &addrtemp,sizeof(addrtemp)); -Nlen =sizeof(addrtemp); theCONNFD = Accept (SOCKFD, &addrtemp, &nlen);//Accept ... Three-time handshake *printf"Accept Client success...\n\n"); $}

Client-side creation:

In fact, the client and server code are similar, but the most behind a connect () function, is to send a request link to the server, because the server in the Accept () function block waiting for the client link, and then after three handshake, after success, the server and the client can communicate.

1WSAStartup (0x202, &wsadata);2 if(SOCKFD = socket (af_inet, Sock_stream,0)) <0)//socket ...3 {4printf"Socket error!\n");5Exit1);6 }    7GetHostName (server_name, -);8Host = gethostbyname (server_name);//get the native name9printf"machine Name:%s\n", server_name);Ten  Onememset (&addrserver,0,sizeof(Addrserver));//Initialize Amemcpy (& (ADDRSERVER.SIN_ADDR), host->h_addr, host->h_length);//Set IP -addrserver.sin_family = af_inet;//Set the version number -Addrserver.sin_port = htons ((unsigned Short) server_port);//set the port number the  - //connect .... Initiating a link to the server - if(Connect (SOCKFD,structSOCKADDR *) &addrserver,sizeof(structsockaddr ))) - { +printf"Connect error!\n"); -Exit1); + } Aprintf"Connection Server succeeded Connec success...\n\n");

Next is the Send () and recv () functions:

1 //send ... Send Message2scanf"%s", sendbuf);3 GetChar ();4 5 if(sendbytes = Send (CONNFD, SendBuf, strlen (SENDBUF),0)) <=0)6 {7printf"Send Error!\n");8     Continue;9 }Tenmemset (SendBuf,0,sizeof(SENDBUF));
1 //recv ... Receiving Messages2memset (Recvbuf,0,sizeof(RECVBUF));3 if(Recvbytes = recv (CONNFD, Recvbuf,sizeof(RECVBUF),0)) <=0)4 {5printf"recv error!\n");6Exit1);7 }8printf"recvbytes =%d\n", recvbytes);9printf"recv from client:%s\n", RECVBUF);

Reference Original: http://blog.csdn.net/shiren_bod/article/details/6647022

"Network programming Notes" Simple TCP protocol Socket programming (C language Edition Server and client)

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.