Advertisement

Network Issue

Started by March 04, 2005 12:23 AM
4 comments, last by NickyP101 19 years, 11 months ago
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{
			Thread.sleep(0);
		}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;
		
			String read =  (String) input.readObject();
			System.out.println(read);
 
			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){
 
				x = (int)input.readInt();
				y = (int)input.readInt();
 
				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
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?
Advertisement
Quote:
Original post by NickyP101
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 *** 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...

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

So is turning Nagle's algorithm off bad practice? And is my design poor and should i redesign the networking part of it?
Quote:
Original post by NickyP101
So 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...

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

ok, so how often would u suggest updating?

This topic is closed to new replies.

Advertisement