Dgrecv.c
1 /****************************************************************2 * Dgrecv.c-datagram receiver3 * USAGE:DGRECV Portnum4 * Action:listens at the specfied port and reports messages5 */6#include <stdio.h>7#include <stdlib.h>8#include <sys/types.h>9#include <sys/socket.h>Ten#include <netinet/inch.h> One#include <netdb.h> A#include <string.h> -#include <arpa/inet.h> -#include <unistd.h> the - #defineOops (m,x) {perror (M); exit (x);} - - intMake_dgram_server_socket (int); + intGet_internet_address (Char* ,int,int*,structSOCKADDR_IN *); - voidSay_who_called (structsockaddr_in* ); + A intMainintargcChar*argv[]) { at intPort; - intsock; - CharBuf[bufsiz]; - size_t Msglen; - structsockaddr_in saddr; - socklen_t Saddrlen; in - if(argc==1|| (Port=atoi (argv[1])) <=0){ tofprintf (stderr,"usage:d grecv portnumber\n"); +Exit1); - } the * /*get a socket and assign it a port number*/ $ if(Sock=make_dgram_server_socket (port)) ==-1)Panax NotoginsengOops"cannot make server",2); - the /*receive messages on that socket*/ +saddrlen=sizeof(SADDR); A while(Msglen=recvfrom (sock, buf, Bufsiz,0, &saddr, &saddrlen)) >0) { thebuf[msglen]=' /'; +printf"dgrecv:got a message:%s\n", buf); -Say_who_called (&saddr); $ } $ return 0; - } - the //return the sock_id - intMake_dgram_server_socket (intPort) {Wuyi structsockaddr_in saddr; the structHostent *HP; - CharHostname[bufsiz]; Wu intsock; - AboutSock=socket (Pf_inet, Sock_dgram,0); $ if(sock==-1) - return-1; - gethostname (hostname, bufsiz); -hp=gethostbyname (hostname); ABzero ((void*) &saddr,sizeof(SADDR)); +Bcopy (Hp->h_addr, (structsockaddr*) &saddr.sin_addr, hp->h_length); thesaddr.sin_family=af_inet; -saddr.sin_port=htons (port); $ if(Bind (sock, (structsockaddr*) &saddr,sizeof(SADDR))! =0) the return-1; the returnsock; the } the - intGet_internet_address (Char* Host,intLenint*PORTP,structsockaddr_in*ADDRP) { in /* the * Extracts host and port from an Internet socket address the * *addrp-> (host, Port) About */ thestrncpy (Host, Inet_ntoa (addrp->sin_addr), Len); the*portp=ntohs (addrp->sin_port); the return 0; + } - the voidSay_who_called (structSOCKADDR_IN *ADDRP) {Bayi CharHost[bufsiz]; the intPort; the -Get_internet_address (Host, Bufsiz, &port, ADDRP); -printf"From :%s:%d\n", host, port); the}
Dgsend.c
1 /***********************************************************************2 * Dgsend.c-datagram Sender3 * usage:dgsend hostname portnumber "message"4 * Action:sends message to Hostname:portnumber5 */6 7#include <stdio.h>8#include <stdlib.h>9#include <sys/types.h>Ten#include <sys/socket.h> One#include <netinet/inch.h> A#include <netdb.h> - - #defineOops (m, x) {perror (M); exit (x);} the - intMainintargcChar*argv[]) { - Char*message; - intCli_sock; + structsockaddr_in saddr; - structHostent *HP; + A if(argc!=4){ atprintf"Please cin hostname portnumber and message./n"); -Exit (-1); - } -Cli_sock=socket (Af_inet, Sock_dgram,0); - if(cli_sock==-1) -Oops"cannot make socket",2); in -message=argv[3]; to + /*Make Internet address*/ -Bzero ((void*) &saddr,sizeof(SADDR)); theHp=gethostbyname (argv[1]); *Bcopy (Hp->h_addr, (structsockaddr*) &saddr.sin_addr, hp->h_length); $saddr.sin_family=af_inet;Panax NotoginsengSaddr.sin_port=htons (Atoi (argv[2])); - the if(SendTo (cli_sock, message, strlen (message),0, &SADDR,sizeof(SADDR)) ==-1) +Oops"SendTo failed",3); A return 0; the}
Among them, note
In order to make the network program portable, so that the same C code in the big and small end of the computer can be compiled after the normal operation, you can tune the following library functions to do network byte sequence and host byte sequence conversion.
#include <arpa/inet.h>
uint32_t
uint16_t
uint32_t
uint16_t
Htonl (uint32_t
Htons (uint16_t
Ntohl (uint32_t
Ntohs (uint16_t
Hostlong);
Hostshort);
Netlong);
Netshort);
These function names are well remembered, h means that host,n means that network,l represents a 32-bit long integer, and s represents a 16-bit short integer. Cases
For example, htonl indicates that a 32-bit long integer is converted from the host byte order to a network byte order, such as converting an IP address to ready to be sent. If
The host is the small-endian byte-order, these functions convert the parameters to the corresponding size end and then return, if the host is a large byte-order, these
The function does not convert and returns the parameter intact.
Client of Linux_c socket datagram, SERVER.C