This article is from: view the original article. Click here O (partition _ partition) o ha!
This article explains with Windows Socket, Development Environment vs2010, testing platform Windows 7
As we all know, TCP (Transmission Control Protocol) is a connection-oriented, reliable, and byte stream-based communication protocol. Developing a TCP socket application is simple. The following describes the development process using a simple demo.
The program is divided into the client and the server. The client sends data to the server, and the server displays the received data.
The main process of TCP server and client:
Server: 1 create socket 2 bind 3 listen 4 accept connection 5 send and receive data 6 close
Client: 1 create socket 2 Connect 3 send and receive data 4 close
Main functions used in implementation and
Server: 1 wsastartup () 2 socket () 3 BIND () 4 listen () 5 accept () 6 Recv () 7 closesocket () 8 wsacleanup ()
Client: 1 wsastartup () 2 socket () 3 connect () 4 send () 5 closesocket () 6 wsacleanup ()
The following is the implementation code of the server and client. When running, start the server first, and then start the client, "Hello world" is printed in the server section ".
Server. cpp source code
# Include "stdafx. H "# define buf_szie 64 # include" winsock2.h "# pragma comment (Lib," ws2_32.lib ") int main (INT argc, char * argv []) {wsadata WSD; /* initialize the nested dynamic library */If (wsastartup (makeword (2, 2), & WSD )! = 0) {printf ("wsastartup failed! \ N "); return 1;}/* Create socket */socket sserver = socket (af_inet, sock_stream, ipproto_tcp); If (invalid_socket = sserver) {printf ("socket failed! \ N "); wsacleanup (); Return-1;}/* server socket address */sockaddr_in addrserv; addrserv. sin_family = af_inet; addrserv. sin_port = htons (4999); addrserv. sin_addr.s_addr = inaddr_any;/* bind socket */INT retval = BIND (sserver, (lpsockaddr) & addrserv, sizeof (sockaddr_in); If (socket_error = retval) {closesocket (sserver); wsacleanup (); Return-1;}/* Start listening */retval = listen (sserver, 1); If (socket_error = retval) {closesocket (sserver); wsacleanup (); Return-1;}/* accept client requests */sockaddr_in addrclient; int addrclientlen = sizeof (addrclient); socket sclient = accept (sserver, (sockaddr far *) & addrclient, & addrclientlen); If (invalid_socket = sclient) {closesocket (sserver); wsacleanup (); Return-1 ;} /* receive client data */Char Buf [buf_szie]; zeromemory (BUF, buf_szie); retval = Recv (sclient, Buf, buf_szie, 0); If (socket_error = retval) {closesocket (sserver); closesocket (sclient); wsacleanup (); Return-1;} printf ("% s \ n", Buf ); /* exit */closesocket (sserver); closesocket (sclient); wsacleanup (); getchar (); Return 0 ;}
Client. cpp source code
# Include "stdafx. H "# define buf_szie 64 # include" winsock2.h "# pragma comment (Lib," ws2_32.lib ") int main (INT argc, char * argv []) {wsadata WSD; if (wsastartup (makeword (2, 2), & WSD )! = 0) {printf ("wsastartup failed! \ N "); Return-1;}/* Create a socket */socket shost = socket (af_inet, sock_stream, ipproto_tcp); If (invalid_socket = shost) {wsacleanup (); return-1;}/* set the server address */sockaddr_in servaddr; servaddr. sin_family = af_inet; servaddr. sin_addr.s_addr = inet_addr ("127.0.0.1"); servaddr. sin_port = htons (short) 4999); int nservaddlen = sizeof (servaddr);/* connect to the server */INT retval = connect (shost, (lpsockaddr) & servaddr, sizeof (servaddr); If (socket_error = retval) {closesocket (shost); wsacleanup (); Return-1 ;} /* send data to the server */Char Buf [buf_szie]; zeromemory (BUF, buf_szie); strcpy (BUF, "Hello World"); retval = Send (shost, Buf, strlen (BUF), 0); If (socket_error = retval) {closesocket (shost); wsacleanup (); Return-1;}/* exit */closesocket (shost ); wsacleanup (); getchar (); Return 0 ;}
PS: I 'd like to say a little more. If you have never been able to implement communication, try another port ~~~
I 've been doing this for a long time .... To find that the port is occupied .... Amount .... Second (second) Second