Toan Tran
Toan Tran

Reputation: 526

How to verify ACK packet with write function in TCP client?

I'm trying to write a TCP client with write function following this: guide.

I shut down my network and try to send a PING package with 2 bytes of data.

Since it's a TCP client, I expect some kind of error returned because there is no network at all and the server won't be able to receive the message, hence no ACK packet will be returned. However, write function returns 2 bytes were written, seem like it doesn't verify ACK packet from server side at all.

How could I check for ACK to make sure whether my message is sent successfully and server receive it?

Upvotes: 0

Views: 1197

Answers (1)

user11562467
user11562467

Reputation:

If you run both client and server on the same machine they'll communicate through the loopback interface, so unplugging or deactivating the outbound network interface will not change anything. Your server and client communicate happily with each other. If there truly isn't any connection between server and client the connect() should've failed with errno set to ENETUNREACH or ECONNREFUSED if the server refuses the connection. So what you probably want is to run the client on your machine and the server either another computer or in a VM with a bridged network. Note that write() successfully returning only means that the bytes are buffered to be send. It may even return fewer bytes than your message contains, indicating the buffer is currently full and you need to try sending the remaining bytes later.

The best way to analyze protocol implementations is using a tool like wireshark to see what packages are actually sent over the wire (or through the loopback interface).

Upvotes: 1

Related Questions