Socket select() method troubles

This is a discussion on Socket select() method troubles within the C++ Programming forums, part of the General Programming Boards category; So, I have a small program that I am trying to get up and running. I must tell you all ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Socket select() method troubles

    So, I have a small program that I am trying to get up and running. I must tell you all that I've not programmed in C++ for a while. So, please be gentle. Attached is code that I've put in place for the client-side of my program. All I want this code to do is connect to the socket specified and deliver a bit string every 5 seconds. And if it receives a message from the server; to output a message (just to indicate that it is being executed). However, this is not working ....

    ================================================== =
    Code:
    #include <sys/socket.h>       
    #include <sys/types.h>        
    #include <arpa/inet.h>        
    #include <unistd.h>           
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include "helper.h"
    using namespace std;
    
    #define MAXDATASIZE 100
    
    int main(int argc, char *argv[]) {
        int sockfd, max, num_bytes, rv;
            short int port;
            char* endptr;
            struct sockaddr_in servaddr;
            char buf[MAXDATASIZE];
            struct timeval tv;
                fd_set readfds;
    
    
                tv.tv_sec = 5;
                tv.tv_usec = 0;
    
    
            port = strtol(argv[1], &endptr, 0);
            sockfd = socket(AF_INET, SOCK_STREAM, 0);
    
            bzero(&servaddr, sizeof(servaddr));
            servaddr.sin_family = AF_INET;
            servaddr.sin_port = htons(port);
            inet_pton(AF_INET, argv[2], &servaddr.sin_addr);
    
            connect(sockfd, (sockaddr*) &servaddr, sizeof(servaddr));
    
            cout << "Connected..." << endl;
    
            FD_ZERO(&readfds);
            FD_SET(sockfd, &readfds);
    
            // Set the max fd
            max = sockfd;
    
            do {
                    rv = select(max+1, &readfds, NULL, NULL, &tv);
    
                    printf("After select(): %d\n", rv);
    
    
            if (rv == -1) {
                perror("select");
            }
            else if (rv == 0) {
                // We timed out.  That means no input .. 
                                        // Generate a BitString packet
                            BitstringPacket data_packet =                   generate_bitstring_packet();
            
                            // Send the BitString Packet to the server
                            if (send(sockfd, &data_packet, sizeof(data_packet), 0) == -1) {
                                    perror("recv");
                                    exit(1);
                            }
          }
          else if ( FD_ISSET(sockfd, &readfds) ) {        
              printf("Received something\n");
          }
          else {
              printf("What is going on?");
          }
    
        } while(1);
    
        close(sockfd);
        exit(0);
    }
    ================================================== =

    It has no problems sending the bitstrings, but it does not ever receive a message from the server, which it should because I've validated the message being sent. I am sure that I am doing something _stupid_ here. Please let me know, gently ;-)

  2. #2
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Quote Originally Posted by newbie@C View Post
    Code:
            connect(sockfd, (sockaddr*) &servaddr, sizeof(servaddr));
    
            cout << "Connected..." << endl;
    
            //Should be
            if (connect(sockfd, (sockaddr*) &servaddr, sizeof(servaddr)) == SOCKET_ERROR) {
                close(sockfd);
                cout << "Connection failed..." << endl;
                return; // or exit(0);
            }
    On line 62, perror("recv"); should be "send"

    See if your getting a connection error, if not we can figure out what is going wrong
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Nor: I made those changes you suggested and I am receiving a connection with no error.

    I believe the problem is with the following portion of the code

    Code:
    do {
    		FD_SET(sockfd, &readfds);
    		rv = select(max+1, &readfds, NULL, NULL, &tv);
    		
    		printf("After select(): %d\n", rv);
    
    
    		if (rv == -1) {
    			perror("select");
    		}
    		else if (rv == 0) {
    			// Generate a BitString packet
    			BitstringPacket data_packet = generate_bitstring_packet();
    		
    			// Send the BitString Packet to the echo server
    			if (send(sockfd, &data_packet, sizeof(data_packet), 0) == -1) {
    				perror("recv");
    				exit(1);
    			}
    		}
    		else if ( FD_ISSET(sockfd, &readfds) ) {		
    			printf("Received Recipe\n");
    		}
    		else {
    			printf("What is going on?");
    		}
    
    
    	} while(1);
    I successfully establish a connection, send data to the server, and receive the response from the server; However, once I receive the response I keep outputting "Received Recipe" continuously. I would like the client to send a message every 5 seconds and if it receives input output "Received .." just to let me know it is working. Then, it would continue sending data until it received another response.

  4. #4
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    you need to read the data off the socket.
    select is letting you know there is data to be read. it will only clear out once you have read the recv() or recvfrom() the socket
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    NICE ! Thanks so much :-) .. I knew it was a _stupid_ error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket() and select()
    By India in forum C Programming
    Replies: 1
    Last Post: 01-19-2011, 01:41 AM
  2. Replies: 2
    Last Post: 04-21-2009, 11:38 AM
  3. Socket Select problem
    By saipkjai in forum Networking/Device Communication
    Replies: 4
    Last Post: 02-08-2008, 09:57 AM
  4. Internet Socket - Read/Write sync with Select()
    By INFERNO2K in forum Networking/Device Communication
    Replies: 8
    Last Post: 07-19-2007, 04:28 PM
  5. Socket Select() Delimiter
    By Lee A O in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2002, 06:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21