Here it is. the networking I’ve been working on.
-client-
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import java.io.*;
import java.net.*;
import java.util.Vector;
import javax.swing.JOptionPane;
public class SlickClient extends BasicGame{
ClientThread ct;
Vector<Player> players;
Player me;
int ALL_KEYS = 0xFF;
boolean keys[];
public SlickClient()
{
super("Test Online Client - by William Starkovich");
}
public void init(GameContainer gc) throws SlickException {
try{
keys = new boolean[ALL_KEYS];
for(int i = 0; i < ALL_KEYS; i++){
keys[i] = false;
}
players = new Vector<Player>();
connect();
}
catch(Exception e){
}
}
public void connect(){
String ip = JOptionPane.showInputDialog("Input server IP.");
ct = new ClientThread(ip);
ct.start();
//ct.setPriority(Thread.MAX_PRIORITY);
me = ct.me;
players = ct.players;
}
public void update(GameContainer gc, int delta)throws SlickException{
controls();
players = new Vector<Player>();
System.out.println("ct size: " + ct.players.size());
me = ct.me;
players = ct.players;
}
public void render(GameContainer gc, Graphics g) throws SlickException{
g.setColor(Color.black);
g.fillRect(0,0,640,480);
for(int i = 0; i < players.size(); i++){
g.setColor(Color.cyan);
g.fillRect(players.get(i).x, players.get(i).y, 50, 50);
}
g.drawString("Players: " + players.size(), 50, 10);
}
public void keyPressed(int key, char c) {
keys[key] = true;
}
public void keyReleased(int key, char c) {
keys[key] = false;
}
public void controls(){
if(keys[Input.KEY_UP]){
me.y--;
}
else if(keys[Input.KEY_DOWN]){
me.y++;
}
else if(keys[Input.KEY_LEFT]){
me.x--;
}
else if(keys[Input.KEY_RIGHT]){
me.x++;
}
}
public static void main(String[] args) throws SlickException{
AppGameContainer app =
new AppGameContainer( new SlickClient() );
app.setShowFPS(false);
app.setAlwaysRender(true);
app.setTargetFrameRate(60);
app.setDisplayMode(800, 600, false);
app.start();
}
}
class ClientThread extends Thread implements Runnable{
Socket socket;
Vector<Player> players;
int playerID;
Player me;
DataOutputStream out;
ObjectInputStream in;
boolean loop = true;
@SuppressWarnings("unchecked")
public ClientThread(String ip){
super("ClientThread");
try{
players = new Vector<Player>();
socket = new Socket(ip, 4444);
socket.setTcpNoDelay(true);
out = new DataOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
playerID = in.readInt();
players = (Vector<Player>) in.readObject();
if(players != null)
System.out.println("Not Null: " + players.size());
boolean b = false;
for(int i = 0; i < players.size(); i++){
if(!b){
if(players.get(i).id == playerID){
me = players.get(i);
b = true;
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
while(loop){
try{
if(!socket.isClosed() && socket.isConnected()){
out.writeInt(me.x);
out.writeInt(me.y);
out.flush();
//players = new Vector<Player>();
players = (Vector<Player>) in.readObject();
System.out.println("size" + players.size());
sleep(15);
}
else
loop = false;
}
catch(Exception e){
e.printStackTrace();
socket.close();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
-Server-
import java.net.*;
import java.util.Vector;
import java.io.*;
import javax.swing.*;
public class SlickServer extends JFrame{
private static final long serialVersionUID = 1L;
JTextArea textArea;
JScrollPane scrollPane;
public SlickServer(){
super("Test Online Server - by William Starkovich");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 100);
setLocationRelativeTo(null);
textArea = new JTextArea();
scrollPane = new JScrollPane(textArea);
//getContentPane().add(textArea);
getContentPane().add(scrollPane);
}
public static void main(String[] args) throws IOException {
Vector<Player> player = new Vector<Player>();
SlickServer ss = new SlickServer();
ss.setVisible(true);
ServerSocket serverSocket = new ServerSocket(4444);
//serverSocket.setSoTimeout(10000);
boolean listening = true;
int playerID = 0;
while(listening){
ss.textArea.append("Waiting to connect with player: " + playerID + "\n");
new ClientThread(serverSocket.accept(), player, playerID, ss.textArea).start();
playerID++;
ss.textArea.append("Players: " + player.size() + "\n");
}
serverSocket.close();
System.exit(0);
}
}
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.Vector;
import java.io.*;
import javax.swing.*;
public class ClientThread extends Thread implements Runnable{
int playerID;
Socket acceptedSocket;
JTextArea textArea;
Vector<Player> players;
Player me;
public ClientThread(Socket acceptedSocket, Vector<Player> players, int playerID, JTextArea textArea){
super("ClientThread");
this.acceptedSocket = acceptedSocket;
this.players = players;
players.add(new Player(50,50, playerID));
if(players != null)
System.out.println("Not Null: " + players.size());
boolean b = false;
for(int i = 0; i < players.size(); i++){
if(!b){
if(players.get(i).id == playerID){
me = players.get(i);
b = true;
}
}
}
this.playerID = playerID;
this.textArea = textArea;
}
public void removeMeAndClose(){
boolean b = false;
for(int i = 0; i < players.size(); i++){
if(!b){
if(players.get(i).id == playerID){
players.remove(i);
b = true;
}
}
}
try{
acceptedSocket.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
public void run(){
try{
Socket clientSocket = acceptedSocket;
clientSocket.setTcpNoDelay(true);
textArea.append("Accepted. Now creating I/O.\n");
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
textArea.append("player connected. \n");
out.writeInt(me.id);
out.writeObject(players);
out.flush();
while(!clientSocket.isClosed() && clientSocket.isConnected()){
try{
me.x = in.readInt();
me.y = in.readInt();
//if(players != null)
//System.out.println("Not Null: " + players.size());
textArea.append("PlayerID: " + playerID + " Players: " + players.size() + " me.x: " + me.x + " me.y: " + me.y + "\n");
out.reset();
out.writeObject(players);
out.flush();
sleep(15);
}
catch(Exception e){
removeMeAndClose();
}
}
}
catch(Exception e){
removeMeAndClose();
}
}
}
import java.io.Serializable;
public class Player implements Serializable{
private static final long serialVersionUID = 1L;
int x;
int y;
int id;
public Player(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
}
Enjoy~!