Click to See Complete Forum and Search --> : god...the select() statment...
Teln3t
May 18th, 2008, 01:10 PM
This select() statment is killing me! i've been looking for a user-friendly tutorial on select()(beejs doesnt even compile for me on windows) for the past 2 days. I've been experimenting constantly with my client/server chat app.
My ultimate goal is a multi-user chat server(and im well aware that I need to use select() to accept and manage multiple connections).
The Server Currently:
Accepts one connection
Reads data from that connection(ASCII Text...duh)
The Client Currently:
Connects to the server(duh)
Sends data which the server reads.
However, I tested running two clients, and noticed that the client connected just fine, but the server was only reading data from the first client connection, so I knew I needed to learn Select().
please help guys, i'm in need of desperate help here.
Richard.J
May 18th, 2008, 01:32 PM
you've got 2 possibilities:
- create a new thread for each new connection created with accept. Pass the new socket to that thread and let it handle the communication. The thread exits when the connection is closed.
- use one thread and select(). At startup, put the listening socket to the read fd set (second parameter to select()). Once you have a new connection, add that to the read fd set as well. When select returns, you'll have to check which socket is signalled and then action on that. More than one socket may be set when select() returns. Any specific questions on select?
HTH
Richard
Teln3t
May 18th, 2008, 01:50 PM
Thanks Richard, right now I have everything working up until the Select() statment is called.
I have the listen socket set to non-blocking, then I accept() a connection, then I call the select() statment. Once the select() statement is called, it detects that my client is connected because it continues on pass that, however, it doesnt read any of the data my client is sending.
Heres my code:
Server:
http://www.e-imagesite.com/UploadedFiles/Server5284540.txt
Client(I made this just to detect basic connections and data, no real significance.):
http://www.e-imagesite.com/UploadedFiles/Client1141642.txt
Screenshot of whats happening for reference(sorry, large image, I use duel-monitors):
http://www.e-imagesite.com/UploadedFiles/Problem6796260.jpg
Richard.J
May 18th, 2008, 04:09 PM
that's not quite what you should do. I did not look at the attachments, but:
- create your server's socket using socket() and bind(), listen()
- then add that socket to the read fd set and call select()
- when a connection request comes in, select will return
- call accept
- add the listen socket and the new socket to your read fd set
- call select()
- now, when select returns, the new socket may be signalled. Check each socket you put in the read fd set if it is set via FD_ISSET. If it is set, and it's not the listening socket, call recv() and process the message. If it is the listening socket, call accept() to accept a new incoming connection request.
HTH,
Richard
codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.