JAVA UDP 服务器无法接收数据包 [英] JAVA UDP Server Can't receive Packet

查看:59
本文介绍了JAVA UDP 服务器无法接收数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码,如下所示,套接字绑定到 IP 10.10.88.11 和端口 9876.我使用wireshark 测试了两种条件,如下所示.两台 PC 都在同一子网中.

I have a sample code as below and the socket is bound to IP 10.10.88.11 and port 9876. I tested with the 2 conditions with wireshark as below. Both PCs are in the same subnet.

  1. 从同一台电脑 (10.10.88.11) 发送 UDP 数据包 - UDP 服务器能够接收
  2. 从另一台电脑 (10.10.88.10) 发送 UDP 数据包 - UDP 服务器无法接收但 Wireshark(在 10.10.88.11)能够捕获数据包

我已经在互联网上搜索过,但找不到解决方案.我在创建 InetScoketAddress 时做错了什么吗?

I have searched the internet but can't find a solution for this. Is there anything i did wrong in creating the InetScoketAddress?

import java.io.*;
import java.net.*;

public class UDPServer {
    public static void main(String args[]) throws Exception {        

     InetSocketAddress address = new InetSocketAddress("10.10.88.11", 9876);

     DatagramSocket serverSocket = new DatagramSocket(address);
        byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];
        while(true)
           {
              DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
              System.out.println("Waiting to receive");
              serverSocket.receive(receivePacket);
              String sentence = new String( receivePacket.getData());
              System.out.println("RECEIVED: " + sentence);
              InetAddress IPAddress = receivePacket.getAddress();
              int port = receivePacket.getPort();
              String capitalizedSentence = sentence.toUpperCase();
              sendData = capitalizedSentence.getBytes();
              DatagramPacket sendPacket =
              new DatagramPacket(sendData, sendData.length, IPAddress, port);
              serverSocket.send(sendPacket);
           }

  }
}

推荐答案

我相信 Wireshark 能够在数据包被防火墙评估之前抓取数据包,这意味着您将检测到它们,但它们永远不会到达 Java 应用程序.您是否尝试停用防火墙?

I believe Wireshark is able to grab packets before they are evaluated by the firewall, meaning that you will detect them but they will never reach the java app. Did you try deactivating your firewall ?

这篇关于JAVA UDP 服务器无法接收数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆