Network Programming-Comparison of TCP/UDP and its Process

Source: Internet
Author: User
Tags htons

Difference Between TCP and UDP

  1. Connection-based and connectionless
  2. Requirements on system resources (more TCP and less UDP)
  3. Simple UDP program structure
  4. 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

  1. The socket () parameters are different.
  2. UDP server does not need to call listen and accept
  3. Use the sendto/recvfrom function to send and receive UDP data
  4. TCP: The address is determined at connect/accept.
    UDP: Specify the address information each time in the sendto/recvfrom function.
  5. UDP: the shutdown function is invalid.

When some of the following requirements are met, UDP-oriented datagram should be used

  1. Most of network data is short messages.
  2. Large number of clients
  3. No special requirements on Data Security
  4. Heavy network load, but high response speed requirements

Example: ICQ and Ping

Server program flow (multi-process ):

  1. Program Initialization
  2. Enter local address information
  3. Bind and listen to a fixed port
  4. Establish a socket connection after receiving the Client Connection
  5. Generate a new process for communication and information processing with the client
  6. Disconnect from the client after the sub-communication ends

Client procedure:

  1. Program Initialization
  2. Enter the server address
  3. Connect to the server
  4. Communication with servers and Information Processing
  5. 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 ):

  1. Program Initialization
  2. Enter local address information
  3. Bind a fixed port
  4. Process and communicate after receiving the client Datagram
  5. Disconnect after communication ends

Client procedure:

  1. Program Initialization
  2. Enter the server address
  3. Connect to the server
  4. Communication with servers and Information Processing
  5. 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

 

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.