fred basset
fred basset

Reputation: 10082

Why is this UDP client/server code not working?

I'm experimenting with trying to send a single UDP message from one machine to another. The client is an embedded Linux system that's connected to the internet via a modem. No firewall is in place. The server is a public VPS server I have. Despite calling the client a number of times the server never receives anything. Can anyone see what could be going wrong?

client:

int main(int argc, char**argv)
{
   int sockfd,n;
   struct sockaddr_in servaddr,cliaddr;
   char sendline[1000];
   char recvline[1000];

   if (argc != 2)
   {
      printf("usage:  udpcli <IP address>\n");
      exit(1);
   }

   sockfd=socket(AF_INET,SOCK_DGRAM,0);

   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=inet_addr(argv[1]);
   servaddr.sin_port=htons(32000);

      sendto(sockfd,"abcd", 4,0,
             (struct sockaddr *)&servaddr,sizeof(servaddr));
}           

Server:

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

   sockfd=socket(AF_INET,SOCK_DGRAM,0);

   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servaddr.sin_port=htons(32000);
   bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

   for (;;)
   {
      len = sizeof(cliaddr);
      printf("calling recvfrom\n");
      n = recvfrom(sockfd,mesg,4,0,(struct sockaddr *)&cliaddr,&len);
      printf("-------------------------------------------------------\n");
      mesg[n] = 0;
      printf("Received the following:\n");
      printf("%s",mesg);
      printf("-------------------------------------------------------\n");
   }
}

Upvotes: 0

Views: 109

Answers (1)

fred basset
fred basset

Reputation: 10082

Problem looks like it was UDP traffic being blocked on my VPS server. When I re-ran the server on the embedded system and the client on my Linux PC it worked fine.

Upvotes: 1

Related Questions