2013年9月13日 星期五

Implement Server and multiple clients with select()

Refer to:
http://beej-zhtw.netdpi.net/07-advanced-technology/7-2-select
http://blog.csdn.net/zgrjkflmkyc/article/details/48055319

server source:
http://www.mediafire.com/file/dzfca3t7taccag8/bandwidth-server-many-up.c
client source:
http://www.mediafire.com/file/9m7m61rs790k7se/bandwidth-client.c

There are 3 clients connect to the server.
We want all clients do not exit quickly. 
In client add code usleep(60000000);

select can help you monitor many clients.
server side use -> select(fdmax+1, &rdset, NULL, NULL, NULL)

fdmax+1 = max file_descriptor + 1 = max socket number + 1

step 1:
client No.4 connect to server then suspend, server accept socket number is No.4
http://www.mediafire.com/view/d3eatf7ohm4y33x/modbus00.png#

step 2:
another two clients connect to server, server accept socket numbers are No.5 , No.6
http://www.mediafire.com/view/4go53l336goc2qy/modbus01.png
and send some data to server.

step 3:
resume client No.4
http://www.mediafire.com/view/0lq1g6h0uq15wrc/modbus02.png#

step 4:
Server get client No.4 data
http://www.mediafire.com/view/yzxd7r2y9kcd3rk/modbus03.png#

It's prove  select(max file_descriptor + 1,...) will help you
accept new client and get client data

full source:
http://www.mediafire.com/file/axu2f4m9374cq9z/modapp.tar.gz 

沒有留言:

張貼留言