The difference between TCP and UDP in socket communication

Source: Internet
Author: User
Tags htons

1.TCP Server side:

#include <Winsock2.h>
#include <stdio.h>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0) {
Return
}

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}
SOCKET Socksrv=socket (af_inet,sock_stream,0);

Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);

Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));

Listen (socksrv,5);

Sockaddr_in addrclient;
int len=sizeof (SOCKADDR);

while (1)
{
SOCKET sockconn=accept (Socksrv, (sockaddr*) &addrclient,&len);
Char sendbuf[100];
sprintf (SendBuf, "Welcome%s to http://www.sunxin.org",
Inet_ntoa (ADDRCLIENT.SIN_ADDR));
Send (Sockconn,sendbuf,strlen (SENDBUF) +1,0);
Char recvbuf[100];
Recv (sockconn,recvbuf,100,0);
printf ("%s/n", recvbuf);
Closesocket (Sockconn);
}
}

2.TCP Client:

#include <Winsock2.h>
#include <stdio.h>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0) {
Return
}

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}
SOCKET Sockclient=socket (af_inet,sock_stream,0);

Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);
Connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));

Char recvbuf[100];
Recv (sockclient,recvbuf,100,0);
printf ("%s/n", recvbuf);
Send (Sockclient, "This are Lisi", strlen ("This is Lisi") +1,0);

Closesocket (sockclient);
WSACleanup ();
}

3.UDP Server Side

#include <Winsock2.h>
#include <stdio.h>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0) {
Return
}

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}

SOCKET Socksrv=socket (af_inet,sock_dgram,0);
Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);

Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));

Sockaddr_in addrclient;
int len=sizeof (SOCKADDR);
Char recvbuf[100];

Recvfrom (socksrv,recvbuf,100,0, (sockaddr*) &addrclient,&len);
printf ("%s/n", recvbuf);
Closesocket (SOCKSRV);
WSACleanup ();
}

4.UDP Client

#include <Winsock2.h>
#include <stdio.h>

void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;

wversionrequested = Makeword (1, 1);

Err = WSAStartup (wversionrequested, &wsadata);
if (Err!= 0) {
Return
}

if (Lobyte (wsadata.wversion)!= 1 | |
Hibyte (wsadata.wversion)!= 1) {
WSACleanup ();
Return
}

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

SendTo (sockclient, "Hello", strlen ("Hello") +1,0,
(sockaddr*) &addrsrv,sizeof (sockaddr));
Closesocket (sockclient);
WSACleanup ();
}

The difference between TCP and UDP based on connection and connectionless requirements for system resources (TCP more, UDP less) UDP program structure simpler flow mode and datagram mode
TCP guarantees data correctness, UDP may lose packets
TCP guarantees data order, UDP does not guarantee

Specific programming when the difference between the parameters of the socket () different UDP server does not need to invoke listen and accept UDP transceiver data with the Sendto/recvfrom function TCP: Address information is determined when connect/accept
UDP: Address information must be specified each time in the Sendto/recvfrom function Udp:shutdown function is invalid

Some of the following requirements, should be adopted UDP-oriented datagram network data mostly for short messages have a large number of client for data security and no special requirements network burden is very heavy, but the response speed requires high

Example: ICQ, Ping

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.