PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 21, 2022

[FIXED] How to create a Server Socket to which only a specific IP can connect

 April 21, 2022     connection, java, serversocket, sockets     No comments   

Issue

Is it possible to create a ServerSocket where instead of using the ServerSocket#accept method for any connection, the ServerSocket waits and accepts a connection only from a specific IP?

Or, alternatively, is it possible to know the IP of a connection before accepting it so that I can reject it if it is not from that specific IP?


Solution

No. The underlying communication stack does not support such a thing for TCP sockets.

The accept call itself returns a socket which can then be queried for the remote IP address. You can then immediately close the socket if the IP is not to your liking.

(By the way, what you accept is a 'connection', not a 'socket'. The socket is merely a local data structure, one at each endpoint; the socket is created on your machine in response to accepting a connection. It's the difference between your telephone and the conversation with the person at the other end.)



Answered By - user13784117
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing