简体   繁体   English

Java在端口上侦听

[英]Java listening on port

We want to capture the data which comes to the system on port say 7777. 我们希望捕获端口上的系统数据7777。

public static void main(String[] args) {
        try {
            final ServerSocket serverSocket = new ServerSocket(7777);
            new Thread("Device Listener") {
                public void run() {
                    try {
                        System.out.println("Listener Running . . .");
                        Socket socket = null;
                        while ((socket = serverSocket.accept()) != null) {
                            System.out.println("| Incoming : "+ socket.toString());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                };
            }.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

We have a device which sends data to the port 7777, which comes with a native windows application. 我们有一个设备将数据发送到端口7777,它带有一个本机Windows应用程序。 The windows native application is receiving data which comes from that device. Windows本机应用程序正在接收来自该设备的数据。 We have to receive that data on port 7777 through our java project. 我们必须通过java项目在端口7777上接收数据。

In the above code, 在上面的代码中,

  1. The java server socket is created but no incoming connections are received from the device. 创建了Java服务器套接字,但没有从设备接收传入连接。

  2. The java server socket is receiving connections from telnet command. java服务器套接字正在从telnet命令接收连接。

  3. The data format which is used by the device and the other native application may be different, but atleast it has to be connected from java server socket. 设备和其他本机应用程序使用的数据格式可能不同,但至少必须从java服务器套接字连接。 is it correct? 这是正确的吗?

  4. how to receive the data which is transmitted to port 7777. 如何接收传输到端口7777的数据。

EDIT: 编辑:

Ok, the data is received with UDP socket. 好的,数据是通过UDP套接字接收的。 it is 68 in length. 它的长度是68。 The device documentation doesn't specify any methods to capture this data, because may be it is designed to work with the specified application. 设备文档未指定捕获此数据的任何方法,因为它可能旨在与指定的应用程序一起使用。 We can't contact the manufacturer also. 我们也无法联系制造商。 is there any way (if possible) to know the format of incoming bytes. 有没有办法(如果可能的话)知道传入字节的格式。 we have tried network sniffers but we cant understand the format. 我们尝试过网络嗅探器,但是我们无法理解这种格式。

If you're receiving from the telnet command, then I suspect you have a network-specific issue. 如果您从telnet命令接收,那么我怀疑您有特定于网络的问题。

  1. your device isn't talking to the same ip address / hostname that you're configuring telnet with 您的设备没有与您正在配置telnet的相同IP地址/主机名进行通信
  2. you have a routing or firewall issue 你有路由或防火墙问题
  3. is your device possibly using UDP rather than TCP ? 您的设备是否可能使用UDP而不是TCP?

The java server socket is created but no incoming connections are received from the device. 创建了Java服务器套接字,但没有从设备接收传入连接。

So either there is a firewall in the way or the device isn't trying to connect to that port. 因此,无论是在路上还是防火墙,或者设备没有尝试连接到该端口。

The java server socket is receiving connections from telnet command. java服务器套接字正在从telnet命令接收连接。

So the Java application is listening to that port. 所以Java应用程序正在侦听该端口。

The data format which is used by the device and the other native application may be different, but at least it has to be connected from java server socket. 设备和其他本机应用程序使用的数据格式可能不同,但至少必须从java服务器套接字连接。 is it correct? 这是正确的吗?

Yes. 是。

how to receive the data which is transmitted to port 7777. 如何接收传输到端口7777的数据。

First you have to accept the connection. 首先,你必须接受连接。 On the evidence here the device isn't connecting to port 7777 at all. 根据此处的证据,设备根本没有连接到端口7777。 I suggest some network sniffing is in order to see what it really is doing. 我建议一些网络嗅探是为了看看它到底在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM