Re: Socket write behaviour is inconsistent?



cjard <mcw8@xxxxxxxxxx> writes:


[...]

THIS FAILS:
socket.Send(new byte[]{ 0x02 }); //send start
socket.Send(response); //send start
socket.Send(new byte[]{ 0x03 }); //send start

it fails because the client throws an error that EOT was encountered
from the host


THIS WORKS:
byte[] tmp = new byte[response.Length + 2]
Array.Copy(response 0, tmp, 1);
tmp[0] = 0x02;
tmp[tmp.Length -2] = 0x03;
socket.send(tmp)

[...]

As others have suggested, look at this with a packet sniffer.

A common bug in C socket programming is something like this:

char buf[8192];
read(socket_fd, buf, 8192);
if (!strchr(buf,'\x03')) {
/* Error! */
}

If three different packets are sent, read() can return 3 times, each
time with part of the packet.

It's possible that the first example sends 3 different packets, and
the second sends just one. In that case, the first example would
expose this bug, while the second would work around it (or mask it,
depending on your viewpoint).

One possibility is to make sure Nagle's algorithm is enabled (that is,
the NODELAY option is turned off) on the sending socket. This may
combine the multiple Send's into one, although it's not guaranteed.
Another possibility is to just use one of the two examples that works.
:-)

Good luck!

----Scott.
.



Relevant Pages

  • Re: Python sockets UDP broadcast multicast question??
    ... I'm new to socket programming so please bare with me. ... So my question is how can I receive a continuous stream of UDP packets ... The UDP protocol is defined 'unreliable' because it cannot guarantee ... So, first of all, if you cannon tolerate packet loss, consider ...
    (comp.lang.python)
  • Re: Python sockets UDP broadcast multicast question??
    ... I'm new to socket programming so please bare with me. ... receiver computer to have to respond, I only want it to receive the ... So, first of all, if you cannon tolerate packet loss, consider ... computer and the network, e.g. by inserting a delay between two packet ...
    (comp.lang.python)
  • Re: write data to a file from a kernel module
    ... receives a packet from the network interface card. ... What makes tcpdump and friends not usable that you need to write kernel ... char *tmp; ... Please read the FAQ at http://www.tux.org/lkml/ ...
    (Linux-Kernel)
  • write data to a file from a kernel module
    ... the pkt_handler function is called each time a packet is received. ... It is called by the kernel when a new packet has ... char *tmp; ... struct inode *inode; ...
    (Linux-Kernel)

Loading