tcpclient - How to connect multiple TCP IP clients to same server port using c++ -


i want connect 2 clients same server port using tcp ip. have use below code before bind:- // reuse binded socket

int reuse=1;      setsockopt(m_isocketid, sol_socket, so_reuseaddr, (char *)&reuse, sizeof(reuse)) 

listen(isocketid, 2); struct sockaddr clientaddr; socklen_t length = sizeof(clientaddr); int firstclientsocket = accept(isocketid, &clientaddr, &length); length = sizeof(clientaddr); int secondclientsocket = accept(isocketid, &clientaddr, &length); 

after code, have 2 client sockets work with. note, 'accept' function blocks until client connects. in general, should use aync methods (e.g. select) handle multiple clients.

so_reuseaddr not intended purposes. tells system listening port can reused multiple instances of server. it's debugging, when app doesn't close socket upon exit. otherwise system might hold port time, refusing bind socket it.

and don't forget error handling on listen , accept calls =)


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -