Aboogie
Aboogie

Reputation: 460

How to listen to data packets using UDP in C?

I wrote up this udp receive C code to listen to data packets being streamed and it seems like the code is not entering the while loop: The output of the code is as following and then it stops there with no error although the port and ip are both defined correctly and send data works:

Initialising Winsock...Initialised. This is the socket that was created: 128 Socket created. connects to the socket ; it then stays here and doesnt go into the loop..

Code :

#include<stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <winsock2.h>
#include <fcntl.h>

#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "wsock32.lib")

  int main(int argc , char *argv[])
  {
      printf("started \n");
      WSADATA wsa;
      SOCKET s;
      struct sockaddr_in server, client_addr;
      int recv_size =8192;// 2000000;
      char ser[recv_size] ;
      int count = 0;
      int addr_len;
      int bytes;
      char *message , server_reply[recv_size];

      printf("\nInitialising Winsock...");
      if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
      { 
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1; 
      }

      printf("Initialised.\n");

      if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
      { printf("Could not create socket : %d" , WSAGetLastError());
      }else{ printf("This is the socket that was created:  %d \n", s); }

      printf("Socket created.\n");

      server.sin_addr.s_addr = inet_addr("192.168.10.103");
      server.sin_family = AF_INET;
      server.sin_port = htonl(25000);
      addr_len = sizeof(struct sockaddr);

      if (connect(s, (struct sockaddr *)&server, sizeof(server))<0)
      { puts("connect error");return 1 ;
      }else {puts("connects to the socket"); }
      while(1) {
//        if ((recv_size = recv(s, server_reply, recv_size, 0)) == SOCKET_ERROR) {
//            puts("failed at receive");
//        }
        puts("receiving from microzed");
        bytes = recvfrom(s,ser,recv_size,0,(struct sockaddr *)&client_addr,&addr_len);
        //ser[bytes] = '\0';
        write(1,ser,bytes);

//        printf("count: %d \n ",count = count+ 1);
//        printf("data received: %d \n",ser);

      }
  puts("works \n");

 // server_reply[recv_size] = '\0';
 // printf("This is what the message reply is : %d, \n ",server_reply);

      printf("finished \n");
      return 0;
  }

Upvotes: 1

Views: 379

Answers (1)

NadavL
NadavL

Reputation: 400

I'm going to assume that it's some kind of a 'race condition' between puts() and the recv(), since nothing else shows up, it's obvious that the recvfrom() is blocking, otherwise, you'd see the rest of the output (i.e "work \n").

a easy way to test that would be setting the mode of the socket to NON_BLOCKING.

I've tried running the code with MS Visual 2015 and it runs without issues. Output:

started

Initialising Winsock...Initialised. This is the socket that was created: 200 Socket created. connects to the socket receiving from microzed

Upvotes: 1

Related Questions