Windows sockets (tcpclient) problem.

I

Inspired

hi..
im writing an application that uses tcpclient/tcplistener to send file
via network, what im doing is dividing the file into small parts each
part equals the buffer size (8192) and send the parts sequentialy.
the problem is :
the listener is detecting a stream to be read once every two times the
sender sends a stream.
I triend to fix it by sending one byte {0} before each send function
and it worked because the listener ignores the first send (which is the
0 byte) then detects the next send which is the acutaul data, but i
realized that the 0 byte is still in the buffer so its attached the
next stream read.
any help? any idea is appreciated .. thank you in advance.
 
B

Bart Mermuys

hi,

Inspired said:
hi..
im writing an application that uses tcpclient/tcplistener to send file
via network, what im doing is dividing the file into small parts each
part equals the buffer size (8192) and send the parts sequentialy.
the problem is :
the listener is detecting a stream to be read once every two times the
sender sends a stream.
I triend to fix it by sending one byte {0} before each send function
and it worked because the listener ignores the first send (which is the
0 byte) then detects the next send which is the acutaul data, but i
realized that the 0 byte is still in the buffer so its attached the
next stream read.
any help? any idea is appreciated .. thank you in advance.

TCP is a byte-stream protocol not a message or package stream, if you send a
buffer it can be split or joined.

If you want a packet stream then you have to build your protocol on top of
it, either by using:
- delimiter between packets
- prefixing each variable packet with the packet length
- using fixed size packets

You are already using fixed size packets, but the end packet of the file may
have a different size, at the least you will need to send the length of the
file before transmitting the file : (pseudocode)

Do While( receivedBytes < fileSize )
int i = receive ( 4000 bytes)
' nothing garantees we have received 4000 bytes
' that's why i is used
receivedBytes+= i
add i received bytes to byte queue
If ( byte queue >= 8192 )
process first 8192 from byte queue
remove first 8192 from byte queue
End If
End
process last packet, which could be smaller then 8192

Also note that for file transfer it may not be necesairy to receive them in
blocks (just write the bytes as they come in), except if you want to do a
checksum or something on a block basis.

HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top