ccplusplus.com
Learn C, C++ Concepts
Friday, October 21, 2011
daytime client example in c
/************************************************************* * File : daytime-client.c * Author : Saurabh Gupta * Desc : daytime service client * Source : http://saurabhgupta0527.blogspot.com/2011/07/socket.html * Created : AM 09:15 21 October 2011 ************************************************************/ #include <stdio.h> // Basic I/O routines #include <stdlib.h> // exit routine #include <memory.h> // memset, memcpy #include <sys/types.h> // standard system types #include <netinet/in.h> // Internet address structures #include <sys/socket.h> // socket interface functions #include <netdb.h> // host to IP resolution #define HOSTNAMELEN 40 // maximal host name length #define BUFLEN 1024 // maximum response size #define PORT 13 // port of daytime server int main(int argc, char *argv[]) { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXX daytime client XXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); int nSystemCallReturn; // system calls return value storage int nSockDesc; // socket descriptor char cBuffer[BUFLEN+1]; // buffer server answer char* pc; // pointer into the buffer struct sockaddr_in sa; // Internet address struct struct hostent* hen; // host-to-IP translation /* * check there are enough parameters */ if (argc < 2) { fprintf(stderr, "host name missing\n"); exit (1); } /* * Address resolution stage */ hen = gethostbyname(argv[1]); if (!hen) { perror("couldn't resolve host name"); } /* * initiate machine's Internet address structure * first clear out the struct, to avoid garbage */ memset(&sa, 0, sizeof(sa)); /* * Using Internet address family */ sa.sin_family = AF_INET; /* * copy port number in network byte order */ sa.sin_port = htons(PORT); /* * copy IP address into address struct */ memcpy(&sa.sin_addr.s_addr, hen->h_addr_list[0], hen->h_length); /* * allocate a free socket * Internet address family, Stream socket */ nSockDesc = socket(AF_INET, SOCK_STREAM, 0); if (nSockDesc < 0) { perror("socket: allocation failed"); } /* * now connect to the remote server. the system will * use the 4th binding method (see section 3) * note the cast to a struct sockaddr pointer of the * address of variable sa. */ nSystemCallReturn = connect(nSockDesc, (struct sockaddr *)&sa, sizeof(sa)); /* * check there was no error */ if (nSystemCallReturn) { perror("connect"); } /* * now that we are connected, start reading the socket * till read() returns 0, meaning the server closed * the connection. */ pc = cBuffer; while (nSystemCallReturn = read(nSockDesc, pc, BUFLEN - (pc-cBuffer))) { pc += nSystemCallReturn; } /* * close the socket */ close(nSockDesc); /* * pad a null character to the end of the result */ *pc = '\0'; /* * print the result */ printf("\nTime: %s\n", cBuffer); /* * and terminate */ printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * * [sgupta@rhel54x64 socket]$ gcc daytime-client.c -o daytime-client [sgupta@rhel54x64 socket]$ ./daytime-client 172.31.108.200 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX daytime client XXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Time: 21 OCT 2011 12:35:57 IST XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 socket]$ */
See Also:
Socket Concepts and sample codes
Advance Socket Concepts and sample codes
2 comments:
Anonymous
January 26, 2012 at 11:09 AM
thank u !!!
Reply
Delete
Replies
Saurabh Gupta
January 30, 2012 at 9:20 AM
My Pleasure.
Thanks,
Saurabh Gupta
saurabh.gupta@ccplusplus.com
Delete
Replies
Reply
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
thank u !!!
ReplyDeleteMy Pleasure.
DeleteThanks,
Saurabh Gupta
saurabh.gupta@ccplusplus.com