When I use my server, if I get more than 2 clients connected onto it, I get massive lag. this is my socket.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* This file is part of the "Renoria" Game.
* Copyright (C) 2008
* IDGames.
*/
package renoria.net;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import renoria.Console;
import renoria.GameProperties;
import renoria.tools.HexTool;
import renoria.tools.data.input.ByteArrayByteStream;
import renoria.tools.data.input.GenericSeekableLittleEndianAccessor;
import renoria.tools.data.input.SeekableLittleEndianAccessor;
/**
*
* @author David
*/
public class PacketHandler implements GameProperties, Runnable {
private Socket sock;
private Thread runThread;
private DataOutputStream dos;
private DataInputStream dis;
private PacketProcessor processor;
public PacketHandler() throws IOException, Exception {
runThread = new Thread(this, "Packet Processor");
//runThread.setPriority(Thread.MAX_PRIORITY);
runThread.start();
processor = new PacketProcessor();
sock = new Socket(ip, port);
sock.setTcpNoDelay(true);
sock.setKeepAlive(true);
dos = new DataOutputStream(sock.getOutputStream());
dis = new DataInputStream(sock.getInputStream());
System.out.println("Packet processor started.");
}
@Override public void run() {
while(true) {
try {
if (sock.isInputShutdown() || sock.isOutputShutdown()
|| sock.isClosed() || !sock.isConnected()) {
Console.getWindow().showError("Connection Lost", "The connection with the server was lost.");
sock.close();
return;
}
byte[] data = new byte[1024];
byte[] rdata;
byte b;
int c = 0;
while ((b = dis.readByte()) != -1) {
data[c] = b;
c++;
}
rdata = new byte[c];
for (int i = 0; i < c; i++) {
rdata[i] = data[i];
}
SeekableLittleEndianAccessor slea = new GenericSeekableLittleEndianAccessor(new ByteArrayByteStream(rdata));
short packetId = slea.readShort();
if (processor.getHandler(packetId) != null) {
processor.getHandler(packetId).handlePacket(slea);
} else {
System.out.println("Got unhandled packet: " + HexTool.toString(rdata));
}
}catch (UnknownHostException e) {
e.printStackTrace();
} catch (Exception e) {
}
}
}
public Socket getConnection() {
return sock;
}
public void close() throws IOException {
sock.close();
}
public void write(RenoriaPacket packet) throws IOException {
if (packet.getOnSend() != null) {
packet.getOnSend().run();
}
byte[] b = packet.getBytes();
try {
for (int i = 0; i < b.length; i++) {
dos.writeByte(b[i]);
}
dos.writeByte(-1);
} catch (IOException ioe) {
}
System.out.println("Sent packet: " + HexTool.toString(packet.getBytes()));
}
public void write(byte[] b) throws IOException {
try {
for (int i = 0; i < b.length; i++) {
dos.writeByte(b[i]);
}
dos.writeByte(-1);
} catch (IOException ioe) {
}
}
}
