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 ):
- 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.
UDP server program: