2017-05-22 70 views
-2

ここでは、2つの別々のパケットを送受信するように作成しています。これに代えて、同じデータを使ってデータを上書きして、新しいデータバッファでsetDataメソッドを使用して返信するにはどうすればいいですか?packet.setData(newbuffer);同じDatagramPacketを使用してUDPでデータを送受信する方法

クライアント

package labsheet2; 

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

public class Clientnew { 
    public final static int UDP_PORT = 50001; 
    public static void main(String[] args) throws Exception { 
    System.out.println("Server Time >>>>"); 

    //create a DatagramSocket object 
    DatagramSocket clientSocket = new DatagramSocket(); 
    InetAddress ip = InetAddress.getByName("localhost"); 

    //create buffers to store datagram data in DatagramPacket Objecct 
    byte[] buffReceiveData = new byte[100]; //for incoming data 
    byte[] buffSendData = new byte[100]; //for outgoing data 

    //create the outgoing Datagram with ip and port 
    DatagramPacket packetOut = new DatagramPacket(buffSendData, 
    buffSendData.length, ip, UDP_PORT); 

    //create the incoming DatagramPacket object to wrap receiving data 
    DatagramPacket packetIn = new DatagramPacket(buffReceiveData, 
    buffReceiveData.length); 
    clientSocket.send(packetOut); //send data 
    clientSocket.receive(packetIn); //receive data from the server 
    String time = new String(packetIn.getData()); 
    System.out.println(time); 
    clientSocket.close(); //close the client socket 
} 

}

サーバー

package labsheet2; 

import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 
import java.util.Date; 

public class Servernew { 
public final static int UDP_PORT = 50001; 
public static void main(String[] args) throws Exception { 

//create a DatagramSocket and bind it to the PORT 
DatagramSocket serverSocket = new DatagramSocket(UDP_PORT); 
while (true) { 
System.out.println("Server is up...."); 

//create buffers to store datagram data in DatagramPacket Objecct 
byte[] buffReceiveData = new byte[100]; //for incoming data 
byte[] buffSendData = new byte[100]; //for outgoing data 

//Datagram object to wrap incoming data 
DatagramPacket packetIn = new DatagramPacket(buffReceiveData, 
buffReceiveData.length); 

//Receive the incoming data packet to DatagramPacket Object 
serverSocket.receive(packetIn); 

//Get the source ip from the incoming packet 
InetAddress ip = packetIn.getAddress(); 

//Get the source port from the incoming packet 
int port = packetIn.getPort(); 
buffSendData = new Date().toString().getBytes();//get Date in bytes 

//packetIn.setData(buffReceiveData, buffReceiveData.length, ip, port); 

//create the outgoing Datagram with source ip and port 
DatagramPacket packetOut = new DatagramPacket(buffSendData, 
buffSendData.length, ip, port); 
serverSocket.send(packetOut); 
packetIn = null; //reset incoming DatagramPacket Object 
System.out.println("Done !! "); 
} 
} 
} 

答えて

1

どのように私はSを使用することができますpacket.setData(newbuffer);

データを上書きすることで、新たなデータ・バッファとsetData()方法を使用して:新しいデータ・バッファとsetData方法を使用して、データを上書きすることによって、応答するAMEパケットpacket.setData(newbuffer);

+0

簡単な10点Iここで今までに得たことがあります。 – EJP

関連する問題