TCP Chat Client

I am writing a TCP chat program that uses clients and a server. I have got the server, however I am having a bit of trouble with the client. I am trying to use the select() function to check if any data is waiting to be recv'd from the server, but I get an error. My code is C, but I thought I'd still ask here despite this being a C++ forum. Here is a basic outline of my code:

1
2
3
4
5
6
7
8
9
10
/* variable declarations and basic setting up */

FD_ZERO(&read_set);

ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);

FD_SET(ConnectSocket, &read_set);

select(0, &read_set, NULL, NULL, &timeout); // this always causes an error at runtime 


I've missed out various bits and all the error checking for the sake of clarity.

I can send and recv using ConnectSocket, but when I provide read_set as an argument to select() I get error code 10022.

Any help would be appreciated.
Your first parameter to select() is not correct. It should be the highest fd set in read_set plus 1.
In your case, it should be ConnectSocket + 1.

It worked. Thanks.

Although, on my server I have used 0 as my first argument to all my calls to select() and it works every time, and according to MSDN ( http://msdn.microsoft.com/en-us/library/ms740141(VS.85).aspx ) the first argument is ignored. Any idea why this is?
Last edited on
I'm not a Windows programmer; I speak only to the POSIX standard and what it mandates.
I'm not a Windows programmer; I speak only to the POSIX standard and what it mandates.

Lol.
Topic archived. No new replies allowed.