2017-05-22 1 views
-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

은 내가 어떻게의를 사용할 수 있습니다 새로운 데이터 버퍼와 setData() 방법을 사용하여 데이터를 덮어 써서 packet.setData(newbuffer);

: AME 패킷은 새로운 데이터 버퍼와 setData 방법을 사용하여 데이터를 덮어 쓰기하여 답장 packet.setData(newbuffer);

+0

쉬운 10 점 I 이제까지 여기에서 번다. – EJP

관련 문제