Difference Between TCP and UDP
- Connection-based and connectionless
- Requirements on system resources (more TCP and less UDP)
- Simple UDP program structure
- Stream mode and datagram Mode
TCP ensures data correctness and UDP may cause packet loss.
TCP ensures data order, But UDP does not
Differences in programming
- The socket () parameters are different.
- UDP server does not need to call listen and accept
- Use the sendto/recvfrom function to send and receive UDP data
- TCP: The address is determined at connect/accept.
UDP: Specify the address information each time in the sendto/recvfrom function.
- UDP: the shutdown function is invalid.
When some of the following requirements are met, UDP-oriented datagram should be used
- Most of network data is short messages.
- Large number of clients
- No special requirements on Data Security
- Heavy network load, but high response speed requirements
Example: ICQ and Ping
Server program flow (multi-process ):
- Program Initialization
- Enter local address information
- Bind and listen to a fixed port
- Establish a socket connection after receiving the Client Connection
- Generate a new process for communication and information processing with the client
- Disconnect from the client after the sub-communication ends
Client procedure:
- Program Initialization
- Enter the server address
- Connect to the server
- Communication with servers and Information Processing
- Disconnect after communication ends
Server code
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <sys/Wait. H>
# Define myport 3490/* listening port */
# Define backlog 10/* length of the listen request receiving queue */
Int main (){
Int sockfd, new_fd;/* listener port, data port */
Struct sockaddr_in SA;/* address information */
Struct sockaddr_in their_addr;/* connection address information */
Int sin_size;
If (sockfd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
SA. sin_family = af_inet;
SA. sin_port = htons (myport);/* network byte sequence */
SA. sin_addr.s_addr = inaddr_any;/* automatically fill in the local IP Address */
Bzero (& (SA. sin_zero), 8);/* set the remaining part to 0 */
If (BIND (sockfd, (struct sockaddr *) & SA, sizeof (SA) =-1 ){
Perror ("bind ");
Exit (1 );
}
If (Listen (sockfd, backlog) =-1 ){
Perror ("listen ");
Exit (1 );
}
/* Main Loop */
While (1 ){
Sin_size = sizeof (struct sockaddr_in );
New_fd = accept (sockfd, (struct sockaddr *) & their_addr, & sin_size ));
If (new_fd =-1 ){
Perror ("accept ");
Continue;
}
Printf ("got connection from % s/n", inet_ntoa (their_addr.sin_addr ));
If (Fork () = 0 ){
/* Sub-process */
If (send (new_fd, "Hello, world! /N ", 14, 0) =-1)
Perror ("send ");
Close (new_fd );
Exit (0 );
}
Close (new_fd );
/* Clear all sub-Processes */
While (waitpid (-1, null, wnohang)> 0 );
}
}
Client code
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Define Port 3490/* server port */
# Define maxdatasize 100/* Maximum number of bytes that can be read at a time */
Int main (INT argc, char * argv [])
{
Int sockfd, numbytes;
Char Buf [maxdatasize];
Struct hostent * He;/* host information */
Struct sockaddr_in their_addr;/* peer address information */
If (argc! = 2 ){
Fprintf (stderr, "Usage: client hostname/N ");
Exit (1 );
}
/* Get the host info */
If (He = gethostbyname (argv [1]) = NULL ){
/* Note: herror instead of perror is required when an error occurs when obtaining DNS information */
Herror ("gethostbyname ");
Exit (1 );
}
If (sockfd = socket (af_inet, sock_stream, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
Their_addr.sin_family = af_inet;
Their_addr.sin_port = htons (port);/* short, nbo */
Their_addr.sin_addr = * (struct in_addr *) He-> h_addr );
Bzero (& (their_addr.sin_zero), 8);/* set the remaining part to 0 */
If (connect (sockfd, (struct sockaddr *) & their_addr, sizeof (struct sockaddr) =-1 ){
Perror ("Connect ");
Exit (1 );
}
If (numbytes = Recv (sockfd, Buf, maxdatasize, 0) =-1 ){
Perror ("Recv ");
Exit (1 );
}
Buf [numbytes] = '/0 ';
Printf ("Received: % s", Buf );
Close (sockfd );
Return 0;
}
Server program process (single process ):
- Program Initialization
- Enter local address information
- Bind a fixed port
- Process and communicate after receiving the client Datagram
- Disconnect after communication ends
Client procedure:
- Program Initialization
- Enter the server address
- Connect to the server
- Communication with servers and Information Processing
- Disconnect after communication ends
In UDP mode, there is little difference between the server and the client program. Only step 3 is different.
Server
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Include <sys/Wait. H>
# Define myport 3490/* listener port */
Void main ()
{
Int sockfd;/* data port */
Struct sockaddr_in my_addr;/* address information */
Struct sockaddr_in their_addr;/* connection address information */
Int sin_size, retval;
Char Buf [128];
If (sockfd = socket (af_inet, sock_dgram, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
My_addr.sin_family = af_inet;
My_addr.sin_port = htons (myport);/* network byte sequence */
My_addr.sin_addr.s_addr = inaddr_any;/* automatically fill in the local IP Address */
Bzero (& (my_addr.sin_zero), 8);/* set the remaining part to 0 */
If (BIND (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr) =-1 ){
Perror ("bind ");
Exit (1 );
}
/* Main Loop */
While (1 ){
Retval = recvfrom (sockfd, Buf, 128, 0, (struct sockaddr *) & their_addr, & sin_size );
Printf ("received datainfrom % s/n", inet_ntoa (their_addr.sin_addr ));
If (retval = 0 ){
Perror ("recvfrom ");
Close (sockfd );
Break;
}
Retval = sendto (sockfd, Buf, 128, 0, (struct sockaddr *) & their_addr, sin_size );
}
}
Client
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>
# Define Port 3490/* server port */
# Define maxdatasize 100/* Maximum number of bytes that can be read at a time */
Int main (INT argc, char * argv [])
{
Int sockfd, numbytes, sin_size;
Char Buf [maxdatasize] = "Hello, world !";
Struct hostent * He;/* host information */
Struct sockaddr_in their_addr;/* peer address information */
If (argc! = 2 ){
Fprintf (stderr, "Usage: client hostname/N ");
Exit (1 );
}
/* Get the host info */
If (He = gethostbyname (argv [1]) = NULL ){
Herror ("gethostbyname ");
Exit (1 );
}
If (sockfd = socket (af_inet, sock_dgram, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
Their_addr.sin_family = af_inet;
Their_addr.sin_port = htons (port);/* short, nbo */
Their_addr.sin_addr = * (struct in_addr *) He-> h_addr );
Bzero (& (their_addr.sin_zero), 8);/* set the remaining part to 0 */
Numbytes = sendto (sockfd, Buf, maxdatasize, 0, (struct sockaddr *) & their_addr, sizeof (their_addr ));
If (numbytes =-1 ){
Perror ("sendto ");
Exit (1 );
}
Printf ("Send: % s", Buf );
Numbytes = recvfrom (sockfd, Buf, maxdatasize, 0, (struct sockaddr *) & their_addr, & sin_size );
If (numbytes =-1 ){
Perror ("recvfrom ");
Exit (1 );
}
Buf [numbytes] = '/0 ';
Printf ("Received: % s", Buf );
Close (sockfd );
Return 0;
} // Reprinted Source: http://www.cnblogs.com/chenxizhang/archive/2009/04/14/1435777.html