I call sendto in my udp socket client side like this:
if((num_w = sendto(cli_udp_sock, buffer, strlen(buffer), 0, (struct sockaddr *) &servaddr, sizeof(servaddr) )) < 0)
but there seems to be no nothing received at the server side and in very strange fashion, num_w, when printed out, gives values like:
-197379208 -1440076936 2054978424
And perror() displays “success”
Client code up till that point:
#include <sys/socket.h> /* socket definitions */ #include <sys/types.h> /* socket types */ #include <arpa/inet.h> /* inet (3) funtions */ #include <unistd.h> /* misc. UNIX functions */ #include <stdlib.h> #include <string.h> #include <stdio.h> #include <errno.h> #include <netdb.h> /* Global constants */ #define MAX_LINE (1000) #define LISTENQ (10) /* Function declarations */ int ParseCmdLine(int argc, char *argv[], char **szPort, char **szAddress, char ** serv_udp_port); /* main() */ int main(int argc, char *argv[]) { // short int cli_port; /* port number */ short int serv_port; struct sockaddr_in servaddr; /* socket address structure */ socklen_t serv_addrlen = sizeof(servaddr); struct sockaddr_in cliaddr; char buffer[MAX_LINE]; /* character buffer */ char *szAddress; /* Holds remote IP address */ char *szPort; /* Holds remote port */ char *serv_udp_port; char *endptr; /* for strtol() */ int cli_udp_sock; /* Get command line arguments */ ParseCmdLine(argc, argv, &szPort, &szAddress, &serv_udp_port); /* Set the remote port */ cli_port = strtol(szPort, &endptr, 0); serv_port = strtol(serv_udp_port, &endptr, 0); if ( *endptr ) { printf("ECHOCLNT: Invalid port supplied.n"); exit(EXIT_FAILURE); } /* Set all bytes in socket address structure to zero, and fill in the relevant data members */ memset(&cliaddr, 0, sizeof(cliaddr)); cliaddr.sin_family = AF_INET; cliaddr.sin_port = htons(cli_port); cliaddr.sin_addr.s_addr = htonl(INADDR_ANY); memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(serv_port); /* Set the remote IP address */ if ( inet_aton(argv[2], &servaddr.sin_addr) <= 0 ) { printf("ECHOCLNT: Invalid remote IP address.n"); exit(EXIT_FAILURE); } int list_s; if ( (list_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { fprintf(stderr, "ECHOCLNT: Error creating listening socket.n"); exit(EXIT_FAILURE); } if (listen(list_s, LISTENQ) < 0) { printf("Failed to called listen.n"); } if( ((cli_udp_sock = socket(AF_INET, SOCK_DGRAM, 0) )< 0)) { printf("UDP socket was not successfully created. Exiting...n"); exit(0); } //if(bind(cli_udp_sock, (struct sockaddr *) &cliaddr, sizeof(cliaddr)) > 0); char command[100]; /* Get command from user */ printf("==============================================n"); do { if(strcmp(command, "sn") == 0) { printf("______________________________________________n"); printf("'s' entered, nEnter the string to requestn"); //prompt for string to enter strcpy(buffer, "CAPn"); //set first set of characteres to CAPn char bucket[100]; //space for entered string fgets(bucket, sizeof bucket, stdin); printf("The string entered is: %s", bucket); strcat(buffer, bucket); /* Send string to echo server, and retrieve response */ int num_w; if((num_w = sendto(cli_udp_sock, buffer, strlen(buffer), 0, (struct sockaddr *) &servaddr, sizeof(servaddr) )) < 0) { perror("error: "); printf("Problem writing data to server, errno: %d", errno); exit(0); } else { perror("error 2: "); printf("num_w: %dn"); }
Thanks so much in advance! (Oh and I’m on Kali linux, virtual machine, Windows 10)
Advertisement
Answer
You aren’t printing num_w
. You’ve left it out of the printf()
statement. You’re just printing whatever random junk is on the stack.
And
perror()
displays “success”
So num_w
was positive. Calling perror()
in that case is pointless.