-1

I have the servers connected, but I don't really understand how TCP works well enough to accomplish the task. I want to send the file name before I starting writing the content, but how can I read the file name separately from the content. This was really easy in UDP with socket.receive(packet), but I can't think of a comparable way of doing this with TCP sockets.

Xerunix
  • 431
  • 1
  • 6
  • 21

2 Answers2

1

Write the length of the filename using a fixed number of bytes. Then write the filename.

On the receiving side, read the length of the filename and then once you know the length, read the filename.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

You have two options, depending on the design of your protocol:

  1. send the filename length as a fixed-sized integer, then send the actual filename. The receiver can then read the length first and subsequently read however many bytes it indicates to read the filename.

    <length><filename><file data>
    
  2. send the filename and then send a unique delimiter, such as a CRLF. The receiver can then keep reading until it encounters the delimiter.

    <filename><delimiter><file data>
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770