In Linux,
I am programming simple chatting program using client-server structure. It includes file transfer function too.
I designed format used when client transfer file's data to server like this.
(1 byte that display this is file data) (2 byte information to distinguish sender, receiver) (1 byte that informs what current data sending size is) (DATA..............)
above information will be stored on buf[]
(char[]
)
of course, sender use write call to send file data, receiver use read call to receive file data.
I wonder that above format can be fragmented when read, write function using socket descriptor used.
for example, sender write sbuf[] = "abcdefgh"
. does receiver read rbuf[] = "abcdefgh"
always?
or rbuf[]
could be = "a"
or "ab"
or "abc"
or "abcd"
or .... something?
write()
does not necessarily writes out as many bytes as specfified and needs to be repeated. Always check the value returned by system calls. – alk Mar 18 at 7:23read
,write
from linux system call linux manual. However, it didn't mention about fragmentation and I couldn't find :(. Thanks you for your help in advance. – podray Mar 19 at 11:52read
. tcp is stream oriented and doesn't honor datagram boundries. It can send 20 bytes in a packet or 2000 depending on its own algorithms. You can't count on how it is going to break the data up. On theread
side read is just going to return how many bytes are present (up to the limit you specify) in the tcp buffer. Again, it can be 1 byte or 10000 bytes. Neither tcp norread
has any notion of what your program considers to be a message. It is just a long stream of bytes coming and going. – Duck Mar 20 at 15:39