简体   繁体   English

使用Java从特定端口和指定IP读取数据

[英]Read data from a particular port and specified IP using java

I would like to read the messages from a particular port.For example the IP is 1.2.3.4 and the port is 1000. Already the IP is used for receiving some messages. 我想从特定端口读取消息。例如,IP为1.2.3.4,端口为1000。该IP已用于接收某些消息。 What I would like to do is to listen to that particular IP and receive all the messages using a java program. 我想做的是使用Java程序监听该特定IP并接收所有消息。 Will SocketServer do the purpose?? SocketServer会达到目的吗?

ServerSocket ss = new ServerSocket(1000);
Socket socket = new Socket("1.2.3.4",1000);
socket = ss.accept();

Is it possible to read every contents that are being received by the particular IP and port? 是否可以读取特定IP和端口正在接收的所有内容?

To listen to a specific address you have to create a ServerSocket like this 要侦听特定地址,您必须创建一个ServerSocket,如下所示

ServerSocket ss = new ServerSocket(); // Unbound socket
ss.bind(new InetSocketAddress("1.2.3.4", 1000)); // Bind the socket to a specific interface
Socket client = ss.accept();

This way the server socket is bound to a specific network interface and will only receive incoming connections from it. 这样,服务器套接字将绑定到特定的网络接口,并且仅从该接口接收传入的连接。

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

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