Java 如何使用相同的DatagramPacket在UDP中发送和接收数据
在这里,我创建了两个单独的数据包来发送和接收。与此相反,我如何使用相同的数据包来回复,通过覆盖数据,使用带有新数据缓冲区的setData方法:packet.setData(newbuffer) 客户 } 服务器 我如何使用相同的数据包来回复,通过覆盖数据,使用带有新数据缓冲区的Java 如何使用相同的DatagramPacket在UDP中发送和接收数据,java,udp,Java,Udp,在这里,我创建了两个单独的数据包来发送和接收。与此相反,我如何使用相同的数据包来回复,通过覆盖数据,使用带有新数据缓冲区的setData方法:packet.setData(newbuffer) 客户 } 服务器 我如何使用相同的数据包来回复,通过覆盖数据,使用带有新数据缓冲区的setData方法:packet.setData(newbuffer) 通过覆盖数据,使用带有新数据缓冲区的setData()方法:packet.setData(newbuffer) package labsheet2;
setData
方法:packet.setData(newbuffer)代码>
通过覆盖数据,使用带有新数据缓冲区的setData()
方法:packet.setData(newbuffer)代码>
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 !! ");
}
}
}