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