I was testing out a networking script of mine, and it worked fine locally. It is basically a replica of a very bare bones multiplayer game network, where multiple clients connect to a master server, each client can say anything that it wants, and the server redirects that message to all the other clients it has. This is multithreaded and it runs prefectly locally, I can make any number of connections that I want. My problem begins when I tell my friend to connect. I know everything is fine on the port because he can ping my IP address just fine and there is no loss of packets. I try and use port 3000, and when my friend tries and connect, he just cannot. Does anyone know why this happens?

Recommended Answers

All 21 Replies

bump really need answer please :(

Are you by any chance spawing the server process by passing in the host as "localhost" or "127.0.0.1"? If yes, then AFAIK you need to replace the same with the IP address assigned by your network in case your friend is in the same network or the IP address assigned by your ISP in case he isn't.

Edit: Don't bump posts, at least not within 24 of posting your post. It'll just make you look demanding/impatient and will detract those trying to help you out.

Thanks for the reply lol, but that is not possible because I am just creating a ServerSocket like this:

ServerSocket server = new ServerSocket(3000);

There is no IP address that it is bound to, hence it should be open to any connection on that port correct?

The problem isn't with port but with the host. If you don't pass in a host, it defaults to 0.0.0.0 or 127.0.0.1 (not very sure here); which again gives rise to the scenario I explained in my previous post. Refer the three arg constructor of ServerSocket class for more details.

Oh ok, thanks for the help sorry for the late reply too, there is only ONE constructor with an InetAddress, and when I pass my IP address throguh InetAddress.getName("WAN IP");, it crashes the server, any reason this occurs, if you need the code, I can post :)

"Crashes the server" isn't a very good description. Post the relevant code along with complete stack trace. Also, you don't pass in your WAN IP but *your* IP (assigned by your ISP or your network).

Oh ok, so you dont pass the IP that you would get on say IPchicken or whatsmyip.org, but the NETWORK IP, like 192.168.1.3 ? Also, here is the code excerpt:

private ServerSocket Server;
 

   public static void main(String argv[]) throws Exception {
     new VarnaServer();
   }

   public VarnaServer() throws Exception {
	 System.out.println("Starting Server...");
	 System.out.println("Creating Server Socket");
     Server = new ServerSocket(3000, 50, InetAddress.getByName("network IP"));
     System.out.println("Server Socket Created");
     this.start();
   }

The exception is the following:

Exception in thread main java.net.BindException: Cannot assign requested IP Address .

I obviously just put network IP on this forum because in the actual code I put my real WAN IP

Use getByAddress instead of getByName since you are passing an IP address and not a host name.

byte[] ipBytes = new byte[] { 192, 192, 192, 192 };
InetAddress ip = InetAddress.getByAddress(ipBytes);
ServerSocket server = new ServerSocket(9090, -1, ip);
while(true) {
  Socket client = server.accept();
  // do stuff
}

Ahh, ok I use get by bytes now as well, but It still doesn't work, I still get the BindException, I put my real ip into the Byte array as well here is the code:

public VarnaServer() throws Exception {
	 System.out.println("Starting Server...");
	 System.out.println("Creating Server Socket");
     //Server = new ServerSocket(3000, 50, InetAddress.getByName(""));
	 byte[] ipBytes = new byte[] {0,0,0,0};
	 InetAddress ip = InetAddress.getByAddress(ipBytes);
	 System.out.println(ip.getHostAddress());
	 Server = new ServerSocket(3000, 100, ip);
	 
	 System.out.println("Server Socket Created at: " + Server.getInetAddress().getHostAddress());
     this.start();
   }

It works at my place so it obviously has got something to do with the IP address you are using. Also, try a higher port value, like 8080, to make sure the problem isn't with lower ports being blocked. Also, like I said, post the *entire* stack trace unmodified and not just the first line.

How could i post the entire stack trace, Here is the current error:
Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at VarnaServer.<init>(VarnaServer.java:21)
at VarnaServer.main(VarnaServer.java:12)

> How could i post the entire stack trace,

By copy pasting the entire error trace you see in your console. Anyways, have you tried out the suggestions from my previous post? If it still doesn't work, there is nothing I can do from here since this might very well be a firewall/OS configuration issue/any-other-thing.

Well after some googling, I found out that Java by default uses ipv6, and the address I am giving is ipv4, is that really the issue here? Also it cannot be my port or the IP because my friend can ping me just fine, by my knowledge, isn't that correct? Thank you!

I got it to work, my code was perfect from the start, I used a different method from the first place anyways, you dont need to bind it to a specific IP address or InetAddress or anything, all that was wrong was that my ISP was blocking home servers. I sent the (non-working) server to a friend with a different ISP, and just connected to HIS wan ip, it worked beautifully, thanks for the help anyways!

As an experiment, ask your friend to use his IP address when starting the ServerSocket; does it still work?

nope, no offence but it is impossible to BIND to your WAN ip address :D, it still fails :D (my friend knows a lot about networking), but honestly s.o.s thank you for the help man, I still very much appreciate it :D:D

Err.. sorry, but it's not impossible. Binding to the IP assigned by your network as well as the face IP assigned by your ISP works, *always*. The problem has got something to do with the IP that was being used or local settings/firewall settings enforced by your ISP/OS.

Anyways, good thing it worked out well for you. Good luck. :-)

Thank you, oddly enough my friend told me and I learned ( the hard way ) that you cannot bind the server socket to the IP you would get when you go to ipchicken or whatsmyip.org . I tried it too, it doesn't work, my friend tried it, it didn't work. Maybe the IP that I am talking about is different than the one your talking about?

The IP you get from whatsmyip.org is called the "face" IP or external IP. Normally this is the IP of your ISP's router which acts as a mediator between you and the internet hence binding to that IP fails. You can of course bind to any IP shown when you fire off the "ipconfig" command.

public VarnaServer() throws Exception   {
        System.out.println("Starting Server...");
        System.out.println("Creating Server Socket");
        server = new ServerSocket(3000, 50, InetAddress.getLocalHost());
        System.out.println("Server Socket Created at: " + server.getInetAddress().getLocalHost());
        this.start();
    }

I know it is marked as solved, however you can also use InetAddress.getLocalHost() to return the address.

Yup that is what I use now, but thanks for the hint anyways, it can help someone that is searching the forum :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.