The Client.
The following code contains the implementation for the UDP client application that can send data to the server explained in the previous article.
/*+--------------------------------------------------------------+ | client.c - description | | ------------------- | | begin : 08/01/2010 18.31 | | copyright : (C) 2009 xAppSoftware | | author : Luigi D'Andrea | | email : gg1 ( at ) xappsoftware dot com | | compiling : gcc -o client client.c | | | | Latest version on http://www.xappsoftware.com | +--------------------------------------------------------------+ | udp client-server may be redistributed and modified under | | certain conditions. This software is distributed on an | | "AS IS" basis WITHOUT WARRANTY OF ANY KIND, either express or| | implied. See the file License.txt for details. | +--------------------------------------------------------------+*/ /*+--------------------------------------------------------------+ | SYSTEM INCLUDES | +--------------------------------------------------------------+*/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h>
Blablablabla.
/*+--------------------------------------------------------------+ | SPECIFIC INCLUDES | +--------------------------------------------------------------+*/ #include "clientserver.h"
Finally the main function…..
/*+--------------------------------------------------------------+ | Function name : main | | Parameters : The destination IP address | | The port number | | Description : The simple udp client main | +--------------------------------------------------------------+*/ int main(int argc, char **argv) { struct sockaddr_in si_server; int port, s, i, slen=sizeof(si_server); char buf[BUFLEN]; char srv_addr[16]; memset(srv_addr, 0, 16);
First of all I check for the command line argument. the client shall be launched with at least two parameters, the destination IP address and the port number to which the client will send data, the port number shall be > 1023 because Linux reserves port from 1 to 1023. In addition the client can accept a third argument, if the 3rd argument is present the client will send to the server the string of the 3rd argument (this can be used to send to the server the termination command).
if(argc<3) { fprintf(stderr, "Usage: %s <destination IP address> <port number>\n", argv[0]); exit(EXIT_FAILURE); } port=atoi(argv[2]); printf("port = %d\n", port); if(port<1024) { fprintf(stderr, "Usage: %s <destination IP address> <port number>\n", argv[0]); fprintf(stderr, "\twhere <port number> shall be > 1023\n"); exit(EXIT_FAILURE); }
Now I create a new UDP socket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) { perror("socket"); exit(EXIT_FAILURE); } memcpy(srv_addr, argv[1], strlen(argv[1]));
Now I prepare the address to which I want to send data.
memset((char *) &si_server, 0, sizeof(si_server));
si_server.sin_family = AF_INET; si_server.sin_port = htons(port); if (inet_aton(srv_addr, &si_server.sin_addr)==0) { fprintf(stderr, "inet_aton() failed\n"); exit(EXIT_FAILURE); } /*+---------------------------------------------------------------------+ | If the fourth parameters is present we need to send it instead | | of a sequence of packets. We can use this case when we want to | | terminate the server sending to it the string ".quit." | +---------------------------------------------------------------------+*/
If the 3rd argument is present the client sends it to the server
if(argc==4)
{ sprintf(buf, "%s", argv[3]); if (sendto(s, buf, strlen(argv[3]), 0, (struct sockaddr *)&si_server, slen)==-1) { perror("sendto()"); exit(EXIT_FAILURE); } }
Else the client sends NUMBER_OF_PACKETS packets to the server (NUMBER_OF_PACKETS is defined in clientserver.h)
else
{ for (i=0; i<NUMBER_OF_PACKETS; i++) { printf("Sending packet %d\n", i); sprintf(buf, "This is packet %d\n", i); if (sendto(s, buf, BUFLEN, 0, (struct sockaddr *)&si_server, slen)==-1) { perror("sendto()"); exit(EXIT_FAILURE); } } }
Release the socket and exit
close(s); exit (EXIT_SUCCESS); }
SIMPLE UDP CLIENT/SERVER
Network programming index:
Hi GG1!
Thanks for posting a great series on network programming! I tried to experiment with your code, but it seems that it is not available on the link below. Have you maybe moved the source code to some other location?
http://www.xappsoftware.com/wordpress/2010/02/26/udp-client-source/
Thanks,
Peter
Thanks for your report, I’m working to solve this issue
Ok now the link works, thanks again for your report.