Thread: Sockets and UDP problem

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Sockets and UDP problem

    Hi,
    I'm having problems making a small program that is meant to send out data using UDP on one port and recieve the data on another. The program appears to setup the sockets etc correctly, but it looks like my "send_task" thread is failing, could anyone give me any advice to what I could be doing wrong?

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <pthread.h>
    #include <stdlib.h>
    
    #define SEND_PORT 5004
    #define SEND_INDEX 0
    #define RECIEVE_PORT 5005
    #define RECIEVE_INDEX 1
    #define MSG_SIZE_IN 70
    #define MSG_SIZE_OUT 22
    #define THREAD_SLEEP_TIME 10000
    
    int sockfd[1];
    struct sockaddr_in out_addr, in_addr;
    struct hostent *remote_server;
    
    int outgoing_connection(int port, int pid)
    {
      if ((sockfd[pid] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
         printf("pid%d: ERROR opening socket\n", pid);
         return 1;
      }
      
      bzero((char *) &out_addr, sizeof(out_addr));
      out_addr.sin_family = AF_INET;
      out_addr.sin_addr.s_addr = INADDR_ANY;
      out_addr.sin_port = htons(port);
      
      if (bind(sockfd[pid], (struct sockaddr *)&out_addr, sizeof(out_addr)) < 0) {
        printf("pid%d: ERROR on binding\n", pid);
        return 1;
      }
    
      return 0;
    }
    
    int incoming_connection(int port, int pid)
    {
      struct hostent *server;
      server = remote_server;
    
      if ((sockfd[pid] = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("pid%d: ERROR opening socket\n", pid);
        return 1;
      }
    
      bzero((char *) &in_addr, sizeof(in_addr));
      in_addr.sin_family = AF_INET;
      bcopy((char *) server->h_addr, (char *) &in_addr.sin_addr.s_addr,
             server->h_length);
      in_addr.sin_port = htons(port);
    
      if ((connect(sockfd[pid], &in_addr, sizeof(in_addr))) < 0) {
        printf("pid%d: ERROR on binding\n", pid);
        return 1;
      }
    
      return 0;
    }
    
    void* send_task (void* arg)
    {
      char msg[MSG_SIZE_OUT];
      int sequ_no, pid, mutex, curr_time_s;
      float curr_time_ns;
      struct timespec t;
      pthread_mutex_t t_mutex = PTHREAD_MUTEX_INITIALIZER;
      
      pid = SEND_INDEX;
    
      pthread_mutex_lock(&t_mutex);
      if (clock_gettime(CLOCK_REALTIME, &t)) {
        printf("pid%d: ERROR getting current time\n", pid);
      } else {
        curr_time_ns = t.tv_nsec;
        curr_time_s = t.tv_sec;
            
        sprintf(msg, "%d %d %f", sequ_no, curr_time_s, curr_time_ns);
      
        if (send(sockfd[pid], msg, MSG_SIZE_OUT, 0)) {
          printf("pid%d: ERROR writing to socket\n", pid);
        } else {
          printf("pid%d: %s\n", pid, msg);
          sequ_no++;
        }
      }
    
      usleep(THREAD_SLEEP_TIME);
      pthread_mutex_unlock(&t_mutex);
      pthread_exit(NULL);
      return;
    }
    
    void* recieve_task (void* arg)
    {
      char buffer[MSG_SIZE_IN];
      float lost_packets;
      int socket, pid = RECIEVE_INDEX;
      FILE *fp;
    
      socket = sockfd[pid];
      
      while(1) {
        memset(buffer,0x0,MSG_SIZE_IN-1);
        
        if (recv(sockfd[pid], buffer, MSG_SIZE_IN, 0)) {
          printf("pid%d: ERROR reading from socket\n", pid);
        } else {
          printf("%s\n",buffer);
        
          fp = fopen("sender_output.txt", "w");
      
          lost_packets = atof((char *) strtok(buffer," "));
        
          fprintf(fp, "%f\n", lost_packets);
          fclose(fp);
        }
      }
    
      pthread_exit(NULL);
      return;
    }
    
    int main(int argc, char *argv[])
    {
      pthread_t thread[1];
      int incoming, outgoing;
      
      if (argc < 2) {
          fprintf(stderr,"usage: %s hostname\n", argv[0]);
          return 0;
      }
      
      if((remote_server= gethostbyname(argv[1]))==NULL) {
        printf("Unknown host '%s' \n", argv[1]);
        return 0;
      } else {
        outgoing = SEND_INDEX;
        incoming = RECIEVE_INDEX;
    
        if (incoming_connection(RECIEVE_PORT, incoming)) return 0;
        
        if (pthread_create(&thread[incoming], NULL, recieve_task,
              (void*) incoming)) {
          printf("pid%d: ERROR pthread_create() failed!\n", incoming);
          return 0;
        }
    
        if (outgoing_connection(SEND_PORT, outgoing)) return 0;
        
        if (pthread_create(&thread[outgoing], NULL, send_task, (void*) outgoing)) {
          printf("pid%d: ERROR pthread_create() failed!\n", outgoing);
          return 0;
        }
      }
      
      pthread_exit(NULL);
      return 0;
    }

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    RECIEVE_INDEX should be 0, not 1.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Thanks for the quick response OnionKnight, I'm trying to figure out the logic etc in my head and it's not working right now so I'll need to test that out soon (I haven't got all the header files I need on my home computer and can't be bothered wading through the net trying to find them all - from my experience, some of the headers you can find on the net are pretty flaky so I wouldn't know if it was my code or theirs that is wrong anyways).

    Someone suggested that the "if (recv(sockfd[pid], buffer, MSG_SIZE_IN, 0))" might be wrong, from the example code I found on the net, I think it looks ok though (been a long time since I last did C), what does everyone else think?

    I also wasn't sure about "memset(buffer,0x0,MSG_SIZE_IN-1);" (MSG_SIZE_IN-1??). That was part of a tutorial I was using. By the way, there is a complimentary which should guarentee that the recieved info is well formatted (so the printf won't run riot around memory).

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Hi,

    what i think so is.....Whenever u r using UDP then u must have to use sendto() function instead send() and recvfrom() instead of recv() only. try it and tell its working or not.

    And when u r posting ur code then plz give in written words that what u r expecting from ur code, what kind of arguments u r passing etc. Coz that can help us to trace problems easily.
    S_ccess is waiting for u. Go Ahead, put u there.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    I had the incoming and outgoing connection methods in a muddle (though it's still not right - I'm getting errors writing to the socket). One of my friends succesfully sent packets using UDP and the send() recv() methods because he uses connect(), which apparrently with UDP creates a virtual connection, allowing you to use the shorter send/recv methods. Bit of a fudge I suppose, but he has proven that it does work over a LAN.

    Sorry there isn't any comments in my code... That was me being lazy and using stuff I know too little about to comment on effectively anyways .

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Smile

    Try to get some help from this link...

    http://www.ecst.csuchico.edu/~beej/g..._old/#datagram

    may be it will be useful for u.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with UDP
    By AlexS in forum C# Programming
    Replies: 0
    Last Post: 07-07-2009, 06:21 PM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. udp non-blocking sockets
    By l2u in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-07-2006, 06:57 PM
  4. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  5. Problems with UDP sockets
    By majoub in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-30-2005, 12:25 AM