Tcp-client-c++

Source: Internet
Author: User

#include"stdafx.h"#include<Winsock2.h>#include<iostream>#pragmaComment (lib, "Ws2_32.lib")using namespacestd;voidMain () {interr;    WORD versionrequired;    Wsadata Wsadata; Versionrequired=makeword (1,1); Err=wsastartup (Versionrequired,&wsadata);//version information for the Protocol library    if(!err) {printf ("The client nested word has been opened!\n"); }    Else{printf ("the client's nested word open failed!\n"); return;//End} SOCKET Clientsocket=socket (Af_inet,sock_stream,0);    Sockaddr_in clientsock_in; Clientsock_in.sin_addr. S_un. S_addr=INET_ADDR ("127.0.0.1"); Clientsock_in.sin_family=af_inet; Clientsock_in.sin_port=htons (8001); //bind (Clientsocket, (sockaddr*) &clientsock_in,strlen (sockaddr));//Note the third parameter//Listen (clientsocket,5);Connect (Clientsocket, (sockaddr*) &clientsock_in,sizeof(SOCKADDR));//Start Connection    Charreceivebuf[ -]; Recv (Clientsocket,receivebuf,101,0); printf ("%s\n", RECEIVEBUF); //Send (Clientsocket, "Hello,this is the client", strlen ("Hello,this is Client") +1,0);Send (Clientsocket,"123", strlen ("123")+1,0); Charreceivebuff[ -]; Recv (Clientsocket,receivebuff,101,0); printf ("%s\n", Receivebuff);    Closesocket (Clientsocket); WSACleanup ();}

Tcp-client-c++

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.