1. A simple server time acquisition program
The server and client use UDP communication to write a simple time acquisition application.
The process is generally straightened out, the first is the server-side writing, using the iterative way, no concurrency
First create a socket and then bind the server, after binding you can create a loop to receive and send
Information to reach and communicate with the client.
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<sys/socket.h>#include<sys/types.h>#include<unistd.h>#include<netinet/inch.h>intMainintargcChar**argv) {printf ("server open \ n"); /*Creating Sockets*/ intFD = socket (Af_inet,sock_dgram,0); if(-1==FD) {Perror ("Socket"); Exit (-1); } /*Prepare correspondence Address*/ structsockaddr_in addr; Addr.sin_family=af_inet; Addr.sin_port= Htons (2222); Addr.sin_addr.s_addr= Inet_addr ("172.16.1.21"); /*prevent ports from being occupied*/ intReuse =1; SetSockOpt (FD,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)); /*Binding network ports and IP addresses*/ if(Bind (FD, (structsockaddr*) &addr,sizeof(addr)) == -1) {perror ("Bind"); Exit (-1); } while(1) { /*sending and receiving messages*/ Charbuf[ -] = {}; structSockaddr_in from; /*Create a current time*/time_t Now= Time (0); structtm* time = localtime (&Now ); socklen_t f_size=sizeof( from); Recvfrom (Fd,buf,sizeof(BUF),0,(structsockaddr*) & from,&f_size); printf ("%s\n", BUF); memset (BUF,0,sizeof(BUF)); Charstr[ -] = {}; sprintf (str,"%04d-%02d-%02d%02d:%02d:%02d", time->tm_year+1900, time->tm_mon+1,time->tm_mday,time->Tm_hour, time->tm_min,time->tm_sec); strcpy (BUF,STR); SendTo (Fd,buf,sizeof(BUF),0,(structsockaddr*) & from,sizeof( from)); memset (BUF,0, strlen (BUF)); memset (str,0, strlen (str)); }}
The client was written because it was UDP communication and did not use the Connect connection. The SendTo and Recvfrom functions can save the sent and received socket addresses
So the client simply creates an iterative loop, and each time a server is entered, it can be obtained.
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<sys/socket.h>#include<sys/types.h>#include<unistd.h>#include<netinet/inch.h>#include<arpa/inet.h>intMainvoid){ /*Prepare sockets*/ intFD = socket (Af_inet,sock_dgram,0); if(-1==FD) {Perror ("Socket"); Exit (-1); } /*Prepare correspondence Address*/ structsockaddr_in addr; Addr.sin_family=af_inet; Addr.sin_port= Htons (2222); Addr.sin_addr.s_addr= Inet_addr ("172.16.1.21"); /*when connecting the server with SendTo and UDP communication can not connect, direct hair*/ while(1) { Charbuf[ -] = {}; structSockaddr_in from; socklen_t f_size=sizeof( from); scanf ("%s", BUF); if(-1= = SendTo (Fd,buf,strlen (BUF),0,(structsockaddr*) &addr,f_size)) {Perror ("SendTo"); Exit (-1); } memset (BUF,0, strlen (BUF)); if(-1= = Recvfrom (Fd,buf,sizeof(BUF),0,(structsockaddr*) & from,&f_size)) {Perror ("Recvfrom"); Exit (-1); } printf ("%s\n", BUF); memset (BUF,0, strlen (BUF)); } close (FD); return 0;}