This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Sockets and Internet Protocols and the fly likes Socket Programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket Programming" Watch "Socket Programming" New topic
Author

Socket Programming

Oduabuna Francis
Greenhorn

Joined: Jun 21, 2013
Posts: 2

Hi.
Is there a way in which a I can program a server to be the one to pickup requests from a client (whose IP address and port is stated) instead of the client forwarding the requests to it. I know for normal sockets I would have to create a server as follows:

What if I want the server to be the one to pick a request from a client of IP address x.x.x.x on port 8081, what should I do?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

Oduabuna Francis wrote:Hi.
Is there a way in which a I can program a server to be the one to pickup requests from a client (whose IP address and port is stated) instead of the client forwarding the requests to it. I know for normal sockets I would have to create a server as follows:

What if I want the server to be the one to pick a request from a client of IP address x.x.x.x on port 8081, what should I do?



Not sure what you mean by "pick a request from a client of IP address x.x.x.x on port 8081"? Are you asking to see if it can only accept from certain addresses and port? If so, then no -- not really... but ... after accepting, I guess you can check the remote address and port, and close the socket if it is not within the range that you want. Does that work?

If this is not what you mean, then please elaborate.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

Oduabuna Francis wrote:Hi.
Is there a way in which a I can program a server to be the one to pickup requests from a client (whose IP address and port is stated) instead of the client forwarding the requests to it. I know for normal sockets I would have to create a server as follows:

What if I want the server to be the one to pick a request from a client of IP address x.x.x.x on port 8081, what should I do?


If you need accept all Sockets(Clients) you need to use Thread.

If you need to accept specific client use


You should be able to handle Exceptions
Hope this helps,
Supun


Are you better than me? Then please show me my mistakes..
Oduabuna Francis
Greenhorn

Joined: Jun 21, 2013
Posts: 2

Thanks Supun Lakshan. I wanted to ensure it only accept from certain addresses and port. I used what you suggested and it did help. Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Socket Programming