RongRong
RongRong

Reputation: 1

how to know the client receive the file when use TCP

Here I use Java language of the socket knowledge.I use the client connect the server and receive the file i need .The Question is that how could i know the file the client receive is finished .

Upvotes: 0

Views: 215

Answers (3)

cHao
cHao

Reputation: 86535

When you've transferred the right number of bytes, the file is sent. That "right number" can be obtained in various ways, depending on the protocol.

If you're sending stuff over a socket with no protocol but TCP/IP, the client just reads the stream til it sees 0 bytes (which means the server's shut down its end of the connection). Note, this means that the server sends the file and then immediately shuts down the socket. (Not "closes", at least not immediately. Just "shuts down".) One file per connection. Part of the price you pay for not using a file transfer protocol of some kind.

If you wanted to verify, i suppose you could conceivably have the client calculate a hash of the file's contents and double-check it against what you calculate on your end. But i don't know of a commonly used protocol that does this.

Upvotes: 0

Noah
Noah

Reputation: 1966

Establish some protocol between the client and the server. If the client writes a known finished value back on the socket once it has finished, the server then will know the client's state. There are many ways to solve this problem, the one proposed is a 'sentinel value' strategy.

http://en.wikipedia.org/wiki/Sentinel_value

Upvotes: 1

ATaylor
ATaylor

Reputation: 2598

By either sending some sort of 'I'm done signal' over a control connection (FTP works like this), or by disconnecting the socket, which also signals the 'End of transfer'. A 'ReadByte()' on the socket of the other side will return '0' in that case.

Upvotes: 0

Related Questions