【发布时间】:2019-10-02 01:24:44
【问题描述】:
我正在尝试实现一个 TCP 客户端,它每 50 毫秒向服务器发送一次。所以我开发了一个快速的 TCP 测试程序,我可以在其中更改每条消息之间的时间,但我并没有真正让消息每 X 毫秒发送一次,而且它们正在累积,正如你在 Wireshark 捕获中看到的那样。有什么想法吗?
应该只有一个字母a,并且从这个TCP流中的上一帧开始的时间应该接近这个例子中控制台引入的值0.08秒
public class TCPClient {
static Socket clientSocket;
static DataOutputStream outToServer;
public static class enviar extends TimerTask {
public void run() {
try{
outToServer.writeBytes("a");
System.out.println("Packet Sent");
}catch(Exception e){
System.out.println(e);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int tiempo = 1000;
System.out.print("Tiempo: ");
tiempo = scanner.nextInt();
try{
clientSocket = new Socket("192.168.1.21", 1337);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
}catch(Exception e){
System.out.println(e);
}
Timer timer = new Timer();
timer.schedule(new enviar(), 0, tiempo);
}
}
【问题讨论】:
标签: java networking tcp ip