简体   繁体   中英

Block socket connection for one port in JAVA

因此,基本上我希望我的serversocket.accept()可以用于除1218之外的任何其他端口。我可以阻止从该端口进行的调用,因为1218必须连接到另一个线程。

I have good news and bad news. The good news is that you don't have to do anything special to avoid listening on a particular port. The bad news is that's because any given ServerSocket listens for connections on only one port.

The port is how clients identify which of possibly many services they want to connect to. Some services listen for connections on multiple ports, but they need a separate socket for each one. It doesn't make sense for a server to listen on every port (nor either on all but one).

All you need to do is specify a port number different from 1218 when you bind your ServerSocket (whether via a constructor or via a separate bind() ).

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