TheRealJoeWilson
TheRealJoeWilson

Reputation: 19

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:

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?

Upvotes: 1

Views: 75

Answers (2)

Deepak Bhatia
Deepak Bhatia

Reputation: 6276

AFAIK there is no way that you can deny request without accepting it, (until and unless you have some reverse proxy setup like 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.

Upvotes: 1

Lucurious
Lucurious

Reputation: 174

It's unclear to me what you mean with "certain requests", however you can accept it, validate it and if it is not a "certain request" you simply drop the connection.

Upvotes: 1

Related Questions