C + + uses sockets for TCP,UDP network communication

Source: Internet
Author: User

Learning Xinxin Sun Teacher's VC + +, there is a period of time, the first contact socket to tell the truth a bit do not understand, the first is basically to see him say a sentence I write a complete, the second time in the look at the socket a little bit feel, Next I use the socket to complete the TCP and UDP two communication mode of the process and code to share, I hope to be a little bit of help to everyone, what is wrong to welcome you to the great God.

TCP

TCP is a point-to-point communication mode, high quality data transmission, for the transmission of high data integrity requirements of the general use of TCP, specific to VC + +, the general choice is server/client mode, socket TCP communication in the server side generally divided into the following steps:

Server-side

1. Load socket font, load socket library code is generally fixed each copy below OK.

      WORD wversionrequested;
      wsadata wsadata;
      int err;
      Wversionrequested=makeword ();
      Err=wsastartup (Wversionrequested,&wsadata) ;
      if (err!=0)
    {
    return;
   }
      if (Lobyte (wsadata.wversion)!=1| | Hibyte (wsadata.wversion)!=1)
   {

WSACleanup ();
Return
}

2. Creating sockets

SOCKET Socksrv=socket (af_inet,sock_stream,0);
Sockaddr_in sockaddr;//Create an address structure, set its variables
Sockaddr.sin_addr. S_un. S_addr=htonl (Inaddr_any);
Sockaddr.sin_family=af_inet;
Sockaddr.sin_port=htons (6000);

3. Connect sockets and IP addresses and ports

Bind (Socksrv, (sockaddr*) &sockaddr,sizeof (sockaddr));//Binding

4. Set the socket to listen mode

Listen (socksrv,5);//set to listening mode

5. Create a linked socket

Sockaddr_in sockclient;
int len=sizeof (SOCKADDR);
Socket Sockcon=accept (SOCKSRV, (sockaddr*) &sockclient,&len);//create a linked socket

6. Sending and receiving data to the client

Char sendbuf[100];

Send (Sockcon,sendbuf,strlen (SENDBUF) +1,0);
Char recvbuf[100];
Recv (sockcon,recvbuf,100,0);//send \ receive function

7. Close the socket

Closesocket (Sockcon);//Close socket

Client

1. Loading socket font

As with server-side code, direct replication can

2. Creating sockets

SOCKET Sockclient=socket (af_inet,sock_stream,0);
Sockaddr_in sockaddr;
Sockaddr.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
Sockaddr.sin_family=af_inet;
Sockaddr.sin_port=htons (6000);

3. Sockets and IP addresses and port connections

Connect (sockclient, (sockaddr*) &sockaddr,sizeof (sockaddr));

4. Send and receive data to the server

Char recvbuf[100];
Recv (sockclient,recvbuf,100,0);
printf ("%s\n", recvbuf);
Send (sockclient, "Hello Server", strlen ("Server Hello") +1,0);

5. Close the socket

Closesocket (sockclient);
WSACleanup ();

Don't forget to add the header file #include<winsock2.h> at the same time to link ws2_32.lib. And then it's OK.

Udp

UDP is a non-connected, broadcast-based data transmission mechanism, its real-time high, the integrity of the output transmission can not be guaranteed, for some real-time requirements high, the integrity of the situation is not high use of UDP.

Server-side

1. Loading socket font

Don't say anything, just copy it.

2. Create a UDP socket

SOCKET Socksrv=socket (af_inet,sock_dgram, 0);//The difference between this parameter in the main and TCP mode
Sockaddr_in sockaddr;//Create an address structure, set its variables
Sockaddr.sin_addr. S_un. S_addr=htonl (Inaddr_any);
Sockaddr.sin_family=af_inet;
Sockaddr.sin_port=htons (6000);

3. Bind sockets and IP addresses and ports

Bind (Socksrv, (sockaddr*) &sockaddr,sizeof (sockaddr));//Binding

4. Receiving data

The reception function here is recvfrom ();

5. Close the socket

Closesocket (sock);

Client

1. Loading socket font

Don't say anything, just copy it.

2. Create a UDP socket

SOCKET Sockclient=socket (af_inet,sock_dgram, 0);
Sockaddr_in sockaddr;
Sockaddr.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
Sockaddr.sin_family=af_inet;
Sockaddr.sin_port=htons (6000);

3. Send data

SendTo ();//The Sending function is also different

4. Close the socket

Closesocket (sockclient);
WSACleanup ();

Don't forget to add the header file #include<winsock2.h> at the same time to link ws2_32.lib. And then it's OK.

C + + uses sockets for TCP,UDP network communication

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.