使用数据报多包的套接字编程java

Socket Programming java using datagram multiple packets

我正在尝试制作一个客户端-服务器程序。在客户端的哪个位置,我从用户那里获得了多个输入,我必须将其传递给服务器。服务器使用该信息并进行一些计算并发回结果。 客户端代码的重要部分:

private byte[] buf = new byte[256];
private byte[] buf2 = new byte[256];
private byte[] buf3 = new byte[256];
while (true) {

Arrays.fill(buf, (byte) 0); 

buf = jTextField1.getText().trim().getBytes();
sendPacket = new DatagramPacket(buf, buf.length, address, 8000);
socket.send(sendPacket);

buf2 = jTextField2.getText().trim().getBytes();
sendPacket = new DatagramPacket(buf2, buf2.length, address, 8000);
socket.send(sendPacket);

buf3 = jTextField3.getText().trim().getBytes();
sendPacket = new DatagramPacket(buf3, buf3.length, address, 8000);
socket.send(sendPacket);

//Get result from Server
socket.receive(recivePacket);
double result  = Double.parseDouble(new String(buf).trim());
System.out.println("Result" +result);

}

在服务器端,我正在尝试接收数据(无法正常工作)

   byte[] buf = new byte[256];
   DatagramPacket  recivePacket = new DatagramPacket(buf, buf.length);
   DatagramPacket dgp = new DatagramPacket(buf, buf.length);
    while (true) {
            Arrays.fill(buf, (byte) 0);
            serverSocket.receive(recivePacket);

            System.out.println("host Name is " + 
   recivePacket.getAddress().getHostName() + '\n');

            while (true) {
                serverSocket.receive(dgp);
                System.out.println("String=====" + new String(buf));
            }

请帮帮我。这段代码有什么问题......我完全被卡住了 所以,奇怪的是有时它会起作用。但它给了我最后一个缓冲区的价值。其他值丢失。 提前致谢

所以我认为每次您在服务器上收到一些东西时,都需要为此创建一个新的 DatagramPacket/byte[]。 我所做的是每次收到这样的信息时创建一个新的 DatagramPacket:

DatagramPacket packet = new DatagramPacket(new byte[max_data_size], max_data_size);
socket.receive(packet);

我的max_data_size is normally around 1024 bits.

然后您可以读取客户端、端口、长度等内容:

InetAddress address = packet.getAddress();
int port = packet.getPort();
int len = packet.getLength();
byte[] data = packet.getData();

通常,您应该能够简单地将新的数据报包发送回给定的 InetAddress/port。

您想要用户的多个输入? 把所有东西都装在一个包裹里。


private YourClass newInstance(byte[] bytes){
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;
    YourClass o = null;
    try {
        in = new ObjectInputStream(bis);
        o = (YourClass)in.readObject();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (ClassCastException e){
        e.printStackTrace();
    }finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return o;
}

private byte[] toBytes(YourClass object){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    byte[] bytes = new byte[1];
    try {
        out = new ObjectOutputStream(bos);

        object.setTimestamp(System.currentTimeMillis());

        out.writeObject(object);
        out.flush();
        bytes = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bos.close();
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return bytes;
}