C/S model-udp Mode

Source: Internet
Author: User

UDP server-client relationship diagram

Procedure:

 

 

# 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 );

}

}

UDP client program:

 

# 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;

}

Server procedure(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.

 

UDP server program:

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.