UDP mode point-to-point communication
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <errno.h> #include <stdlib.h> #include <arpa/inet.h> int main (int argc, Char **argv) {struct sockaddr_in s_addr; struct sockaddr_in c_addr; int sock; socklen_t addr_len; int len; char buff[128]
; /* Create socket, the key is this sock_dgram * * * (sock = socket (af_inet, SOCK_DGRAM, 0)) = = 1) {perror ("socket"); exit (errno);}
else printf ("Create socket.\n\r");
memset (&s_addr, 0, sizeof (struct sockaddr_in));
/* Set address and port information * * s_addr.sin_family = af_inet;
if (argv[2]) S_addr.sin_port = htons (atoi (argv[2));
else S_addr.sin_port = htons (7838);
if (argv[1]) s_addr.sin_addr.s_addr = inet_addr (argv[1));
else s_addr.sin_addr.s_addr = Inaddr_any; /* Binding address and port information */if (bind (sock, (struct sockaddr *) &s_addr, sizeof (s_addr)) = = = 1) {perror ("bind"); exit (errno);}
else printf ("Bind address to socket.\n\r"); /* Circular Receive data * * * Addr_len= sizeof (C_ADDR); while (1) {len = recvfrom (sock, buff, sizeof (buff)-1, 0, (struct sockaddr *) &c_addr, &addr_len); if (Len <
0) {perror ("recvfrom"); exit (errno);}
Buff[len] = ' the ';
printf ("Received messages from%s:%d:%s\n\r", Inet_ntoa (C_ADDR.SIN_ADDR), Ntohs (c_addr.sin_port), buff);
return 0;
}
The
Client source code is as follows:
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <errno.h> #include <stdlib.h> #include <arpa/inet.h> int main (int argc,
Char **argv) {struct sockaddr_in s_addr; int sock; int addr_len; int len; char buff[128]; /* Create socket, the key is this sock_dgram * * * (sock = socket (af_inet, SOCK_DGRAM, 0)) = = 1) {perror ("socket"); exit (errno);}
else printf ("Create socket.\n\r");
/* Set the offset address and port information/s_addr.sin_family = af_inet;
if (argv[2]) S_addr.sin_port = htons (atoi (argv[2));
else S_addr.sin_port = htons (7838);
if (argv[1]) s_addr.sin_addr.s_addr = inet_addr (argv[1)); else {printf ("message must have a receiver!")
\ n ");
Exit (0);
* * Send UDP message/* Addr_len = sizeof (S_ADDR);
strcpy (Buff, "hello i ' m here");
Len = sendto (sock, Buff, strlen (buff), 0, (struct sockaddr *) &s_addr, Addr_len);
if (Len < 0) {printf ("\n\rsend error.\n\r"); return 3;}
printf ("Send success.\n\r");
return 0;
}
Compile the program with the following command:
Gcc-wall simple-udpserver.c-o Server
gcc-wall simple-udpclient.c-o Client
Run the program with the following command:
./server 127.0.0.1 7838
./client 127.0.0.1 7838
UDP Mode Broadcast Communication
The source code becomes the following:
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <errno.h> #include <stdlib.h> #include <arpa/inet.h> int main (int argc,
Char **argv) {struct sockaddr_in s_addr; int sock; int addr_len; int len; char buff[128]; int yes; /* Create socket/if (sock = socket (af_inet, SOCK_DGRAM, 0)) = = 1) {perror ("socket"); exit (errno);} else printf ("Create
Socket.\n\r ");
/* Set the means of communication on the broadcast, that this program sent a message, all the hosts on the network can receive * * = 1;
setsockopt (sock, Sol_socket, So_broadcast, &yes, sizeof (yes));
/* The only change is this./* Set the address and port information/* s_addr.sin_family = af_inet;
if (argv[2]) S_addr.sin_port = htons (atoi (argv[2));
else S_addr.sin_port = htons (7838);
if (argv[1]) s_addr.sin_addr.s_addr = inet_addr (argv[1)); else {printf ("message must have a receiver!")
\ n ");
Exit (0);
* * Send UDP message/* Addr_len = sizeof (S_ADDR);
strcpy (Buff, "hello i ' m here"); Len = sendto (sock, Buff, strlen (buff), 0, (struct sockaddr *) &s_addr, Addr_len);
if (Len < 0) {printf ("\n\rsend error.\n\r"); return 3;}
printf ("Send success.\n\r");
return 0;
}
Compile this program with the following command:
Gcc-wall Broadc-udpclient.c-o Client
Run the program with the following command:
./client 192.168.0.255 7838
Messages are sent to all hosts in the 192.168.0 network.
Other hosts if the server is running:
./server own IP address 7838
You will receive a message from the above client.