Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Client can connect to server on localhost, but not global IP?  (Read 8431 times)
0 Members and 1 Guest are viewing this topic.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #30 - Posted 2012-07-13 17:30:23 »

Well then I'm clueless Grin

Offline CyanPrime
« Reply #31 - Posted 2012-07-15 03:28:55 »

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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #32 - Posted 2012-07-15 04:35:17 »

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

My client's data thread, and my server's player thread are using LockSupport.parkNanos(1)

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



Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #33 - Posted 2012-07-15 04:51:17 »

I think I am using blocking IO.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
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);

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #34 - Posted 2012-07-15 10:18:32 »

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?
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. Tongue

Quote
Notes: My server runs as a JFrame

Why in the name of everything that is unholy would a server app run as a JFrame? Shocked

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline jonjava
« Reply #35 - Posted 2012-07-15 11:38:29 »

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.

Offline CyanPrime
« Reply #36 - Posted 2012-07-15 13:54:35 »

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?
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.
Offline 65K
« Reply #37 - Posted 2012-07-15 14:20:29 »

What kind of game is it, realtime, action ?

Offline CyanPrime
« Reply #38 - Posted 2012-07-15 14:31:00 »

What kind of game is it, realtime, action ?
I think real-time action describes it .
Offline jonjava
« Reply #39 - Posted 2012-07-15 14:53:37 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #40 - Posted 2012-07-15 15:20:56 »

Why is LockSupport.parkNanos(1); in there at all?  It serves no useful purpose I can determine, and it looks like something dropped in because someone told you to. 
Offline 65K
« Reply #41 - Posted 2012-07-15 15:32:48 »

What kind of game is it, realtime, action ?
I think real-time action describes it .
Welcome to the wonderful world of lag compensation, client side prediction, interpolation and related crap...
You can find various examples how to deal with that on the internet. You won't get through without as soon as you leave your cozy LAN.
I just can't find the source I used, maybe later.

Offline CyanPrime
« Reply #42 - Posted 2012-07-15 16:52:28 »

Alright, jumping into the next phase: I made a UDP version of my client/server, but the connection starts out slow, then speeds up if I hold down a movement button for a littlebit.

Copy pasted from my SO question:

Alright, so I got a game I'm working on (Right now just movement) and when I move the character it starts out slowly connecting to the server, but if I hold down a move button it speeds up to about half the localhost connection. I heard there's something in UDP Java that says the first 2000-20000 packets I send are going to be slow unless I send some JVM argument while running, like -X something?

Can someone tell me that JVM line argument? And/or why my connection starts out slow, and speeds up?


PS: should UDP be the same speed as TCP?
Online Riven
« League of Dukes »

« JGO Overlord »


Medals: 841
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #43 - Posted 2012-07-15 17:00:01 »

There is no 'connection' in UDP. There is no such thing as a slow IP packet either.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline CyanPrime
« Reply #44 - Posted 2012-07-15 17:05:16 »

There is no 'connection' in UDP. There is no such thing as a slow IP packet either.
Well, any chance I can send you my client and you can tell me whats going on?  Wink
Offline jonjava
« Reply #45 - Posted 2012-07-15 17:42:53 »

I'll just have to take a wild guess then.

You're sending way too many packets, half of which get lost along the way. Send less, compensate with guesstimated positions. Do some additional reading about sockets and familiarize yourself with the TCP and UDP protocols some more.

Try and create a simple server (f.ex an echo server ) before jumping into real-time action servers.

Offline CyanPrime
« Reply #46 - Posted 2012-07-15 18:02:03 »

I'll just have to take a wild guess then.

You're sending way too many packets, half of which get lost along the way. Send less, compensate with guesstimated positions. Do some additional reading about sockets and familiarize yourself with the TCP and UDP protocols some more.

Try and create a simple server (f.ex an echo server ) before jumping into real-time action servers.
That doesn't explain why it gets faster when I'm sending MORE packets though.
Offline jonjava
« Reply #47 - Posted 2012-07-15 18:10:07 »

The internet is quite a lively place. Node's are created and destroyed on the fly. Patterns emerge. Really, the internet is a crazy place. And that's the explanation. If you stick to local host you'd be oblivious to the erratic antics of the internet.

I suggest you just make it work over LAN first and worry about optimization over the internet (lag) later. In any case, real-time action is quite a handful.

What about trying to make a simple echo server first? Have you tried it? Should take only a handful of minutes and you can test it over telnet (command prompt).

[EDIT]: Also, I didn't link the article just for shits and giggles, it's a great article and I'm certain it's going to give everyone who reads it valuable insight. In fact, I'm sure this article was linked to me on this very forum at some point.

Offline CyanPrime
« Reply #48 - Posted 2012-07-15 18:18:33 »

The internet is quite a lively place. Node's are created and destroyed on the fly. Patterns emerge. Really, the internet is a crazy place. And that's the explanation. If you stick to local host you'd be oblivious to the erratic antics of the internet.

I suggest you just make it work over LAN first and worry about optimization over the internet (lag) later. In any case, real-time action is quite a handful.

What about trying to make a simple echo server first? Have you tried it? Should take only a handful of minutes and you can test it over telnet (command prompt).
It already works perfectly over lan, Why would I need to make a echo server? All the connections work, they're just laggy when not on localhost or 127, or LAN.
The logic works, and the movement works.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #49 - Posted 2012-07-15 18:29:31 »

Could be your VPS just isn't fast.  What are the specs for your virtual server, and is it Xen or OpenVZ or something else?
Offline jonjava
« Reply #50 - Posted 2012-07-15 18:30:28 »

I'm just trying to get a foothold on the problem. I'm concerned you might not be fully capable of tackling a real-time action game. Especially if it's your first server app. Then again, who is.

How many packets are the clients sending to the server per second? Is it once per game logic update? while key is being pressed? We don't have access to your protocol and we don't know how much bandwidth is used. We can't, at least I can't, begin to help find a solution to your lag problem if all we know is that it's "laggy".

TCP and UDP are two different beasts. A TCP packet has lots of fancy information that lets the TCP packet arrive at their destination IN ORDER they were sent. UDP are lightweight packets that basically only have a destination address. It neither checks or care weather it arrives at its destination or not. While TCP does multiple checks and confirms with the sender that data has been sent etc.

In general TCP is slower and more reliable and UDP is faster but unreliable. Please read the 6 page Article I linked earlier, it has some insights in the inner workings of TCP and UDP I'm sure you'd love to hear about.

Offline CyanPrime
« Reply #51 - Posted 2012-07-15 18:31:48 »

Could be your VPS just isn't fast.  What are the specs for your virtual server, and is it Xen or OpenVZ or something else?

Xen    CentOS 5 64bit    256 MB    10 GB
Offline sproingie

JGO Kernel


Medals: 202



« Reply #52 - Posted 2012-07-15 19:06:30 »

Yeah that should be pretty good, and with available memory that low it means it's probably not overselling RAM.  It's still possible for there to be congestion beyond your control though, which is why measuring the latency of an echo server would be appropriate, or some other kind of stub server that does nothing but send fake traffic with the same sort of packets your game would.  There's a lot of variables here, you have to tackle them one at a time.
Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #53 - Posted 2012-07-16 01:41:30 »

I'll just have to take a wild guess then.

You're sending way too many packets, half of which get lost along the way. Send less, compensate with guesstimated positions. Do some additional reading about sockets and familiarize yourself with the TCP and UDP protocols some more.

Try and create a simple server (f.ex an echo server ) before jumping into real-time action servers.
That doesn't explain why it gets faster when I'm sending MORE packets though.
1  
socket.setTCPNoDelay(true);


Nagle's algorithm.

Myomyomyo.
Offline CyanPrime
« Reply #54 - Posted 2012-07-16 01:49:53 »

I'll just have to take a wild guess then.

You're sending way too many packets, half of which get lost along the way. Send less, compensate with guesstimated positions. Do some additional reading about sockets and familiarize yourself with the TCP and UDP protocols some more.

Try and create a simple server (f.ex an echo server ) before jumping into real-time action servers.
That doesn't explain why it gets faster when I'm sending MORE packets though.
1  
socket.setTCPNoDelay(true);
A good read, but I'm afraid I'm using UDP right now. I switched my protocol in hopes of it being faster.

Nagle's algorithm.
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #55 - Posted 2012-07-17 19:21:32 »

I read an excellent explanation of a real time UDP networking on the Steam site. Something about the source engine networking capabilities. Anyone still have that link?

Pages: 1 [2]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (59 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!