简体   繁体   English

Java套接字编程-请求

[英]Java socket programming - requests

I am writing a messaging application in java using sockets and currently the server will wait for a request and just automatically accept it: 我正在使用套接字在Java中编写消息传递应用程序,当前服务器将等待请求并自动接受该请求:

ServerSocket s = new ServerSocket(1254);
Socket s1 = s.accept();

If there any way in which I can detect requests to the server and allow the server to only accept certain requests? 是否可以通过某种方式检测到对服务器的请求并允许服务器仅接受某些请求?

我不清楚您对“某些请求”的含义,但是您可以接受它,对其进行验证,如果不是“某些请求”,则只需断开连接。

AFAIK there is no way that you can deny request without accepting it, (until and unless you have some reverse proxy setup like nginx). AFAIK不能不接受就拒绝请求(直到,除非您有一些像nginx这样的反向代理设置)。

After accepting the socket you can check out the various parameters for example Socket's remote address (check for more info java.net.Socket ), and close the socket if you don't want the connection to be established. 接受套接字后,您可以检出各种参数,例如Socket的远程地址(检查更多信息java.net.Socket ),如果不想建立连接,则关闭套接字。

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

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