2016-05-09 13 views
0

Hyが、私はクライアントサーバデータグラム通信を行う必要があると私は少し問題を抱えていました。送信-recive方法が良い取り組んでいるが、私は、クライアントからのsecound時間を送信しようとした場合、サーバは再びメッセージ全体をreciveません。データグラムクライアントサーバーのJava - メッセージ転送問題

私はバッファをフラッシュする必要があることkwnow TCP転送で
Server Started and listening to the port 10000 
Recive from client: Send me a datagram 
Send to client: I am Server! 

Recive from client: Send me a da  -> HERE is the PROBLEM 
Send to client: I am Server! 

、私が何をしなければならないのですか?

DGSServerT:

public class DGSServerT { 
    public static void main (String [] args) throws IOException { 
     final int port = 10000; 

     // Create a datagram socket bound to port 10000. Datagram packets sent from client programs arrive at this port. 
     DatagramSocket s = new DatagramSocket (port); 
     System.out.println("Server Started and listening to the port " + port); 

     // Create a byte array to hold data contents of datagram packet. 
     byte [] data = new byte [100]; 

     // Create a DatagramPacket object that encapsulates a reference to the byte array and destination address information. The 
     // DatagramPacket object is not initialized to an address because it obtains that address from the client program. 
     DatagramPacket dgp = new DatagramPacket (data, data.length); 

     // Enter an infinite loop. Press Ctrl+C to terminate program. 
     while (true) { 
      // Receive a datagram packet from the client program. 
      s.receive (dgp); 
      // Display contents of datagram packet. 
      System.out.println ("Recive from client: " + new String (data)); 

      InetAddress address = dgp.getAddress(); 
      int clPort = dgp.getPort(); 
      data = new String ("I am Server!").getBytes(); 
      dgp = new DatagramPacket (data, data.length, address, clPort); 

      // Echo datagram packet back to client program. 
      s.send (dgp); 
      System.out.println ("Send to client: " + new String (data)); 
     } 
    } 
} 

DGSClientT: "しばらく" ループに

DatagramPacket dgp = new DatagramPacket (data, data.length); 

public class DGSClientT { 
    public static void main (String [] args) { 
     String host = "localhost"; 

     // If user specifies a command-line argument, that argument represents the host name. 
     if (args.length == 1) { 
      host = args [0]; 
     } 
     DatagramSocket s = null; 

     try { 
      s = new DatagramSocket(); 

      // Create a byte array that will hold the data portion of a datagram packet's message 
      byte [] buffer = new String ("Send me a datagram").getBytes(); 

      InetAddress ia = InetAddress.getByName (host); 
      DatagramPacket dgp = new DatagramPacket (buffer, buffer.length, ia, 10000); 

      // Send the datagram packet over the socket. 
      s.send (dgp); 
      System.out.println("Send to server: " + new String (dgp.getData())); 

      // Create a byte array to hold the response from the server program 
      byte [] buffer2 = new byte [100]; 

      // Create a DatagramPacket object that specifies a buffer to hold the server program's response, the IP address of 
      // the server program's computer, and port number 10000. 
      DatagramPacket dgp2 = new DatagramPacket (buffer2, buffer.length, ia, 10000); 

      // Receive a datagram packet over the socket. 
      s.receive (dgp2); 

      // Print the data returned from the server program and stored in the datagram packet. 
      System.out.println ("Recive from Server: " + new String (dgp2.getData())); 
     } catch (IOException e) { 
      System.out.println (e.toString()); 
     } finally { 
      if (s != null) { 
       s.close(); 
      } 
     } 
    } 
} 

答えて

0

あなたが最初の行を移動する必要があります。 "dgp"は、whileループの最後に12バイトのバッファ長で構築され、サーバーの応答を送信します。

+0

あなたが言及したラインのほかに、私は「バイト[]のデータを移動...私はあなたの助けを借りて解決:) – Doro

+0

...問題は、私は、whileループでラインを移動しますが、それは動作しません...同じ=新しいバイト[100]; "しばらくして、それは大丈夫です。ありがとう:) – Doro

関連する問題