ccplusplus.com
Learn C, C++ Concepts
Tuesday, September 20, 2011
I/O on socketpair example in c
/*********************************************************************************** * Program Name : socket_pair_I_O.c * Author : Saurabh Gupta * Discription : Example performing I/O on a Socket Pair: * Source : http://saurabhgupta0527.blogspot.com/2011/07/socket.html * Created : 08:49 PM 10/11/2009 * Modified : PM 10:54 20-09-2011 ************************************************************************************/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> int main(int argc,char **argv) { int z; /* Status return code */ int s[2]; /* Pair of sockets */ char *cp; /* A work pointer */ char buf[80]; /* Work buffer */ /* * create a pair of local sockets: */ z = socketpair (AF_LOCAL,SOCK_STREAM,0,s); if ( z == -1 ) { fprintf(stderr, "%s: socketpair(AF_LOCAL,SOCK_STREAM," "0)\n", strerror(errno)); return 1; /*Failed */ } /* * Write a message to socket s[1]: */ z = write(s[1],cp="Hello?",6); if ( z < 0 ) { fprintf(stderr, "%s: write(%d,\"%s\",%d)\n", strerror(errno),s[1],cp,strlen(cp)); return 2; /* Failed write */ } printf("Wrote message '%s' on s[1]\n",cp); /* * Read from socket s[0]: */ z = read(s[0],buf,sizeof buf); if ( z < 0 ) { fprintf(stderr, "%s: read(%d,buf,%d)\n", strerror(errno),s[0],sizeof buf); return 3; /* Failed read */ } /* * Report received message: */ buf[z] = 0; /* NUL terminate */ printf("Received message '%s' from socket s[0]\n", buf); /* * Send a reply back to s[1] from s[0]: */ z = write(s[0],cp="Go away!",8); if ( z < 0 ) { fprintf(stderr, "%s: write(%d,\"%s\",%d)\n", strerror(errno),s[0],cp,strlen(cp)); return 4; /* Failed write */ } printf("Wrote message '%s' on s[0]\n",cp); /* * Read from socket s[1]: */ z = read(s[1],buf,sizeof buf); if ( z < 0 ) { fprintf(stderr, "%s: read(%d,buf,%d)\n", strerror(errno),s[1],sizeof buf); return 3; /* Failed read */ } /* * Report message received by s[0]: */ buf[z] = 0; /* NUL terminate */ printf("Received message '%s' from socket s[1]\n", buf); /* *Close the sockets: */ close(s[0]); close(s[1]); puts("Done."); return 0; } /* * OUTPUT * [sgupta@rhel55x86 socket]$ gcc socket_pair_I_O.c -o socket_pair_I_O [sgupta@rhel55x86 socket]$ ./socket_pair_I_O Wrote message 'Hello?' on s[1] Received message 'Hello?' from socket s[0] Wrote message 'Go away!' on s[0] Received message 'Go away!' from socket s[1] Done. [sgupta@rhel55x86 socket]$ */
2 comments:
Anonymous
February 20, 2013 at 3:25 AM
Thanks, helpfull!
Reply
Delete
Replies
Reply
Unknown
March 27, 2013 at 7:35 PM
It's helpfull!!
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
Thanks, helpfull!
ReplyDeleteIt's helpfull!!
ReplyDelete