NetCat and TCP

Hi guys,

I think I have some misunderstandings when it comes to the TCP protocol.

Netcat uses TCP to establish TCP connections from one machine to another( even supports UDP), so I've got a couple of questions when it comes to the TCP protocol and how NetCat uses it.

Why is the TCP 3 way hand shake so essential for establishing a connection? couldn't we just communicate without this handshake ie computer A sends a packet with a sequence number and computer B sends back a packet with a sequence number and acknowledgement( so we know that computer A's packet was received.

Listening and sending, in NetCat you can put a client(computer A) to listen on a certain port for example ( nc -l 1223 ) this machine will now be listening on port 1223 waiting for a TCP connection,

but why do we need to listen?? couldn't we just send a packet from computer B to computer A without listening? and how come computer B doesn't have to listen? only computer A

Can a TCP connection be established between two clients? instead of one server and one client and if so how?

Thanks
Last edited on
couldn't we just communicate without this handshake
TCP is a stateful protocol. Both the client and the server need to set up data structures to keep track of the state of the connection to handle packet retransmission, etc.

but why do we need to listen??
Because a connection needs to be established before packets can be sent, and in turn a connection is needed because TCP needs to be able to guarantee that either the packet was received by the destination, or that it was lost and this fact must be notified to the sender.

couldn't we just send a packet from computer B to computer A without listening?
If there's no one listening, what's the point of sending data? I mean, even if there's no one "listening" in the TCP sense, you still need a program on the receiving end that will pull the data from the OS's buffer.
Yes, you can send UDP datagrams or even raw IP or Ethernet packets to some IP or MAC address with zero guarantee that the data is being received by anyone (unless you've already established an implicit connection by external channels, such as through TCP, or by directly controlling both ends of the communication).

Can a TCP connection be established between two clients? instead of one server and one client and if so how?
No. In TCP, one of the participants is the server and it listens for incoming connections, and the other is the client and it opens a connection to a server.
Any connection-based protocol will have the same limitation. To establish a connection, someone first needs to be listening for it and someone else needs to request opening it.
Last edited on
What you're describing is a protocol that sits somewhere between TCP and UDP (which are also commonly called transport protocols). UDP maintains absolutely no state. If you send a UDP packet, it may make it to its destination, it may not, you may receive a confirmation, you may not. The main takeaway being that there is no guarantee built into UDP about what might have happened. In other words, it is a stateless protocol.

TCP (like Helios said) is a stateful protocol. Both sides maintain a lot of information about what is going on (such as the order of packets and what packet should be expected next). This allows it to recover from errors and failures, and provide detailed error messages to the user. You can implement a new transport protocol (or even make one on top of UDP), but as you run into more problems and edge cases, you'll find where TCP has its advantages, and why its usually saner to just start there (assuming reliability is a factor).
Thanks guys that makes sense :)
Topic archived. No new replies allowed.