简体   繁体   中英

Multiple TCP connections in C

Can a process open/maintain 2 TCP connections in parallel? [for sending and receiving]. I tried the following scenario :

1) Client connects to server on one port, say 13101.

2) Once it sends, it will wait on another port 13102 to get the ACK.

3) Here Server can handle multiple connections [using select() on same port number].

Now, I am facing 2 problems: 1) Server on receiving data from Client1, it is processing data and for sending the ACK back [to client1], I am preparing a new TCP connection [with port 13102] and trying to send data. It fails with "Connection Refused".

2) In order to verify the above problem, I wrote another client2 program that just sends data to port 13102 [to client1, when it is in listening mode]. Still client2 is getting "Connection refused" error.

Yes, you can open lots of sockets! However, you shouldn't need separate sockets for sending and receiving, a TCP socket is bi-directional once it's opened. As for your error, if you're using two machines, there could be a firewall preventing the server from connecting to your client. You might try using telnet to try to connect to the same port.

The error is probably because the client is not listening for an incoming connection. As stated above you can use a single socket for both send and receive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM