Jeff
Jeff

Reputation: 33

Can a single thread connect multiple sockets to the same ip/port?

If you have a single-threaded program that talks to a server, can that program connect 2 secure client sockets to the same ip/port or would this cause a problem since the clientIp/clientPort/serverIp/serverPort would be the same? I'm not sure how the client port is determined. The reason I want to do this is so that each connection can login with a different username/password with different permissions.

Thanks!

Upvotes: 2

Views: 1325

Answers (2)

rajeshnair
rajeshnair

Reputation: 1673

Jeff, You can connect to same server IP/Port with different client Port. If you need to connect using same client port then you will have to do a connect/send-request/disconnect/connect... sequence.

Upvotes: 0

rlc
rlc

Reputation: 2857

Yes, you can: just create two separate connections.

If you don't specify the client port (i.e. you set it to 0) the system will select one for your randomly.

If you want to be able to react to events on both, you can use select to do that.

Upvotes: 3

Related Questions