# Network Issue

This topic is 4915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok here is the problem, im making a network game, the server seems to be sending the data out perfectly, but the client thread isnt reading properly, im not quiet sure wat the problem is, ill explain with an example below and then paste some of my code; Ok The game every frame sends the current users x,y co-ords using the update method in the ClientIO thread, this works fyn, the server recieves the info almost instantly and then sends it to the other players all most instantly. Problem is it taks upto 10 seconds for the other player to recieve the new positions. I cant tell whether this is because ClientIO is a thread and therefor is not running, or if there is some other problem. When the client IO is recieving data is with recieve lyk 20 values then pause for lyk 10 seconds then recieve etc etc Below is some of the code: Java
Game loop:

public void game(){

while(true){
updateWorld();
io.updatePosition(playerX + imgCropx1, playerY + imgCropy1);
paintWorld();
strategy.show();

try{
}catch(Exception e){System.out.println(e);}

}

}


Java
import java.io.*;
import java.net.*;

public class ClientIO extends Thread{

Socket connection;
int playerNumber;

ObjectOutputStream output;
ObjectInputStream input;

boolean alive;

int opX;
int opY;

public ClientIO(){

playerNumber = 0;
init();

this.start();

}

public void init(){

try{
connection = new Socket(InetAddress.getByName("192.168.0.6"), 5000);
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();

input = new ObjectInputStream(connection.getInputStream());

}catch(Exception e){System.out.println(e);}

}

public void run(){

try{
alive = true;

while(true){
System.out.println("Waiting For Values");
opX = (int) input.readInt();
System.out.println("Recieved Opposition X = " + opX);
opY = (int) input.readInt();
System.out.println("Recieved Opposition Y = " + opY);
}

}catch(Exception e){System.out.println(e);}

}

public void updatePosition(int x, int y){

try{
System.out.println("Sending");
output.writeInt(x);
output.writeInt(y);
output.flush();
System.out.println("Sent");

}catch(Exception e){System.out.println(e);}
}

}
[/Source]
Java
import java.io.*;
import java.net.*;
import java.util.*;

public class Server{

ServerSocket servsock;
Socket sock;
ServerIO io;
int playerNumber;

Set players;

public Server(){runServer();}

public void runServer(){

try{
players = new HashSet();
playerNumber = 0;
servsock = new ServerSocket(5000, 110);

while(true){

System.out.println("Waiting For Connection");
sock = servsock.accept();
System.out.println("Connection Accepted");
players.add(new ServerIO(sock, playerNumber, this));

playerNumber = playerNumber + 1;
}

}
catch(Exception e){System.out.println(e);}

}

public void updateAll(int x, int y, int playerNum){

try{

Iterator i = players.iterator();

while(i.hasNext()){
io = (ServerIO) i.next();
if(io.playerNumber == playerNum){}
else{

io.output.writeInt(x);
io.output.writeInt(y);
System.out.println("Sent To " + io.playerNumber + x + " " + y);

}

}

}
catch(Exception e){System.out.println(e);}

}

}

import java.io.*;
import java.net.*;
import java.util.*;

public class ServerIO extends Thread{

Socket connection;
int playerNumber;

ObjectOutputStream output;
ObjectInputStream input;

ServerIO io;

Server main;

int x, y;

boolean alive;

Set players;

public ServerIO(Socket sock, int x, Server s){

main = s;

connection = sock;
playerNumber = x;
init();
this.start();

}

public void init(){

try{

output = new ObjectOutputStream(connection.getOutputStream());
output.flush();

input = new ObjectInputStream(connection.getInputStream());

}catch(Exception e){System.out.println(e);}

}

public void run(){

try{
alive = true;

output.writeObject("Connection Complete");

while(alive){

main.updateAll(x, y, playerNumber);

}

}catch(Exception e){System.out.println(e);}

}

public void update(int x, int y, int playerNum){

}

}

[/Source]
Can u see any obvious problems? Any help would be greatly appreciated! Cheers, Nick

##### Share on other sites
I just implemented Thread.getState() into the game loop and its is always RUNNABLE, does this mean thew thread is pretty much always running meaning its a network issue?

##### Share on other sites
Quote:
 Original post by NickyP101Ok here is the problem, im making a network game, the server seems to be sending the data out perfectly, but the client thread isnt reading properly, im not quiet sure wat the problem is, ill explain with an example below and then paste some of my code;Ok The game every frame sends the current users x,y co-ords using the update method in the ClientIO thread, this works fyn, the server recieves the info almost instantly and then sends it to the other players all most instantly. Problem is it taks upto 10 seconds for the other player to recieve the new positions. I cant tell whether this is because ClientIO is a thread and therefor is not running, or if there is some other problem. When the client IO is recieving data is with recieve lyk 20 values then pause for lyk 10 seconds then recieve etc etcBelow is some of the code:Java *** Source Snippet Removed *** Java *** Source Snippet Removed ***[/source]Java *** Source Snippet Removed ***[/source]Can u see any obvious problems? Any help would be greatly appreciated!Cheers, Nick

Well, first off, do you need to send an update every frame? You could just send a vector and an orientation to the server when it changes. Otherwise assume status quo. Secondly, sounds like the Nagle's algorithm is getting you. Make sure you call setTcpNoDelay(true) on your socket. That will disable Nagle's algorithm and thus the socket will send data immediately.

Of course, Nagle probably won't be happy with you...

##### Share on other sites
So is turning Nagle's algorithm off bad practice? And is my design poor and should i redesign the networking part of it?

##### Share on other sites
Quote:
 Original post by NickyP101So is turning Nagle's algorithm off bad practice? And is my design poor and should i redesign the networking part of it?

Well, i think sending an update every frame is excessive. So yes, I would recommend redesigning that.
As far as Nagle goes, I'm sure he will probably be disapointed, but since he doesn't know where you live, it shouldn't be much of a problem...

##### Share on other sites
ok, so how often would u suggest updating?

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632154
• Total Posts
3004472

×