C + + Windows sockets for the simplest C/S network communication (TCP)

Source: Internet
Author: User
Tags htons

1. Server-side code:

#include <iostream> #include <WinSock2.h> #pragma comment (lib, "Ws2_32.lib") #define Connect_num_max 10using namespace Std;int Main () {//Load socket font 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 cor rect! "<< 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;} Initializes the server address family variable sockaddr_in addrsrv;addrsrv.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;} Monitor IRet = Listen (ServerSocket, Connect_num_max), if (IRet = = socket_error) {cout << "Listen (ServerSocket, 10) Execute failed! "<< endl;return-1;} Wait for connection _ receive _ 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, 0);p rintf ("%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 socket font wsadata wsadata;int iRet =0;iret = WSAStartup (Makeword (2, 2), &wsadata); if (IRet! = 0) {cout << Wsastart Up (Makeword (2, 2), &wsadata) Execute failed! "<< endl;return-1;} if (2! = Lobyte (wsadata.wversion) | | 2! = hibyte (wsadata.wversion)) {wsacleanup (); cout << "wsadata version is not cor rect! "<< endl;return-1;} Create 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;} Initializes the server-side address family variable sockaddr_in srvaddr;srvaddr.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); srvaddr.sin_family = Af_inet;srvaddr.sin_port = htons (6000);//Connect Server Iret = Connect ( Clientsocket, (sockaddr*) &srvaddr, sizeof (SOCKADDR)); if (0! = IRet) {cout << connect (clientsocket, sockaddr *) &srvaddr, sizeof (SOCKADDR)) Execute FAiled! "<< endl;return-1;} Receive Message char recvbuf[100];recv (clientsocket, recvbuf, 0);p rintf ("%s\n", recvbuf);//Send Message char sendbuf[100];sprintf_ S (sendbuf, "Hello, this is client%s", "Rabbit"); Send (Clientsocket, SendBuf, strlen (sendbuf) +1, 0);//Cleanup closesocket ( Clientsocket); WSACleanup (); System ("pause"); return 0;}

3. Program Operation:

C + + Windows sockets for the simplest C/S network communication (TCP)

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.