简体   繁体   中英

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. What I would like to do is to listen to that particular IP and receive all the messages using a java program. Will SocketServer do the purpose??

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?

To listen to a specific address you have to create a ServerSocket like this

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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