piyumi_rameshka
piyumi_rameshka

Reputation: 350

Send and Receive packets from same TCP port

I have two custom programs that one sends a stream of data and other one receive that stream for further processing. Is it possible to send data to a one TCP port and receive(read) that data from the same port ?. If not can you recommend any alternative method.Any help will is appreciated. (The two programs run on the same machine)

Upvotes: 1

Views: 5703

Answers (1)

David Schwartz
David Schwartz

Reputation: 182865

A TCP connection has two ends and permits communication in both directions. Each end's sending port is the other end's receiving port. That is always the case.

If you're asking if each end's sending port can be the same as the other end's receiving port, the answer is yes. That's always the case.

If you're asking if both ends can have the same sending and receiving port, the answer is:

1) If the source IP address is the same on both ends (say 127.0.0.1) then absolutely not. If they could be, it would be impossible to distinguish between the two endpoints.

2) If the source IP address is different on both sends (say 127.0.0.2 on one end and 127.0.0.3 on the other) this is theoretically possible and can easily happen if you're using two different machines. But so far as I know, no common TCP/IP stack permits a single port to be used in this way if both ends are on the same machine. Once you're listening on a port, you are not permitted to use that port as a source port. And if you're using a port as a source port, you're not permitted to begin listening on it.

But I get the feeling that you're asking the wrong question. If you clarified what you're trying to do and why, you might get an answer to a different question that would be of more help to you.

Upvotes: 2

Related Questions