Are you showing all of the code involved? Without code that compiles and executes (a SSCCE) it is hard to say.Quote:
is the way correct how im doing it now?
Printable View
Are you showing all of the code involved? Without code that compiles and executes (a SSCCE) it is hard to say.Quote:
is the way correct how im doing it now?
[Host - NetworkSend.java]
[Client - Client.java]Code:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class NetworkSend extends Thread{
int tempX,tempY,weapon,shooting,x,y;
char direction;
String[] line;
String conLine;
public boolean connected;
ServerSocket Server = null;
PrintStream os;
BufferedReader in;
Socket Client = null;
NetworkSend(int port){
try {
Server = new ServerSocket(port);
}
catch (IOException e) {
System.out.println(e);
}
}
public void connect(){
try {
Client = Server.accept();
camera.connected = true;
os = new PrintStream(Client.getOutputStream());
in = new BufferedReader(new InputStreamReader(Client.getInputStream()));
connected = true;
}
catch (IOException e) {
System.out.println(e);
}
}
public void run(){
System.out.println("Host started!");
while(camera.running){
System.out.println("Host sending...");
Send(camera.player_o.getX(),camera.player_o.getY(),camera.player_o.getDirection(),camera.player_o.selection,1);
System.out.println("Host send!");
update();
System.out.println("Host recieved!");
}
}
public void Send(int x, int y, char direction, int weaponType, int shoot){
os.println(","+x+","+y+","+direction+","+weaponType+","+shoot);
os.flush();
}
public void update(){
try {conLine = in.readLine();}
catch (IOException e) {e.printStackTrace();}
line = conLine.split(",");
x = Integer.parseInt(line[1]);
y = Integer.parseInt(line[2]);
direction = line[3].charAt(0);
weapon = Integer.parseInt(line[4]);
shooting = Integer.parseInt(line[5]);
System.out.println("X: "+x+" Y: "+y+" D: "+direction+" W: "+weapon+" S: "+shooting);
}
}
Some code to detirmine the host. Is still manually controlled:Code:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
//P,x,y,direction,weapon,shooting:
//P,50,50,'n',3,1
public class Client extends Thread {
int tempX, tempY, weapon, shooting, x, y;
char direction;
String[] line;
String conLine;
int port;
boolean running;
String ip;
String name;
Socket echoSocket;
PrintWriter out;
BufferedReader in;
int id;
String sendString;
String recieveString;
Client(String host, int port) {
camera.connected = true;
this.ip = host;
line = new String[6];
this.port = port;
x = 0;
y = 0;
}
public void run() {
System.out.println("Client thread started! connecting...");
while (camera.running) {
update();
System.out.println("Client info recieved!");
Send(camera.player_o.getX(), camera.player_o.getY(),
camera.player_o.getDirection(), camera.player_o.selection,
1);
System.out.println("Client info send!");
}
}
public void Send(int x, int y, char direction, int weaponType, int shoot) {
out.println(","+x + "," + y + "," + direction + "," + weaponType + ","+ shoot);
}
public void Connect() {
System.out.println("Attemping to connect to host " + ip + " on port " + port + ".");
try {
echoSocket = new Socket(ip, port);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + ip);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: " + ip);
System.exit(1);
}
//out.println(name);
}
public void update() {
try {conLine = in.readLine();}
catch (IOException e) {e.printStackTrace();}
try{
line = conLine.split(",");
x = Integer.parseInt(line[1]);
y = Integer.parseInt(line[2]);
direction = line[3].charAt(0);
weapon = Integer.parseInt(line[4]);
shooting = Integer.parseInt(line[5]);
}catch(NumberFormatException e){
System.out.println("ERROR PARSING "+conLine);
}
System.out.println("X: " + x + " Y: " + y + " D: " + direction + " W: " + weapon + " S: " + shooting);
}
}
in my paint eventCode:static void init(){
running = true;
if(host){
host_o = new NetworkSend(9988);
host_o.connect();
host_o.start();
}
else if(!host){
client_o = new Client("192.168.1.40",998);
client_o.Connect();
client_o.start();
}
}
I removed the getters and setters from the 2 connection classes, since that is not really necesary for this.Code:if(!camera.host){
g2d.fillRect(camera.client_o.getX()-camera.x, camera.client_o.getY()-camera.y, 8, 8);
}
if(camera.host)
g2d.fillRect(camera.host_o.getX()-camera.x, camera.host_o.getY()-camera.y, 8, 8);
The code works really good on 1 pc. But as soon as i launch the host on my pc first, And then the client on my laptop for example, it is VERY laggy! :(
Do i have to use UDP? or can i do it this way (tcp i believe?) with sockets? Also is there any way of speeding the code up?
If it works ok on one PC then you'll need someone with 2 PCs to test it.
Does The lagging only happen when you are using a network between PCs?
Ive tested it between my pc and another pc from within the network. And from outside the network. This was just imposible, such big lags everywhere!
Client.setTcpNoDelay(true);
Made it MUCH more responsive already. Still isn't totally smooth yet. also this is just with 2 players, Imagine with 8 players even!
Anybody any suggestions