UDP广播问题

时间:2018-10-02 00:40:43

标签: java network-programming udp

我正在尝试一个简单的问题,即通过UDP广播从服务器向客户端发送一些随机数。据我所知,如果我广播到特定的IP地址和端口号,则连接到该频道的所有用户都将能够收听。我已经在互联网上寻找示例代码,并以此为基础开发了我的代码。但是,每当我尝试运行代码时,我的服务器都会在客户端获得任何内容之前关闭套接字。

服务器代码为:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.security.SecureRandom;

public class Server {

  public static void main(String[] args) throws IOException {
      DatagramPacket packet;
      InetAddress address;
      DatagramSocket socket;

      System.out.println("Sending Numbers!");

      socket = new DatagramSocket();

      try {
          int n = 10;

          SecureRandom rand = new SecureRandom();
          address = InetAddress.getByName("233.0.0.1");

          for(int i = 0; i < n; i++)
          {
              int num = rand.nextInt(100);
              byte[] tmp = Integer.toString(num).getBytes();
              packet = new DatagramPacket (tmp, 0, address, 1502);
              socket.send(packet);
              System.out.println("Number has been sent!");
          }
      } catch (Exception e) {
          System.out.println("Error: " + e);
      } finally {
          try {
              socket.close();
          } catch (Exception e) {
              System.out.println("Error2: " + e);
          }
      }
  }
}

客户端代码为:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.ByteBuffer;

public class Client {

  public static void main(String[] args) throws IOException {
      // Initialization
      int n = 10;

      // Create the socket
      int port = 1502;
      DatagramSocket socket;
      DatagramPacket packet = null;

      socket = new DatagramSocket(port);

      for (int i = 0; i < n; i++)
      {
          socket.receive (packet);
          byte[] numb = packet.getData();
          int num = ByteBuffer.wrap(numb).getInt();
          System.out.println(Integer.toString(num));
      }
  }

}

然后,我尝试使用javac和java编译每个代码。

javac Server.java
java package_name.Server

我首先使用上述方法运行服务器,然后使用客户端。我得到客户端的NullPointerException。如果我了解自己在这里做错了,那将非常有帮助。

我正在命令窗口中运行它们。对于服务器,我得到了 Server 对于客户:Client

编辑:我能够在服务器和客户端之间进行通信。但是,我只是一直得到第一个数字。此外,如果将Datagram初始化为大于2的大小,则会得到NumberFormatException。这是改进的代码。

服务器代码:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.security.SecureRandom;

public class Server {
  public static void main(String[] args) throws IOException {
      DatagramPacket packet;
      InetAddress address;
      DatagramSocket socket;

      System.out.println("Sending Numbers!");

      socket = new DatagramSocket();

      try {
          int n = 10;

          SecureRandom rand = new SecureRandom();
          address = InetAddress.getByName("127.0.0.1");

          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          PrintStream pout = new PrintStream( bout );

          for(int i = 0; i < n; i++)
          {
              int num = rand.nextInt(100);
              pout.print(num);
              byte[] barray = bout.toByteArray();
              packet = new DatagramPacket (barray, barray.length, address, 1502);
              socket.send(packet);
              System.out.println("Number has been sent!");
              System.out.println(num);
          }
      } catch (Exception e) {
          System.out.println("Error: " + e);
      } finally {
          try {
              socket.close();
          } catch (Exception e) {
              System.out.println("Error2: " + e);
          }
      }
  }
}

客户代码:

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

public class Client {
  public static void main(String[] args) throws IOException {
      // Initialization
      int n = 10;

      // Create the socket
      int port = 1502;
      DatagramSocket socket;
      byte[] buf = new byte[2];
      DatagramPacket packet = new DatagramPacket(buf, buf.length);

      socket = new DatagramSocket(port);

      for (int i = 0; i < n; i++)
      {
          socket.receive (packet);
          int num = Integer.parseInt(new String(packet.getData()));
          System.out.println(num);
      }
  }
}

现在有什么问题?

编辑2:最后,我使其工作了。只需将boutpout放在for循环中,它应该可以工作!

1 个答案:

答案 0 :(得分:0)

您需要在客户端中初始化DatagramPacket-不能为空

     int n = 10;

      // Create the socket
      int port = 1502;
      DatagramSocket socket;
      byte[] buf = new byte[1000];
      DatagramPacket packet = new DatagramPacket(buf, buf.length);

      socket = new DatagramSocket(port);

      for (int i = 0; i < n; i++)
      {
          socket.receive (packet);
          byte[] numb = packet.getData();
          int num = ByteBuffer.wrap(numb).getInt();
          System.out.println(Integer.toString(num));
      } 

此代码还需要在您的Server

之前运行