Winsockpragramming (2) Client (c + + version)

Source: Internet
Author: User

Creating a client is similar to creating a server process, but slightly different, as I do not describe in detail here.
Create a client process
1, initialize the socket font

Initialize the socket font code:

data;//定义WSAData变量WOED wVersionRequested=MAKEWORD(2,0);//定义套接字库版本号::WSAStartup(wVersionRequested,&data);//初始化套接字库

2. Create socket handle

To create a socket handle code:

SOCKET s;//定义套接字句柄s=::socket(AF_INET,SOCK_STREAM,0);//创建并返回套接字句柄

3. Address structure setting and byte order conversion

Address structure settings and byte order conversion code:

sockaddr_in addr;//定义套接字地址结构变量in_addr in_add;//定义IP地址结构变量addr.sin_fimily=AF_INET;//指定地址家族为TCP/IPaddr.sin_port=htons(80);//指定端口号addr.sin_addr.S_un.S_addr=inet_addr(‘127.0.0.1‘);//将字符串IP转换为网络字节顺序排列的IPchar address[]=inet_ntoa(addr.sin_addr.S_un.S_addr)//将网络字节顺序排列的IP转换为字符串IP

4. Send connection request

Send connection request code

::connect(s,(sockaddr*)&addr,sizeof(addr));

5. Data sending and receiving

Data sending and receiving code:

::send(s1,sztext,sizeof(sztext),0);::recv(s,sztext,sizeof(sztext),0);

6. Close socket

Close the socket code:

::closesocket(s);

7. Release Socket font

Release Socket font code:

::WSACleanup();

Full Source:

/*client.cpp*/#include <winsock2.h>//Include header files #include <stdio.h>#include <windows.h>#pragma comment (lib, "Ws2_32.lib")//Explicit connection socket font intMain () {//Initialize socket fontWsadata data;//define WSADATA Structure objectWORD W=makeword (2,0);//define version number:: WSAStartup (W,&data);//Initialize socket font    Charsztext[Ten]={0};//Create socket handleSOCKET s;//define connection sockets and data receiver socket handlesS=::socket (Af_inet,sock_stream,0);//Create a TCP socket    //Address structure settings and byte conversionSockaddr_in addr;//Define socket address structureAddr.sin_family=af_inet;//Initialize address structureAddr.sin_port=htons ( the);//Set port numberAddr.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");printf("Client has started \ r \ n");//Output prompt information    //Send connection request:: Connect (S, (sockaddr*) &addr,sizeof(addr));//Receive server data:: Recv (S,sztext,sizeof(Sztext),0);printf("%s\r\n", Sztext);//close socket handle:: Closesocket (s);//Release socket font:: WSACleanup ();if(GetChar ()) {return 0; }Else{:: Sleep ( -);//Apply hibernation for 0.1 seconds}}

Winsockpragramming (2) Client (c + + version)

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.