Linux Network programming (simple time to get the server)

Source: Internet
Author: User
Tags htons

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

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.