如何在Java中接收多个UDP数据报包?

时间:2017-06-19 01:19:09

标签: java sockets udp

我创建了一个小型演示,其功能是:
  - 在UDPClient中:
 +用户输入两句话:句子A和句子B
 +将它们传递给UDPServer

  - 在UDPServer中:
 +那两句话就是    资本化的  +将这两个大写句子传回UDPClient

问题是,当我将两个句子从UDPClient传递给UDPServer时例如:" aa"和" bb" 。所以,在UDPServer中,我可以完全得到" aa"和" bb" 。但是,当我返回大写句子时:" AA"和" BB"从UDPServer到UDPClient,我只能获得" AA"。我怎样才能得到" AA"和" BB"在UDPClient处理之后?
这是我的代码:
UDP客户端:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

public class UDPClient extends Thread {

static int port = 1234;

public UDPClient() {

}

@Override
public void run() {
    super.run();
    while (true) {
        try {
            DatagramSocket clientSocket = new DatagramSocket(5678);
            BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
            InetAddress IPAddress = InetAddress.getByName("localhost");

            byte[] sendData = new byte[1024];
            byte[] sendData1 = new byte[1024];

            byte[] receiveData = new byte[1024];

            System.out.println("\nInput sentenceA : " );
            String sentence = inFromUser.readLine();
            System.out.println("Input sentenceB : " );
            String sentence1 = inFromUser.readLine();
            sendData = sentence.getBytes();
            sendData1 = sentence1.getBytes();

            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 1234);
            clientSocket.send(sendPacket);
            DatagramPacket sendPacket1 = new DatagramPacket(sendData1, sendData1.length, IPAddress, 1234);
            clientSocket.send(sendPacket1);

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            String modifiedSentenceA = new String(receivePacket.getData());
            String modifiedSentenceB = new String(receivePacket.getData());
            System.out.println("FROM SERVER : " + " sentenceA : " + modifiedSentenceA + ", sentenceB : " + modifiedSentenceB);

            clientSocket.close();
        } catch (SocketException ex) {
            System.out.println(ex);;
        } catch (UnknownHostException ex) {
            System.out.println(ex);;
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}

public static void main(String[] args) throws SocketException {
    Thread t = new UDPClient();
    t.start();
}
}

UDPServer:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class UDPServer extends Thread{


public UDPServer(){

}

@Override
public void run() {
    super.run(); //To change body of generated methods, choose Tools | Templates.
        try {
        DatagramSocket serverSocket = new DatagramSocket(1234);

        byte[] receiveData = new byte[1024];

        byte[] sendData = new byte[1024];
        byte[] sendData1 = new byte[1024];
        while(true){
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            String sentence = new String(receivePacket.getData());
            String sentence1 = new String(receivePacket.getData());
            System.out.println("RECEIVED " + sentence + " sentence1 : " + sentence1);

            InetAddress IPAddress = receivePacket.getAddress();
            int port = receivePacket.getPort();

            String capitalizedSentence = sentence.toUpperCase();
            String b = sentence1.toUpperCase();

            sendData = capitalizedSentence.getBytes();
            sendData1 = b.getBytes();

            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
            DatagramPacket sendPacket1 = new DatagramPacket(sendData1, sendData1.length, IPAddress, port);
            serverSocket.send(sendPacket1);


        }
    } catch (SocketException ex) {
        System.out.println(ex);;
    } catch (IOException ex) {
        System.out.println(ex);;
    }

}

public static void main(String[] args){
    Thread serverThead = new UDPServer();
    serverThead.start();
}
}

1 个答案:

答案 0 :(得分:0)

DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentenceA = new String(receivePacket.getData());
String modifiedSentenceB = new String(receivePacket.getData());

您的客户只执行一次接收。您打印接收的数据包两次,但它是相同的数据包。