Help with Select?
So i'm trying to make a server that listens on multiple ports. I'm having trouble getting my head around select.
Could someone give me a little bit of pseudo code around the order I do things and why.
I get that i bind two 开发者_运维技巧separate sockets to different ports. Sure. But then can I just call listen on both ports?
On my client(s), do i just call connect, and listen will notice the connection attempt? How do i choose to accept it?
Sorry for the novice questions. I've tried beejs guide and a few others, but they don't really cover multiple ports very well (they all seem to use the same example).
Thanks!
After calling bind
and listen
on the server, the fd for the socket is just another fd that you can use with the select
call. When select
returns and indicates data on that fd, you can call accept
on the fd to begin receiving data.
Edit: Also, the fd you receive when you accept
the connection is another fd that can be passed to the select
call.
Client Side: you need only to call the connect.
Server side the steps are more or less the following:
- create the socket
- Bind the socket
- Start listening
- Add file descriptor of the listening socket to the select
- When the select return on the listening socket then call the accept function upon it. It will return another file descriptor(remember to add this new file descriptor to the list of the FD for which the select has to return through FD_SET)
精彩评论