Client can connect to server on localhost, but not global IP?

Portforwarding on a router is for some reason a very brittle technology. I’ve had about 8 routers, and they all had nice web-interfaces that allowed you to setup portforwarding. Two of those eight actually managed to forward traffic…

You’re better off renting a cheap VPS with little to no RAM, and using it solely to setup a reverse (SSH) tunnel: your desktop will connect to the VPS using TCP, and the outbound connection is used to pump traffic from the outside world to your desktop.

Strange, when I disable port forwarding I get a connection refused with the client right away. Why doesn’t it do that with port forwarding on? It’s not connecting to the server ???.

probably a firewall in between, or you picked the wrong local IP, or made any other mistake in the config. it’s hard to diagnose without information. the serversocket might be listening on 127.0.0.1 instead of your LAN IP.

what about specifying the local IP: new ServerSocket(InetAddress.getByAddress(“10.0.0.x”), 50, 44444)

I’ve had experiences like this with setting up a Minecraft server and I heard that you can’t connect to your global IP from your own router. Because technically, the router is sending the data to itself

What I think you need to do is get a friend to try and connect and see if it works for the reason above

Someone correct me if I’m wrong.

Alright, I tried the normal code, then

new ServerSocket(InetAddress.getByAddress("10.0.0.x"), 50, 44444)

. Both after turning off windows defender, all firewalls (router, and PC) and putting the router into all ports open mode.

Same results.
With port forwarding enabled I got a timeout after some time, and without port forwarding I got connection refused.

Thank you though, that was a good idea. :smiley:

@HeroesGraveDev sproingie already make an attempt from another machine

Maybe your ip address is wrong. Try connecting to this ip http://www.whatismyip.com/.

Didn’t see that.

Just checking it wasn’t a facepalm-level failure

CyanPrime, did you literally use ‘x’ in your code for that LAN IP address? You are supposed to replace it with the actual value. I’m just checking for all possibilities here ;D

Had I done that I would have gotten a error, right? :wink:

Well then I’m clueless ;D

Well, I fixed it kinda, my VPS can run the server now, and my client can connect to my VPS. PS: Guess it wasn’t a code problem after all

Now to deal with lag. I’m copy and pasting a question I had from SO because they all suck now days, and gave me no good answers, or even tried.

alright, so I have a client on my PC, and a server on a VPS I bought, and I’m getting quite a bit of lag, so I was wondering what are some simple tricks to reduce TCP lag in Java? And if thats not enough, what are some more advanced tricks I can use?

Notes: My server runs as a JFrame, and creates a new thread for each client connecting (needed for my client-server project is a simple game) and my client uses LWJGL, and has a different thread for sending/receiving data. My client’s data thread, and my server’s player thread are using LockSupport.parkNanos(1);, and TCPDelay is set to false on both sides of the player’s connection.

Have you tested whether you get the same lag on a single thread, or that it’s in fact network lag?

Why? If you’re spinning up a thread per client, why not simply use blocking I/O?

I think I am using blocking IO.


if(parent.playerID != -1){
				byte[] sentBytes = new byte[2];
				sentBytes[0] = parent.playerID;
				sentBytes[1] = parent.myState;
				try {
					//System.out.println("sentBytes[0]" + sentBytes[0] + " sentBytes[1]" + sentBytes[1] ); 
					parent.toServer.write(sentBytes);
				} catch (IOException e) { e.printStackTrace(); }
			}
			
			byte[] recievedBytes = new byte[100];
			
			try {parent.fromServer.read(recievedBytes); }
			catch (IOException e) { e.printStackTrace(); }
			
			ByteBuffer bb = ByteBuffer.wrap(recievedBytes);
			bb.order(ByteOrder.LITTLE_ENDIAN);
			byte[] header = new byte[4];
			for(int i = 0; i < header.length; i++){
				header[i] = bb.get();
			}
			
			if(header[0] == 50 && header[1] == 100 && header[2] == 60 && header[3] == 90)
				parent.getPacket = true;
			
			if(parent.getPacket){
				
				parent.playersConnected.clear();
			
				byte numConnected = bb.get();
				byte flag = bb.get();
				byte packetPlayerID = bb.get();
				
				int psize = (parent.packetSize * numConnected) + 10;
				
				while(bb.position() < psize){
					
					byte  tempID = bb.get();
					byte  tempState = bb.get();
					float tempX = bb.getFloat();
					float tempY = bb.getFloat();
					parent.playersConnected.add(new Player(tempID, tempX, tempY, tempState));
				}
				
				
				parent.playerID = packetPlayerID;
				Player me = parent.playersConnected.get(parent.playerID);
				parent.debugStr = parent.messages[flag] + " " + numConnected + " " + packetPlayerID + " " + me.pos.x + " " + me.pos.y + " " + me.state;
				
				
				parent.getPacket = false;
				firstLoop = false;
			}
			
			
			LockSupport.parkNanos(1);

What makes you think it’s TCP causing the lag? How long is this lag and how does it compare to the ping between the two? Have you tried running both the client and the server locally to see how much lag that has?

Trying to optimise your network throughput while running your two ends at different places on the internet is madness, so don’t try to do that. :stuck_out_tongue:

[quote]Notes: My server runs as a JFrame
[/quote]
Why in the name of everything that is unholy would a server app run as a JFrame? :o

Have you tried making a simple echo server before attempting this?

How are you measuring the lag?

Instead of running the server as a JFrame, don’t. If you want to have the server display info on things you can make an admin account that you connect through with the client and have the client show the info as a JFrame or something similar.

Yes, sir. When I run client and server locally there is no lag. Therefore it must be the TCP connection from my client to my VPS Server.

What kind of game is it, realtime, action ?

I think real-time action describes it .

How many packets are the client sending per second? If it’s real-time action, you 99.99999999(9) % of the time want to go with UDP.

This is because if a TCP packet is missed, the connection will HALT and trace back and re-send the missed packet. Which, in most (all) real-time action games you wouldn’t care about.

Read this article: http://www.gamasutra.com/view/feature/131781/the_internet_sucks_or_what_i_.php

Could you answer my previous questions about how you’re measuring the lag and have you tried running a simple echo server before on your VPS? If the echo server lags, it’s definitely a problem with your VPS. If not, it’d give us insight on what might be the problem with your protocol.