Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check that a byte is sent successfully using the TCP protocol? [closed]

Tags:

tcp

sockets

I'm having a problem doing network programming. I'm using the TCP protocol to communicate between server and client. My code is working, but I as yet can't detect that the data is successfully sent or if it failed. I have the following questions:

  1. How does one check that the bytes have been sent with socket TCP successfully or not?
  2. How acknowledgment (ACK) is work in TCP protocoll ?
  3. How does one do secure communication using socket programing?

You can explain in C#, Java or PHP.

like image 798
viyancs Avatar asked Apr 17 '12 02:04

viyancs


2 Answers

An explanation of how TCP/IP works is best left as a research exercise, rather than an ad hoc SO question. While the usual first stop might be Wikipedia, that probably doesn't give you a good introduction from a conceptual level. In the comments there is also a link to a TCP tutorial.

However in essence, TCP is, as the name says, a transmission control protocol that provides a mechanism for the reliable delivery of a byte stream. Given a stream of bytes, TCP basically ensures that the receiver receives the bytes in the correct order. If a byte in the middle of a stream got "lost", then TCP can detect this and will arrange its re-transmission. All of this is transparent to the receiving party. The magic of TCP is that the receiving party just reads from the socket, and the data appears intact and in the correct order.

When you utilise a TCP socket, you don't deal with ACKs and NACKs and retransmissions. All of that is transparent to your application.

Now, you can detect that the other end has gone away, but you can't know that the other end definitely did or did not receive your message. This is the Two General's Problem.

But really, if you're interested in how TCP works read W Richard Stevens' TCP/IP Illustrated. Not trying to palm you off, but to understand it, you really do need to go away and read about it (either online or on dead trees).

like image 94
Greg Kopff Avatar answered Sep 27 '22 18:09

Greg Kopff


  1. Usually, whichever function you're using to send the data will return the amount of bytes sent, which you have to count and make sure they've all been sent. Concrete example here.

  2. ACK(nowledgement) packets are sent: during the establishment of a connection to another socket, while sending data and when terminating a connection to a remote host. Basically, they are meant to let the other party know whether your end has or not received the data sent. You don't get to mangle with them, unless you're coding raw sockets, which I wouldn't recommend for obvious reasons (such as implementation standards). Besides, opening raw sockets is forbidden in most systems unless you hold root/administrator access. If you're still interested in raw sockets, please do read this; you should also read the TCP RFC along with its associated documents.

  3. SSL implementation depends on however you want to do it, this means, on how you want your protocol to work like. PHP specific OpenSSL functions are here. If you want to know more about HTTPS' inner workings, I believe you should read here.

Also, if you're crafting your own protocol, I believe taking a peek into Google's Protocol Buffers might prove useful. Needless to say, before even attempting, you should have some idea on how the internet generally works and what you're getting yourself into.

P.S: About raw sockets, you really should try doing them with some C knowledge, as for some reason there seems to be little, or down to none, documentation for other languages (probably because there really is no need to touch them). In any case, this might be useful: http://www.tenouk.com/Module43a.html

like image 33
Misguided Avatar answered Sep 27 '22 19:09

Misguided