ccplusplus.com
Learn C, C++ Concepts
Monday, September 19, 2011
stream tcp server c
/************************************************************* * File : stream-tcp-server.c * Author : Saurabh Gupta * Desc : Example inetd daytime server: * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 06:36 19 September 2011 *************************************************************/ #include
#include
#include
#include
#include
#include
#include
/* * This function reports the error and * exits back to the shell: */ static void displayError(const char *on_what) { if ( errno != 0 ) { fputs(strerror(errno),stderr); fputs(": ",stderr); } fputs(on_what,stderr); fputc('\n',stderr); exit(1); } int main(int argc,char **argv) { int z; int n; time_t td; // Current date&time char dtbuf[128]; // Date/Time info /* * Generate a time stamp: */ time(&td); n = (int) strftime(dtbuf,sizeof dtbuf, "%A %b %d %H:%M:%S %Y\n", localtime(&td)); /* * Write result back to the client: */ z = write(1,dtbuf,n); if ( z == -1 ) { displayError("write(2)"); } return 0; } /* * OUTPUT * [sgupta@rhel55x86 socket]$ gcc stream-tcp-server.c -o stream-tcp-server stream-tcp-server.c:50:3: warning: no newline at end of file [sgupta@rhel55x86 socket]$ ls [sgupta@rhel55x86 socket]$ ./stream-tcp-server Monday Sep 19 06:34:27 2011 [sgupta@rhel55x86 socket]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment