Which socket programming is best (TCP/UDP)? [close

2019-09-08 09:54发布

问题:

My client program wants to send a huge file to the server and in return the server program returns a double or triple sized file.

My question is, which approach should I use? Either TCP or UDP.

回答1:

UDP programmin but it will be difficult to implement



回答2:

You could utilize FTP (File Transfer Protocol) for your use case.
It is very common and you can use it with java to get or to upload files to the FTP server.

Also take a look at this question on SO: File upload in Java through FTP


If you still want to implement it yourself, I would recommend using TCP, since it offers you some services:

  • Ordered data transfer — the destination host rearranges according to sequence number
  • Retransmission of lost packets — any cumulative stream not acknowledged is retransmitted
  • Error-free data transfer

    http://en.wikipedia.org/wiki/Transmission_Control_Protocol#Data_transfer



回答3:

This question is too broad, but the answer is probably TCP; if you're needing to transfer a file, TCP provides ordering and retransmission services that UDP doesn't, and there's no reason to reinvent the wheel.

Along those lines, though, why reinvent HTTP? This sounds like a classic case for using a Web server.