Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServerSocket IP address?

 
Bec Bromley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am attempting to construct a firewall/packet filtering system under linux, and need it to recognise connections to a ServerSocket on a specified port. However, new ServerSockets are not assigned to a specific IP address, as they are always at a port on the local host. Linux iptables will not recognise the request however as there is no IP address.
So I was wondering......
Is there any way of creating a ServerSocket with an IP address?
thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bec,
Welcome to JavaRanch!
A quick look at the Javadoc for java.net.ServerSocket shows that there's a 3-argument constructor
ServerSocket(int port, int backlog, InetAddress bindAddr)
which lets you specify the local address to bind to.
 
Bec Bromley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes sorry about that, I should have looked first!! But still have a problem. This is now linux iptables-specific and I apologise, but if anyone can help it would be much appreciated!
I have set up iptables to accept inputs to the localhost for packets to/from port 200 by:
iptables -A INPUT -d localhost -p tcp --dport 200 -j ACCEPT
iptables -A INPUT -d localhost -p tcp --sport 200 -j ACCEPT
the default policy is set to DROP.
My Java application attempts to create a new ServerSocket on port 200 with the following:

but the packet doesn't get through. any ideas?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think getLocalHost() is particularly designed to look in your hosts file for the entry named "localhost" (traditionally this is associated with the loopback address 127.0.0.1). getLocalHost() will be trying to return the Internet address of the local machine by resolving the actual host name.
You could use
InetAddress addr = new InetAddress("127.0.0.1")
server = new ServerSocket(200,0,addr);
and that should work.
 
Bec Bromley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can create a new InetAddress using a string, you have to use one of the specified methods, which all return "localhost/127.0.0.1".
I've also tried casting a string to an InetAddress but that won't work either.
Hmm.....
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, you're right, there's no public constructor, you have to use the factory method InetAddress.getByName("127.0.0.1") .
But based on the display you're getting, it sounds like you're already getting the loopback entry. I'm not sure what the problem is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic