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 ;-)