ccplusplus.com
Learn C, C++ Concepts
Sunday, February 19, 2012
datagram client server
/************************************************************************* * File : udp-datagram-client.c * Author : Saurabh Gupta * Desc : datagram client server * Source : http://www.ccplusplus.com/2011/07/socket.html * Created : 10:40 AM Sunday, February 19, 2012 *************************************************************************/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <time.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> /* * This function reports the error and * exits back to the shell: */ static void displayError(const char *on_what) { fputs(strerror(errno),stderr); fputs(": ",stderr); fputs(on_what,stderr); fputc('\n',stderr); exit(1); } int main(int argc,char **argv) { int z; int x; char *srvr_addr = NULL; struct sockaddr_in adr_srvr; // AF_INET struct sockaddr_in adr; // AF_INET int len_inet; // length int s; // Socket char dgram[512]; // Recv buffer /* * Use a server address from the command * line, if one has been provided. * Otherwise, this program will default * to using the arbitrary address * 127.0.0.23 */ if ( argc >= 2 ) { srvr_addr = argv[1]; // Addr on cmdline: } else { srvr_addr = "127.0.0.23"; //Use default address: } /* * Create a socket address, to use * to contact the server with: */ memset(&adr_srvr,0,sizeof adr_srvr); adr_srvr.sin_family = AF_INET; adr_srvr.sin_port = htons(9090); adr_srvr.sin_addr.s_addr = inet_addr(srvr_addr); if ( adr_srvr.sin_addr.s_addr == INADDR_NONE ) { displayError("bad address."); } len_inet = sizeof adr_srvr; /* * Create a UDP socket to use: */ s = socket(AF_INET,SOCK_DGRAM,0); if ( s == -1 ) { displayError("socket()"); } for (;;) { /* * Prompt user for a date format string: */ fputs("\nEnter format string: ",stdout); if ( !fgets(dgram, sizeof dgram,stdin) ) { break; // EOF } z = strlen(dgram); if ( z > 0 && dgram[-z] == '\n' ) { dgram[z] = 0; // Stomp out newline } /* * Send format string to server: */ z = sendto(s, // Socket to send result dgram, // The datagram result to snd strlen(dgram), // The datagram lngth 0, // Flags: no options (struct sockaddr *)&adr_srvr,// addr len_inet); // Server address length if ( z < 0 ) { displayError("sendto(2)"); } /* * Test if we asked for a server shutdown: */ if (!strcasecmp(dgram,"QUIT") ) { break; // Yes, we quit too } /* * Wait for a response: */ x = sizeof adr; z = recvfrom(s, // Socket dgram, // Receiving buffer sizeof dgram, // Max recv buf size 0, // Flags: no options (struct sockaddr *)&adr, // Addr &x); // Addr len, in & out if ( z < 0 ) { displayError("recvfrom(2)"); } dgram[z] = 0; // null terminate. /* * Report Result: */ printf("Result from %s port %u :\n\t'%s'\n", inet_ntoa(adr.sin_addr), (unsigned)ntohs(adr.sin_port), dgram); } /* * Close the socket and exit: */ close(s); putchar('\n'); return 0; } /* * OUTPUT * [root@rhel6x86 datagram]# ./udp-datagram-client 192.168.1.15 Enter format string: %D Result from 192.168.1.15 port 9090 : '02/19/12 ' Enter format string: %A %D Result from 192.168.1.15 port 9090 : 'Sunday 02/19/12 ' Enter format string: %A Result from 192.168.1.15 port 9090 : 'Sunday ' Enter format string: QUIT Result from 192.168.1.15 port 9090 : 'QUIT ' [root@rhel6x86 datagram]# */
UDP Server code for complete udp server-client application
See Also:
Socket Tutorial and Sample Codes
Advance Socket Tutorial and Sample Code
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment