TCP Service Side
1#include <WinSock2.h>//Include header file2#include <stdio.h>3#include <Windows.h>4#include <iostream>5 #pragmaComment (lib, "Ws2_32.lib")//Display Connection Socket font6 intMain ()//main function Start7 {8Wsadata data;//defining Wsadata Structure Objects9WORD W=makeword (2,0);//Define version numberTen Charsztext[]="welcome you \ r \ n";//defines and initializes an array of characters sent to the client OneWSAStartup (W,&data);//initializing the socket font ASOCKET s,s1;//define a connection socket and a data transceiver socket handle -S=socket (Af_inet,sock_stream,0);//Create a TCP socket -Sockaddr_in ADDR,ADDR2;//defining a socket address structure the intn=sizeof(ADDR2);//Get socket address structure size -Addr.sin_family=af_inet;//Initialize the address structure -Addr.sin_port=htons ( the); -Addr.sin_addr. S_un. S_addr=Inaddr_any; +Bind (S, (sockaddr*) &addr,sizeof(addr));//binding Sockets -Listen (s),5);//Listening sockets +std::cout<<"server already started \ r"<<std::endl;//Output hint Information A while(true) at { -S1=accept (S, (sockaddr*) &addr2,&n);//Accept connection Request - if(s1!=NULL) - { -std::cout<<"already connected to the \ r"<<std::endl<<Inet_ntoa (addr2.sin_addr); -Send (S1,sztext,sizeof(Sztext),0);//send a character array to the client in } - closesocket (s); toClosesocket (S1);//Close Socket handle +WSACleanup ();//Release Socket Font - if(GetChar ())//If there is an input, close the program the { * return 0;//Normal shutdown program $ }Panax Notoginseng Else - { theSleep ( -);//application sleep 0.1 seconds + } A the } +}
Run results
After the server program starts, if no client sends a connection request to it, the server waits until the client program is known to connect.
TCP Client
1#include <WinSock2.h>//Include header file2#include <stdio.h>3#include <Windows.h>4#include <iostream>5 #pragmaComment (lib, "Ws2_32.lib")//Display Connection Socket font6 intMain ()//main function Start7 {8Wsadata data;//defining Wsadata Structure Objects9WORD W=makeword (2,0);//Define version numberTen Charsztext[Ten]={0};//defines and initializes an array of characters sent to the client OneWSAStartup (W,&data);//initializing the socket font ASOCKET s;//define a connection socket and a data transceiver socket handle -S=socket (Af_inet,sock_stream,0);//Create a TCP socket -Sockaddr_in addr;//defining a socket address structure theAddr.sin_family=af_inet;//Initialize the address structure -Addr.sin_port=htons ( the); -Addr.sin_addr. S_un. S_ADDR=INET_ADDR ("192.168.7.6"); -std::cout<<"client has started \ r"<<std::endl;//Output hint Information +Connect (S, (sockaddr*) &addr,sizeof(addr)); -Recv (S,sztext,sizeof(Sztext),0); +std::cout<<sztext<<Std::endl; AClosesocket (s);//Close Socket handle atWSACleanup ();//Release Socket Font - if(GetChar ())//If there is an input, close the program - { - return 0;//Normal shutdown program - } - Else in { -Sleep ( -);//application sleep 0.1 seconds to } + - the}
Compile and run the program, open the server, and then open the client, the server will accept the client's request, the client will also display the welcome message.
Visual C + + network programming--TCP Communication