wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes ServerSocket IP address? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "ServerSocket IP address?" Watch "ServerSocket IP address?" New topic
Author

ServerSocket IP address?

Bec Bromley
Greenhorn

Joined: Nov 21, 2003
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Bec Bromley
Greenhorn

Joined: Nov 21, 2003
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Nov 21, 2003
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
 
subject: ServerSocket IP address?
 
Similar Threads
Socket Question
ethernet-to-serial
how to connect to other machine rather than localhost.
accessing a server using socket connection
Socket problem