Thread: problem in select() in a Multiprotocol client

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    problem in select() in a Multiprotocol client

    Hi,

    I am new to socket programming and I am trying to write code for a client that sends and receives messages on both TCP and UDP. I am using select() system call for it. My problem is, select() works fine for TCP but it doesn't go inside the if loop for UDP and hence no messages are being received on UDP. The sendto() seems to be working fine as it returns a positive integer value.
    I am not able to figure out the reason. Here is some of my code. Any help is appreciated.


    fd_set master; // master file descriptor list

    FD_ZERO(&master);

    while(1)
    {
    FD_SET(child_fd,&master); //child_fd is socket descriptor for TCP
    FD_SET(sockudpfd,&master); //sockudpfd is socket descriptor for UDP
    struct timeval t;
    t.tv_sec=15;

    if(select(255, &master, (fd_set *)0, (fd_set *)0,(struct timeval *)0) < 0)
    perror("select error \n");

    if(FD_ISSET(child_fd, &master))
    {

    recv_msg=recv(child_fd, message,500,MSG_WAITALL);

    // some computation goes here...where "sendmsg" is calculated

    cliaddr.sin_port=htons(destination_port);

    udp_send=sendto(sockudpfd, sendmsg, strlen(sendmsg), 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
    }

    else if(FD_ISSET(sockudpfd, &master)) // this loop doesnt work.
    {

    int m=recvfrom(sockudpfd, udpmesg, 500, 0, (struct sockaddr *) &cliaddr, &cli_len);
    }
    }

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Are you sure that sockudpfd is actually getting set by your FD_SET()? Trying running FD_ISSET immediately after to check. You may want to see what exact status select is at when it exits instead of just checking for '< 0'.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your code is written so the sockudpfd socket only gets serviced if the child_fd socket doesn't have anything. Don't you want to change that loop to:
    Code:
    if(FD_ISSET(child_fd, &master))
    {
        // ...
    }
    if(FD_ISSET(sockudpfd, &master))
    {
        // ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A fifo file and select() problem
    By itsdafoetime in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 02:23 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. [C++] FTP client problem (winsock)
    By Niekie in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-19-2003, 09:23 AM
  5. Problem with my client and connect()
    By Thantos in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-02-2003, 05:38 PM