C ++ Windows Socket implements the simplest C/S network communication (TCP) and socket network communication

Source: Internet
Author: User

C ++ Windows Socket implements the simplest C/S network communication (TCP) and socket network communication

1. server code:

# Include <iostream> # include <WinSock2.h> # pragma comment (lib, "ws2_32.lib") # define CONNECT_NUM_MAX 10 using namespace std; int main () {// load the socket library WSADATA wsaData; int iRet = 0; iRet = WSAStartup (MAKEWORD (2, 2), & wsaData); if (iRet! = 0) {cout <"WSAStartup (MAKEWORD (2, 2), & wsaData) execute failed! "<Endl; return-1;} if (2! = LOBYTE (wsaData. wVersion) | 2! = HIBYTE (wsaData. wVersion) {WSACleanup (); cout <"WSADATA version is not correct! "<Endl; return-1;} // create SOCKET socket serverSocket = SOCKET (AF_INET, SOCK_STREAM, 0); if (serverSocket = INVALID_SOCKET) {cout <"serverSocket = socket (AF_INET, SOCK_STREAM, 0) execute failed! "<Endl; return-1;} // initialize the SOCKADDR_IN addrSrv and addrSrv of the server address family. sin_addr.S_un.S_addr = htonl (INADDR_ANY); addrSrv. sin_family = AF_INET; addrSrv. sin_port = htons (6000); // bind iRet = bind (serverSocket, (SOCKADDR *) & addrSrv, sizeof (SOCKADDR); if (iRet = SOCKET_ERROR) {cout <"bind (serverSocket, (SOCKADDR *) & addrSrv, sizeof (SOCKADDR) execute failed! "<Endl; return-1 ;}// listener iRet = listen (serverSocket, CONNECT_NUM_MAX); if (iRet = SOCKET_ERROR) {cout <" listen (serverSocket, 10) execute failed! "<Endl; return-1;} // wait for connection _ receiver _ Send SOCKADDR_IN clientAddr; int len = sizeof (SOCKADDR); while (1) {SOCKET connSocket = accept (serverSocket, (SOCKADDR *) & clientAddr, & len); if (connSocket = INVALID_SOCKET) {cout <"accept (serverSocket, (SOCKADDR *) & clientAddr, & len) execute failed! "<Endl; return-1;} char sendBuf [100]; sprintf_s (sendBuf," Welcome % s ", inet_ntoa (clientAddr. sin_addr); send (connSocket, sendBuf, strlen (sendBuf) + 1, 0); char recvBuf [100]; recv (connSocket, recvBuf, 100, 0 ); printf ("% s \ n", recvBuf); closesocket (connSocket);} system ("pause"); return 0 ;}

 

2. client code:

# Include <iostream> # include <winsock2.h> using namespace std; # pragma comment (lib, "ws2_32.lib") int main () {// load the socket library WSADATA wsaData; int iRet = 0; iRet = WSAStartup (MAKEWORD (2, 2), & wsaData); if (iRet! = 0) {cout <"WSAStartup (MAKEWORD (2, 2), & wsaData) execute failed! "<Endl; return-1;} if (2! = LOBYTE (wsaData. wVersion) | 2! = HIBYTE (wsaData. wVersion) {WSACleanup (); cout <"WSADATA version is not correct! "<Endl; return-1;} // create a SOCKET: socket clientSocket = SOCKET (AF_INET, SOCK_STREAM, 0); if (clientSocket = INVALID_SOCKET) {cout <"clientSocket = socket (AF_INET, SOCK_STREAM, 0) execute failed! "<Endl; return-1;} // initialize the SOCKADDR_IN srvAddr and srvAddr variable of the server address family. sin_addr.S_un.S_addr = inet_addr ("127.0.0.1"); srvAddr. sin_family = AF_INET; srvAddr. sin_port = htons (6000); // connection server iRet = connect (clientSocket, (SOCKADDR *) & srvAddr, sizeof (SOCKADDR); if (0! = IRet) {cout <"connect (clientSocket, (SOCKADDR *) & srvAddr, sizeof (SOCKADDR) execute failed! "<Endl; return-1;} // receives the message char recvBuf [100]; recv (clientSocket, recvBuf, 100, 0); printf (" % s \ n ", recvBuf); // send the message char sendBuf [100]; sprintf_s (sendBuf, "Hello, This is client % s", "rabbit"); send (clientSocket, sendBuf, strlen (sendBuf) + 1, 0); // clear closesocket (clientSocket); WSACleanup (); system ("pause"); return 0 ;}

 

3. program running:

 

Related Article

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.