2013-10-18 2 views
0

네트워크의 패킷 및 UDP 응답의 UDP 성능을 조사하는 프로그램을 설정하려고합니다. 클라이언트와 서버 사이드 클래스가 있는데, 텍스트 조각을 보낼 패킷 크기를 지정하고 있습니다. 예를 들어, "Tester"라는 단어를 4 바이트 패킷으로 보내려면 "TEST"부분을 보내고 나머지 단어는 반복하지 마십시오. while 루프를 추가하려고 시도했지만, 처음 4 바이트를 연속적으로 보내는 것처럼 정확하지 않다고 생각합니다. 누구든지 내가 필요한 루프의 종류와 그 결과를 얻기 위해 배치해야 할 위치를 알고 있습니까? 클라이언트 코드는 다음과 같습니다. 모든 지침에 대해 미리 감사드립니다.Java 소켓 UDP 루프

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

페이로드를 peaces로 잘라 내면 직접 재구성해야합니다. UDP 패킷은 순서가 잘못되었거나 전부가 아니라는 것을 기억하십시오 ... 이것은 일종의 할당입니까? – Fildor

+0

그 종류의 과제. 사장이 그 일에 '관심을 가졌기'때문에 우리에게 주어진 임무입니다. (나는 자바 팀이 처음이다. 그리고 그 작업을한지 얼마되지 않았다.) 이상적으로 우리는 우리 프로그램이 문자열을 해체하고, 바이트를 설정된 패킷 크기에 맞추고, 시간을 계산할 수있는 방법을 찾고있다. 패킷이 전송되는 데 오랜 시간이 걸립니다. Im은 재구성에 대해별로 신경 쓰지 않고 각 패킷이 전송하는 데 걸리는 시간입니다. '이 문자열의 처음 4 바이트 보내기'라고 말하고, 전송할 바이트가 남아 있지 않을 때까지 루프를 통과하여 다음 4 개를 보냅니다. – Reidacus

답변

0

이 뜻입니까?

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

은 완벽하게 작동합니다. 대단히 감사합니다. D – Reidacus