简体   繁体   中英

UDP to TCP Java

I am trying to figure out how to make this piece of code into TCP instead of UDP

DatagramPacket answerDP = null;
answerDP = new DatagramPacket(new byte[110], 110);  

What do I use in TCP instead of DatagramPacket ?

The same goes for DatagramSocket , what do I use in TCP instead?

DatagramSocket socket = null;    
socket = new DatagramSocket(); 
socket.send(packet);
socket.setSoTimeout(5000); //wait for answar max. 1 sec.
socket.receive(answerDP);     

DatagramSocket in TCP is basically ServerSocket

So for example, to initialise it:

ServerSocket welcomeSocket = new ServerSocket(6789);

And the client socket would be something like:

Socket clientSocket = new Socket("localhost", 6789);

Set timeout works the same

socket.setSoTimeout(5000);

See a simple example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM