一个UDP测试server和client的连接,为什么服务端上打印不出客户端发过来的字符串? 
帮我看看,谢谢了
这是服务端:
import java.net.*;public class TestUDPServer
{
public static void main(String args[]) throws Exception
{
byte buf[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while(true)
{
ds.receive(dp);
System.out.println(new String(buf,0,dp.getLength()));
}
}
}
这是客户端import java.net.*;public class TestUDPClient
{
public static void main(String args[]) throws Exception
{
byte[] buf = (new String("Hello")).getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
}
}

解决方案 »

  1.   

    我这控制台下测试没问题.服务端可以接收到hell
      

  2.   

    程序是在win7 上写的,运行后客户端正常, 服务端没反应,可能是服务端 ds.receive(dp)没有接受到数据.我在Xp上测试也没问题这是怎么一回事? 
      

  3.   

    可以参考下:
    import java.io.*;
    import java.net.*;public class OneServer {  
      // 选择一个端口
      public static final int PORT = 8080;
      //main方法
      public static void main(String[] args) throws IOException {
        //在localhost的8080端口建立一个用于守听得ServerSocket
        ServerSocket s = new ServerSocket(PORT,1,InetAddress.getByName("localhost"));
        //打印开始守听的消息
        System.out.println(s);
        System.out.println("开始守听......");
        //开始工作
        try {
          // ServerSocket的accept方法用来接受Client的连接,一旦收到。就建立Socket连接
          Socket socket = s.accept();
          //对socket进行操作
          try {
    //打印出已经连接的消息
            System.out.println("通过"+ socket+"与Client建立连接");
    //建立引用in用于读入socket的数据
            BufferedReader in = 
              new BufferedReader(
                new InputStreamReader(
                  socket.getInputStream()));

            //建立引用out用于向socket输出数据
            PrintWriter out = 
              new PrintWriter(
                new BufferedWriter(
                  new OutputStreamWriter(
                    socket.getOutputStream())),true);

            while (true) {
      //从in(即socket)读入数据  
              String str = in.readLine();
      //如果数据为“END”则结束
              if (str.equals("END")) break;
      //打印以收到的数据
              System.out.println("收到来自Client的数据:" + str+", 并且向Client回发。");
              //向out(即Socket)发送数据
      out.println(str+"(Server回发)");
            }
          } finally {
            //关闭socket
    System.out.println("连接关闭...");
            //socket.close();
          }
        } finally {
          //结束守听
          s.close();
        }
      } 
    } import java.net.*;
    import java.io.*;public class OneClient {
      public static void main(String[] args) throws IOException {
        //获得本机地址    
        InetAddress addr = InetAddress.getByName("localhost");
        System.out.println("Client地址:" + addr);
        //建立一个指向8080端口的socket连接
        Socket socket = new Socket(addr, 8080);
        //也可使用 Socket socket = new Socket(addr, OneServer.PORT);
        
        try {
          System.out.println("通过" + socket+"与Server建立连接");      //建立引用in用于读入socket的数据
          BufferedReader in =
            new BufferedReader(
              new InputStreamReader(
                socket.getInputStream()));
          
          //建立引用out用于向socket输出数据
          PrintWriter out =
            new PrintWriter(
              new BufferedWriter(
                new OutputStreamWriter(
                  socket.getOutputStream())),true);
          
          //建立一个从屏幕读入数据的引用keyin
          BufferedReader keyin =
            new BufferedReader(
              new InputStreamReader(
                System.in));
          //循环交互,直到敲入END
          while(true){
    //从屏幕获得字符
    String s=keyin.readLine();
    //向out(即Socket)发送数据
            out.println(s);
    //判断是否结束连接
    if (s.equals("END")) break;
    //从in(即socket)读入数据
    String str = in.readLine();
            //将读入的数据打印到屏幕
    System.out.println("Server的回复是: "+str);
          }
        } finally {
          //关闭连接
          System.out.println("关闭连接");
          socket.close();
        }
      }

      

  4.   

    我就是在win7上册的,没问题 - - ;
      

  5.   

    我也觉得是win7的问题,Server端的程序执行到 ds.receive()后就一直等待接收数据,我看Java API文档里写的  “If there is a security manager, a packet cannot be received if the security manager's checkAccept method does not allow it.“
    这里的 security manager 应该是JVM 里的吧。应该不是这个问题。
    3楼给的那个程序是TCP的,ServerSocket, 和Socket在我机器上测试没问题,我估计就是DatagramSocket接收不到包。Win 7 有没有什么安全设置阻止接收UDP packet的? 刚装的WIn7 还不太会用 -,-