1 /*timeserv.c A socket-based time of day server access display times2 */3#include <stdio.h>4#include <unistd.h>5#include <sys/types.h>6#include <sys/socket.h>7#include <netinet/inch.h>8#include <netdb.h>9#include <time.h>Ten#include <strings.h> One A #definePortnum 13000//Port - #defineHostlen 256 - #defineOops (msg) {perror (msg); exit (1);} the - intMainintARGC,Char*argv[]) { - structsockaddr_in saddr; - /***************************************************************************** + * strcut sockaddr in socket domain for af_inet domain, the SOCKETADDR structure is defined as: - * struct sockadr_in{ + * unsigned short int sin_family;//refers to the protocol family, can only be af_inet in socket programming. The domain parameter when the socket () is called, which is the value of Af_xxxx A * uint16_t Sin_port; The port number to use at * struct IN_ADDR sin_addr; SIN_ADDR.S_ADDR for IP address - * unsigned char sin_zero[8];//Not used - * } - *****************************************************************************/ - structHostent *HP; - /***************************************************************************** in * Strcut hostent { - * Char *h_name; The canonical name of the host, Eg:www.google.com's canonical name is actually www.I.google.com to * char **h_aliases;//host Alias, Eg:www.google.com is Google's alias, maybe a lot of aliases + * int h_addrtype; Represents the host IP type, IPv4 (af_inet), IPv6 (Af_inet6) - * int h_length; Indicates the host IP length the * Char **h_addr_list; Indicates the host IP address, note that this is stored in network byte order, needs to be printed with the Inet_ntop () function, and H_ADDR is the first address of H_addr_list * } $ *****************************************************************************/Panax Notoginseng CharHostname[hostlen];//Address - intsock_id, SOCK_FD;//Line ID, file desc theFILE *SOCK_FP;//Use socket as stream + Char* CTime ();//convert secs to string Atime_t thetime;//The time we report the /** * Step1:ask kernel for a socket***/ +sock_id = socket (pf_inet, Sock_stream,0);//get a socket - /*sockid = socket (int domain, int type, int protocol) $ *domain Communication domain: pf_inet for Internet sockets; Type: Socket type, sock_stream similar pipeline; Protocol protocol: protocol used, default = 0*/ $ if(sock_id==-1) -Oops"Socket"); - the /** * Step2:bind address to socket, address is host, Port * **/ -Bzero ((void*) &saddr,sizeof(SADDR));WuyiGetHostName (hostname, hostlen);//Get host name theHp=gethostbyname (hostname);//Get host info get info about host - WuBcopy ((void*) Hp->h_addr, (void*) &saddr.sin_addr, hp->h_length);//copy IP to saddr -Saddr.sin_port=htons (Portnum);//fill port fill in socket port About /*where the htons function is to perform a htons conversion, changing the big end to a small terminal structure*/ $Saddr.sin_family=af_inet;//fill in the Protocol family - - if(Bind (sock_id, (structsockaddr*) &saddr,sizeof(SADDR)) !=0)//bind IP Address port number to socket -Oops"Bind"); A + /** * Step3:allow incoming calls with qsize=1 on socket***/ the if(Listen (sock_id,1)!=0) - /**************************************************************************** $ The Listen request kernel allows the specified socket to receive access calls, listen for connections on the socket, not all socket types can receive access calls, but the Sock_stream type is possible. The second parameter indicates the length of the receive queue the ****************************************************************************/ theOops"Listen"); the the /* - * Main loop:accept (), write (), close () in */ the while(1) { theSOCK_FD = Accept (sock_id, NULL, NULL);//wait for call Aboutprintf"wow! got a call!\n"); the if(SOCK_FD = =-1) theOops"Accept"); the +SOCK_FP = Fdopen (SOCK_FD,"W");//we ' ll write to the - if(SOCK_FP = = NULL)//socket as a stream theOops"Fdopen");BayiThetime =Time (NULL); the thefprintf (SOCK_FP,"The time here is ..."); -fprintf (SOCK_FP,"%s", CTime (&thetime)); - fclose (SOCK_FP); the } the return 0; the}
To run an action method:
1.GCC Timeserv.c-o TimeServ
2. The ./timeserv& start service ends with a "&" sign, so the shell runs it but does not call the wait call.
3. You can connect to this server with Telnet:
Telnet ' hostname ' 13000 (hostname can be viewed with echo $HOSTNAME)
Linux_socket some basic functions and structures