Works peachy on a lan across any distance though it gets ridiculously laggy. I used the blocking approach because I do not come close to understanding the non-blocking approach no matter how much I read about it. In fact I didn’t read anything at all on nonblocking, which is probably why my code is laggy, but I was still able to figure it out.
My last post was far too long so Ill ahve to give it in bits and pieces. Here are what I think are the most important two classes.
SocketAction - socket wraper
import java.io.*;
import java.net.*;
public class SocketAction extends Thread {
private BufferedReader inStream = null;
protected PrintStream outStream = null;
private ObjectOutputStream oos = null;
private ObjectInputStream ois = null;
private Socket socket = null;
public SocketAction(Socket sock) {
super("SocketAction");
try {
inStream = new BufferedReader(new InputStreamReader(sock.getInputStream()));
outStream = new PrintStream(new
BufferedOutputStream(sock.getOutputStream(), 1024), true);
oos = new ObjectOutputStream(sock.getOutputStream());
ois = new ObjectInputStream(sock.getInputStream());
socket = sock;
}
catch (IOException e) {
System.out.println("Couldn't initialize SocketAction: " + e);
System.exit(1);
}
}
public void send(Object o) throws IOException {
oos.writeObject(o);
oos.reset();
}
public void send(String s) {
outStream.println(s);
}
public String receive() throws IOException {
return inStream.readLine();
}
public Object receiveObject() throws IOException, ClassNotFoundException {
return ois.readObject();
}
public boolean isConnected() {
return ((inStream != null) && (outStream != null) && (socket != null));
}
protected void finalize () {
if (socket != null) {
try {
socket.close();
}
catch (IOException e) {
System.out.println("Couldn't close socket: " + e);
}
socket = null;
}
}
public void closeConnections() {
try {
socket.close();
socket = null;
}
catch (IOException e) {
System.out.println("Couldn't close socket: " + e);
}
}
public void reset() {
try {
inStream.reset();
}
catch (IOException e) {
}
}
}
Message - sent object
import java.io.*;
public class Message implements Serializable {
private String title;
private String message;
private Object data;
public Message(String title, String message) {
this.title = title;
this.message = message;
}
public Message(String title, String message, Object data) {
this(title, message);
this.data = data;
}
public String getTitle() {
return title;
}
public String getMessage() {
return message;
}
public Object getData() {
return data;
}
}
HostListener - clients communication with host
import java.io.*;
import java.net.*;
public class HostListener extends Thread {
private SocketAction host;
private boolean running = true;
private String name;
public HostListener(String name) {
this.name = name;
}
public boolean connectTo(String ip) {
try {
host = new SocketAction(new Socket(ip, 64652));
}
catch (UnknownHostException e) {
e.printStackTrace();
return false;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
send("init:" + name);
start();
return true;
}
public void run() {
try {
while (running) {
Object o = host.receiveObject();
processObject(o);
}
}
catch (IOException e) {
System.err.println(e);
}
catch (ClassNotFoundException e) {
System.err.println(e);
}
}
public void processMessage(String message) {
}
public synchronized void processObject(Object o) {
Message message = (Message)o;
if (message.getTitle().equals("state")) {
GameManager.INSTANCE.adjustWorld(message.getMessage());
}
else if (message.getTitle().equals("new")) {
GameManager.INSTANCE.getWorld().addSprite((Sprite)message.getData());
}
else if (message.getTitle().equals("remove")) {
GameManager.INSTANCE.getWorld().removeSpriteForID(Integer.parseInt(message.getMessage()));
}
}
public void send(String s) {
host.send(s);
}
}
Any point in the right direction would be appriciated
To retain message order and reduce thread creation overhead, you night want to create one dedicated worker thread and insert the messages into a processing queue.