C + + simple TCP implementation [socket] client and client communication __c++

Source: Internet
Author: User
Tags htons

Development Environment Vs 2008 Create a new Win32 console project

Server-side code

Server.cpp:Defines the entry point for the console application. #include "stdafx.h" #include "winsock2.h" #pragma comment (lib, "Ws2_32.lib") #include <iostream> using namespace Std int _tmain (int argc, char* argv[]) {const int buf_size = 64; Wsadata WSD; Wsadata variable SOCKET sserver; Server socket socket sclient; Client socket sockaddr_in Addrserv;; Server address char buf[buf_size]; Receive data buffer char sendbuf[buf_size];//returns the data int retVal to the client; return value//Initialize socket dynamic Library if (WSAStartup (Makeword (2,2), &AMP;WSD)!= 0) {cout << "WSAStartup failed!" << Endl; 1; ///Create Socket sserver = socket (af_inet, sock_stream, ipproto_tcp); if (Invalid_socket = = sserver) {cout << "SOCKET failed!" << Endl; WSACleanup ();//free socket resource; return-1; }//server socket address addrserv.sin_family = af_inet; Addrserv.sin_port = htons (4999); ADDRSERV.SIN_ADDR.S_ADDR = Inaddr_any; Binding Sockets RetVal = Bind (Sserver, (lpsockaddr) &addrserv, sizeof (sockaddr_in)); if (Socket_error = = retVal) {cout << "bind failed!";< Endl; Closesocket (sserver); Close socket WSACleanup (); releasing socket resources; return-1; //start Listening RetVal = Listen (sserver, 1); if (Socket_error = = retVal) {cout << "Listen failed!" << Endl; closesocket (sserver);//Close Socket WSACleanup ();//Interpretation put socket resources; return-1; //Accept client request sockaddr_in addrclient; int addrclientlen = sizeof (addrclient); Sclient = Accept (Sserver, (sockaddr far*) &addrclient, &addrclientlen); if (Invalid_socket = = sclient) {cout << "accept failed!" << Endl; closesocket (sserver);//Close Socket WSACleanup (); releasing socket resources; return-1; } while (true) {//Receive client Data ZeroMemory (BUF, buf_size); retVal = recv (sclient, buf, buf_size, 0); if (socket_error = = RetVal) {cout << "recv failed!" << Endl; closesocket (sserver);//Close Socket closesocket (sclient);//Close Socket WSACleanup ();// releasing socket resources; return-1; } if (buf[0] = = ' 0 ') break; cout << "Client-sent data:" << buf <<endl; cout << "Send data to client:"; Cin >> SendBuf; Send (Sclient, SendBuf, strlen (SENDBUF), 0); }//FallbackOut closesocket (sserver); Close socket closesocket (sclient); Close socket WSACleanup (); releasing socket resources; return 0; } 

Client code

Client.cpp:Defines the entry point for the console application. #include "stdafx.h" #include "winsock2.h" #include <iostream> #pragma comment (lib, "Ws2_32.lib") using namespace Std BOOL Recvline (SOCKET s, char* buf); Reads a row of data int main (int argc, char* argv[]) {const int buf_size = 64; Wsadata WSD; Wsadata variable SOCKET shost; Server socket sockaddr_in servaddr; Server address char buf[buf_size]; Receive data buffer char bufrecv[buf_size]; int retVal; return value//Initialize socket dynamic Library if (WSAStartup (Makeword (2,2), &AMP;WSD)!= 0) {cout << "WSAStartup failed!" << Endl; -1; ///Create Socket Shost = socket (af_inet, sock_stream, ipproto_tcp); if (Invalid_socket = = shost) {cout << "SOCKET failed!" << Endl; WSACleanup ()//releasing socket resource return-1; //Set server address 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); Connection Server Retval=connect (Shost, (lpsockaddr) &servaddr, sizeof (SERVADDR)); if (socket_error = = RetVal {cout << "connect failed!" << Endl; closesocket (shost);//Close Socket WSACleanup ();//Release Socket resource return -1; {(true) {//Send data to Server ZeroMemory (buf, buf_size); cout << send data to server: "; Cin >> BUF; RetVal = Send (Shost, buf, strlen (BUF), 0); if (Socket_error = = retVal) {cout << "send failed!" << Endl; closesocket (shost);//Close Socket WSACleanup ();//Release socket Word resource return-1; }//recvline (Shost, BUFRECV); Recv (shost, bufrecv,5, 0); Receive server-side data, receive only 5 characters cout << Endl << "receive data from server:" << bufrecv; }//Exit closesocket (Shost); Close socket WSACleanup (); Release socket resource return 0; } 

 

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.