Reputation: 29
I have a load test application that I want to have start multiple threads and each one of those threads will open up a socket to the same server and communicate with it. Is this possible or must I fork() or run multiple instances of a single threaded app?
[Update from comments:]
The problem I seem to be getting is that the multiple calls to socket() all seem to be returning a value of 0. Therefore, when the threads try to communicate with the server, only one of them succeeds while the rest are waiting for a response and time out.
Upvotes: 0
Views: 1011
Reputation: 595349
Yes, you can create multiple client socket connections to the same server IP/Port, as long as you are not binding those client sockets to the same local IP/Port at the same time. By default, connect()
does an implicit bind()
to a random local port unless bind()
was explicitly called beforehand.
Upvotes: 1
Reputation: 2619
Sure! The only time this would be a problem is if they were all acting as a server and trying to listen on the same port. Sounds like you're using them as clients and in this regard, you can have as many as you want (as long as the OS doesn't run out of file descriptors for your process).
Upvotes: 1