Pavel
Pavel

Reputation: 138

What is wrong in sendto() arguments here?

This is the server code portion of a UDP client-server program. Inside the while loop I called sendto() to write to the socket. But when I execute the program it says - 'Invalid Argument'. What is wrong with the sendto() arguments here ?

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}


int main(int argc, char**argv)
{
   int sockfd,n,newsockfd;
   struct sockaddr_in servaddr,cliaddr;
   socklen_t len;
   char mesg[1000];

   sockfd=socket(AF_INET,SOCK_DGRAM,0);
   //printf("sockfd: %d\n",sockfd);

   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servaddr.sin_port=htons(32000);

   if(bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
   {
        printf("Problem in bind function\n");
   }

   len = sizeof(cliaddr);
   while(1)
     {
         memset(mesg,0,sizeof(mesg));
         gets(mesg);
         if (sendto(sockfd,mesg,100,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr) < 0))
         {
            error("Problem writing to socket");
         }

     }
}

PS: Changed the code from the previous one. Now getting this error.

Upvotes: 0

Views: 520

Answers (1)

nevets
nevets

Reputation: 4828

You got bracket position wrong.

This line:

if (sendto(sockfd,mesg,100,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr) < 0))

should be this instead:

if ( sendto(sockfd,mesg,100,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr)) < 0 )

What's more, since you have defined len = sizeof(cliaddr) outside your while loop and it doesn't get changed inside the loop, you can use it in your sendto command instead of sizeof(cliaddr):

if ( sendto(sockfd,mesg,100,0,(struct sockaddr *)&cliaddr, len) < 0 )

Edit:

After a more careful look, your cliaddr should be replaced with servaddr. The client should send the message to the server, and the fifth parameter in sendto command specifies the server address.

if (sendto(sockfd,mesg,strlen(mesg),0,(struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)

And the most important part you missed out is setting host address, i.e. server IP. Try the following code (it's based on your code, but with essential lines added/ modified):

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
# include <string.h>

// change the host address to the appropriate one, this one is a loopback address
char host[10] = "127.0.0.1";

void error(const char *msg)
{
    perror(msg);
    return;
}


int main(int argc, char**argv)
{
   int sockfd,n,newsockfd;
   struct sockaddr_in servaddr,cliaddr;
   struct hostent *sh;
   struct in_addr **addrs;
   socklen_t len;
   char mesg[1000];

   if ((sh=gethostbyname(host))==NULL) {             //get host's information
        printf("error when gethostbyname");
    }

   sockfd=socket(AF_INET,SOCK_DGRAM,0);
   printf("sockfd: %d\n",sockfd);

   addrs = (struct in_addr **)sh->h_addr_list;

   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servaddr.sin_port=htons(32000);

   // setting address information
   memcpy(&(servaddr.sin_addr.s_addr), *addrs, sizeof(struct in_addr));
   bzero(&(servaddr.sin_zero), 8);

   if(bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
   {
        printf("Problem in bind function\n");
   }

   len = sizeof(servaddr);
   while(1)
     {
         memset(mesg,0,sizeof(mesg));
         gets(mesg);
         if (sendto(sockfd,mesg,strlen(mesg),0,(struct sockaddr *)&servaddr,len) < 0)
         {
            error("Problem writing to socket");
         }
     }
}

Upvotes: 2

Related Questions