[英]UDP to TCP Java
I am trying to figure out how to make this piece of code into TCP instead of UDP 我试图弄清楚如何将这段代码转换为TCP而不是UDP
DatagramPacket answerDP = null;
answerDP = new DatagramPacket(new byte[110], 110);
What do I use in TCP instead of DatagramPacket
? 在TCP中代替
DatagramPacket
什么?
The same goes for DatagramSocket
, what do I use in TCP instead? DatagramSocket
也是如此,我该如何在TCP中使用呢?
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
TCP中的
DatagramSocket
基本上是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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.