通过UDP套接字发送数据时出现问题

时间:2011-09-15 02:27:16

标签: java sockets udp

这是我昨天提出的问题的后续行动。我有一个家庭作业,通过客户端/服务器TCP套接字连接发送和接收数据。我想用UDP制作它的版本。我的想法是我可以重定向标准I / O并使用UDP发送流。例如,如果我输入:

 server: java UDPServer 5555 < file1.txt
 client: java UDPClient localhost 5555 > file2.txt

它应该将file1.txt中的数据从服务器发送到客户端的file2.txt。当我在单独的终端中运行客户端/服务器对时,会创建file2.txt但实际上从未发送过数据。相反,我似乎陷入无限循环,除非我杀死应用程序,否则我无法在终端中输入任何内容。

服务器代码是:

public static final int BUF_SIZE = 256;

public static void main(String[] args) throws IOException{

    port = Integer.parseInt(args[0]);

    DatagramSocket serverSocket = new DatagramSocket(port);

    BufferedInputStream input = new BufferedInputStream(System.in);
    BufferedOutputStream output = new BufferedOutputStream(System.out);

    byte[] receiveData = new byte[BUF_SIZE];
    byte[] sendData = new byte[BUF_SIZE];
    byte[] buf = new byte[BUF_SIZE];
    String sentence;

    if(System.in.available() > 0) {
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        InetAddress address = receivePacket.getAddress();
        int bytesRead = 0;
        while((bytesRead = input.read(buf, 0, BUF_SIZE)) != -1) {
            sentence = new String(buf, 0, bytesRead);
            sendData = sentence.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
            serverSocket.send(sendPacket);
        }
    } else {
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        sentence = new String(receivePacket.getData());
        output.write(sentence.getBytes());
    }

    serverSocket.close();
    input.close();
    output.close();
}

客户端代码是:

public static final int BUF_SIZE = 256;

public static void main(String[] args) throws IOException{

    String hostName = args[0];
    port = Integer.parseInt(args[1]);

    DatagramSocket clientSocket = new DatagramSocket();
    InetAddress address = InetAddress.getByName(hostName);

    BufferedInputStream input = new BufferedInputStream(System.in);
    BufferedOutputStream output = new BufferedOutputStream(System.out);

    byte[] sendData = new byte[BUF_SIZE];
    byte[] receiveData = new byte[BUF_SIZE];
    byte[] buf = new byte[BUF_SIZE];
    String sentence;

    if(System.in.available() > 0) {
        int bytesRead = 0;
        while((bytesRead = input.read(buf, 0, BUF_SIZE)) != -1) {
            sentence = new String(buf, 0, bytesRead);
            sendData = sentence.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
            clientSocket.send(sendPacket);
        }
    } else {
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        sentence = new String(receivePacket.getData());
        output.write(sentence.getBytes());
    }

    clientSocket.close();
    input.close();
    output.close();
}

我仍然是socket编程的新手,所以我基于示例代码在我的教科书中。是否有一些明显的错误,我正在阻止数据传输?非常感谢您的耐心和帮助!

2 个答案:

答案 0 :(得分:0)

您遇到的第一个问题是 client.main 未运行,因为您的shell命令错误。出于某种原因,使用Java,您无法重定向输出传统方式。您可以在 client.main 的开头简单地放置一些print语句,以便不执行任何操作。试试这个:

java UDPClient localhost 5555 | tee file2.txt

请参阅redirect Java output

您遇到的另一个问题是客户端和服务器都在等待接收数据报。你有服务器代码:

if(System.in.available() > 0) {
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);

你有客户代码:

else {
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        serverSocket.receive(receivePacket);
        sentence = new String(receivePacket.getData());
        output.write(sentence.getBytes());
        }

请注意,else语句将始终执行,因为 System.in.available 将始终返回0.记住您不将重定向输入重定向到客户端,因此 System.in中没有任何内容。要解决此问题,您需要先发送客户端数据报,以便服务器可以使用 file2.txt 的内容进行响应。

答案 1 :(得分:0)

你错过了使这项工作需要做的所有事情。代码在哪里进行传输调步,重传,确认,重新排序等等?如果你想使用UDP,你必须自己完成TCP为你做的所有事情。

有关在UDP之上分层的文件传输协议的示例,请参阅RFC 1350