0

When I do the following steps to receive a message and send a reply, it fails. I am using TCP. I need the program to send data from the same port it received from.

bind() listen() accept() recv() connect()//it fails to connect here using the same socket.<br> send() 
2
  • You are a TCP server - clients do the connecting, not you! Get rid of the connect attempt. Just send to the same socket you used for the recv call. Commented Dec 14, 2013 at 13:58
  • I want to send to another client from the same port Commented Dec 14, 2013 at 14:05

1 Answer 1

4

It seems you have a problem in understanding the way tcp works. There is a server and a client. The server waits for connections, and the client makes connections. Once a connection is established, the server and the client can communicate bi-directional (i.e. both can send and recive messages). Of course, their role might change, but this is the way it works. So, the server does:

bind() listen() accept() recv() send() 

It is stuck at accept() until a client performs connect() on the port that the server is listening to.

As my explanation is pretty brief, I suggest you read this tutorial about linux sockets.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.