Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java套接字UDP循环_Java_Sockets_Loops_Tcp_Udp - Fatal编程技术网

Java套接字UDP循环

Java套接字UDP循环,java,sockets,loops,tcp,udp,Java,Sockets,Loops,Tcp,Udp,我正试图建立一个程序,调查数据包的UDP性能和网络上的响应时间。我有一个客户端和服务器端类,我为它指定发送文本的数据包大小。例如,如果我想在一个4字节的数据包中发送一个单词“Tester”,它将发送“TEST”部分,但不会重复该单词的其余部分。我试图添加一个while循环,但我不认为我所拥有的是正确的,因为它连续发送前4个字节。有人知道我需要什么样的循环,应该把它放在哪里才能得到我想要的结果吗?客户端的代码如下所示。非常感谢您的指导 //UDP Client //Usage: java UDPC

我正试图建立一个程序,调查数据包的UDP性能和网络上的响应时间。我有一个客户端和服务器端类,我为它指定发送文本的数据包大小。例如,如果我想在一个4字节的数据包中发送一个单词“Tester”,它将发送“TEST”部分,但不会重复该单词的其余部分。我试图添加一个while循环,但我不认为我所拥有的是正确的,因为它连续发送前4个字节。有人知道我需要什么样的循环,应该把它放在哪里才能得到我想要的结果吗?客户端的代码如下所示。非常感谢您的指导

//UDP Client
//Usage: java UDPClient [server addr] [server port]

import java.io.*;
import java.net.*;

public class UDPClient extends variable {

   // static Integer portNo = 4444;
    static Integer byteSize = 4;

    public static void main(String[] args) throws Exception {
      SocketForm form = new SocketForm();

      long startTime; // Starting time of program, in milliseconds.
      long endTime;   // Time when computations are done, in milliseconds.
      double time;  

        //get server address
        String serverName = "localhost";

        if (args.length >= 1)
            serverName = args[0];
      InetAddress serverIPAddress = InetAddress.getByName(serverName);

        //get server port;
        int serverPort = form.cliportNo;
        if (args.length >= 2)
            serverPort = Integer.parseInt(args[1]);
        //create socket
        DatagramSocket clientSocket = new DatagramSocket();
        //get input from keybaord
        byte[] sendData = new byte[byteSize];
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader (System.in));
        while (true){ //incorrect as it is only repeating the first four bytes of the word typed in the console
        String sentence = inFromUser.readLine();
        startTime = System.currentTimeMillis();
        sendData = sentence.getBytes();
        //construct and send datagram;

        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverIPAddress, serverPort);
        clientSocket.send(sendPacket);

        //receive datagram
        byte[] receiveData = new byte [byteSize];

        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        //print output
        String sentenceFromServer = new String(receivePacket.getData());
        System.out.println("From Server:" + sentenceFromServer);

        //close client socket
                //clientSocket.close();
        endTime = System.currentTimeMillis();
        time = endTime - startTime;
        //System.out.println("Time :" + time);
        }
    } //end of main
} //end of UDPClient
你是说像这样吗

private void sendChunked( String msg, int chunkSizeInBytes ) {
   byte[] msgBytes = msg.getBytes();
   for( int index = 0; index < msgBytes.length ; index += chunkSizeInBytes ) {
         DatagramPacket packet = new DatagramPacket( msgBytes, index, Math.min( chunkSizeInBytes, msgBytes.length-index ));
         send( packet ); // You know how that works ...
   }
}
private void sendChunked(字符串msg,int chunkSizeInBytes){
字节[]msgBytes=msg.getBytes();
对于(int index=0;index
如果你将有效载荷切碎成和平,你必须自己重建。请记住,UDP数据包可能出现顺序错误,也可能不是全部。。。这是一种任务吗?这是一种任务。这是老板交给我们的任务,因为他对这类事情“感兴趣”。(我是Java团队的新手,与Java团队合作已经有一段时间了)理想情况下,我们只是希望看到我们的程序能够分解字符串,将字节放入设置的数据包大小,以及这些数据包传输所需的时间。我不太在意重建,只是每个数据包传输所需的时间。有没有办法说“发送这个字符串的前4个字节”,循环发送下4个字节,直到没有剩余的字节可以传输。