468,720 Members | 1,602 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Windows sockets (tcpclient) problem.

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.

Nov 23 '05 #1
1 1442
hi,

"Inspired" <mu***********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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

Nov 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dmitry Akselrod | last post: by
reply views Thread by Torsten Brasch | last post: by
4 posts views Thread by Sueffel | last post: by
reply views Thread by Tim Wagaman | last post: by
9 posts views Thread by TulasiKumar | last post: by
1 post views Thread by larspeter | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.