Linux socket programming time server

Source: Internet
Author: User

Provides time services for users on the network, that is, the current time when the network user returns the server;

Record the IP address of the network user sending the request (save it to a file );

Write the time service client timeclient, which can send a time service request to the server and return the obtained time to the user.

Development Environment: fedora13, Vim, GCC

Timeserver. c file.

#include<stdio.h>#include<unistd.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<time.h>#include<netdb.h>#include<stdlib.h>#include<string.h>#define PORT 1234#define BACKLOG 5int main(){int listenfd, connectfd;struct sockaddr_in server;struct sockaddr_in client;socklen_t addrlen;time_t t;t=time(NULL);FILE *fd=fopen("/root/saveIP","a");if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("socket() error.");return 0;}int opt = SO_REUSEADDR;setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));bzero(&server, sizeof(server));server.sin_family = AF_INET;server.sin_port = htons(PORT);server.sin_addr.s_addr = htonl(INADDR_ANY);if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1){perror("bind() error.");        return 0;}if(listen(listenfd, BACKLOG) == -1){perror("listen() error.");return 0;}addrlen = sizeof(client);if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen )) == -1)   {    perror("accept() error.");return 0;   }printf("Timeclient's IP is %s.  ",inet_ntoa(client.sin_addr));printf("Time is %s\n",ctime(&t));send(connectfd,(time_t *)&t,sizeof(time_t),0);fprintf(fd,"%s  %s\n",inet_ntoa(client.sin_addr),ctime(&t));fclose(fd);return 1;}

Timeclient. c file

#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<strings.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<netdb.h>#define PORT 1234   /*listen port*/int main ( int argc, char *argv[]){ int sockfd, num;struct hostent *he; struct sockaddr_in server;time_t t; if(argc!=2) {  printf("usage %s<ip address>\n",argv[0]); return 0;} /*get ip address*/ if((he = gethostbyname(argv[1])) == NULL) {  printf("gethostbyname error\n");                return 0; } if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {  printf("socket() error \n");return 0; }  bzero(&server, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *)he->h_addr);   if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1) {  printf("connetc() error\n");return 0; }recv(sockfd,(void *)&t,sizeof(time_t),0);printf("Time is %s\n",ctime(&t));}

Running result:

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.