开发者

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:

  1. create the socket
  2. Bind the socket
  3. Start listening
  4. Add file descriptor of the listening socket to the select
  5. 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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜