简体   繁体   English

Java套接字UDP循环

[英]Java Socket UDP Loop

I am trying to set up a program that looks into UDP performance of packets and response times on my network. 我正在尝试建立一个程序,以研究网络上数据包的UDP性能和响应时间。 I have a client and server side class, for which I am specifying the packet size which to send pieces of text. 我有一个客户端和服务器端类,为此我指定了发送文本片段的包大小。 For example, if I want to send a word of "Tester" in a 4 byte packet, it will send the "TEST" part but not reiterate through the rest of the word. 例如,如果我要在4字节的数据包中发送单词“ Tester”,它将发送“ TEST”部分,但不会在单词的其余部分重复。 I have tried to add in a while loop, but i don't think what I have is correct as it continuously sends the first 4 bytes. 我试图添加一个while循环,但是我不认为我的正确,因为它连续发送前4个字节。 Does anyone know what sort of loop I need and where abouts it should be placed to get the outcome I am after? 有谁知道我需要哪种循环以及应该将其放置在什么位置才能得到我想要的结果? Code for the client is below. 客户端代码如下。 Many thanks in advance for any guidance. 在此先感谢您的指导。

//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

Do you mean like this? 你是这个意思吗

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 ...
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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