Getting started with sockets-Get server time instances

Source: Internet
Author: User
Tags connect socket htons

Daytimetcpsrv.c

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <sys/ types.h> #include <sys/socket.h> #include <netinet/in.h> #include <time.h>int main (int Argc,char *    *ARGV) {int listenfd,connfd;//listen socket, connect socket struct sockaddr_in servaddr;    Char buff[1024];        time_t ticks;        Create a listener Socket Listenfd=socket (af_inet,sock_stream,0); Fill in the Internet socket address//bzero is better remembered than Memset, with only two parameters. Berkeley function and ANSI C standard function bzero (&servaddr,sizeof (SERVADDR));//empty structure recommended memset function, bzero function is obsolete, slow servaddr.sin_family =af_inet;//protocol//htonl htons byte sort function servaddr.sin_addr.s_addr=htonl (inaddr_any);//Connect server in any host network interface Servaddr.sin_p    Ort=htons (10000);//Bind port//bind socket Bind (LISTENFD, (struct sockaddr*) &servaddr,sizeof (SERVADDR));    The Listen function converts the socket into a listener socket Listen (listenfd,10);    Wait to connect printf ("Waiting connection!\n");    for (;;) {//block, put into sleep, wait for a client connection to arrive and be accepted by the kernel. TCP uses three handshake to establish the connection, accept return when handshake is complete, only one connected descriptionCharacter Connfd=accept (LISTENFD, (struct sockaddr *) null,null);        Ticks=time (NULL); snprintf (Buff), "%.24s\r\n", CTime (&ticks)),//ctime converts an integer value into an intuitive, readable time format//writes to the buff buffer, does not mean that it has been sent to the other party,        After the buffer has finished processing the data, the buffer content write (Connfd,buff,strlen (buff)) begins to be transferred;    Close (CONNFD); }}//Supplemental Knowledge://------------------------//byte manipulation function <strings.h>//berkeley function//bzero bcopy bcmp---above only lists function name//ansi C function/ /*memset *memcpy memcmp//------------------------

Daytimetcpcli.c

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <sys/    types.h> #include <sys/socket.h> #include <netinet/in.h>int main (int argc,char **argv) {int sockfd,n;    Char recvline[1024];    struct sockaddr_in servaddr; if (argc!=2) {printf ("The parameter is wrong!")    \ n "); return-1;} if ((Sockfd=socket (af_inet,sock_stream,0)) <0) {printf ("Socket creation failed!    \ n "); return-1;}    Specify the IP address and port bzero of the server (&servaddr,sizeof (SERVADDR)); servaddr.sin_family=af_inet;//Protocol Servaddr.sin_port =htons (10000);//port//inet_pton Address translation (ASCII string vs. network byte-order binary conversion Network        ->n value if (Inet_pton (af_inet,argv[1],&servaddr.sin_addr) <=0)//IP address has parameters to get argv[1],argv[0]-> current program        {printf ("Inet_pton error for"); return-1;} Connect to server if (connect (sockfd, struct sockaddr*) &servaddr,sizeof (servaddr)) {printf ("Connect error"); re    turn-1;} Read data while ((N=read (sockfd,recvline,1024)) >0) {Recvline[n]= 0;    if (fputs (recvline,stdout) ==eof)//outputs data from Recvline to standard output {printf ("fputs error"); return-1;}    } if (n<0) printf ("read error");        Exit (0); }

  
Output Result:
Server-side:
[Email protected]:~/public/c$./daytimetcpsrv.out
Waiting connection!

Client side:
[Email protected]:~/public/c$./daytimetcpcli.out 127.0.0.1
Sat June 4 09:28:33 2016

Getting started with sockets-Get server time instances

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.