Problem: Server drops all clients after any one currently connected clients drop without the close() method (unexpected drop / window closing/power outtage/ etc.).
I have played around with this so much but I just can't get this to work right. I even tried using isConnected() on the sockets, tried using exceptions to close the socket (but for some reason the socket closes its output too)
I know the error occurs when user A writes to user B (after B drops) because the streams are still open... but i dont know how to close them properly without effecting the remaining clients.
This is my first attempt at a client-server and i used some tutorials, googling and previous knowledge. (I dont know how to use this yet properly so I have not bothered with NIO yet, so I've stuck to the threads per connection method.)
if someone could fix this / show me a working similar type application - that would help also so i can find the differences. Right now Concurrency and Networking are my weak points.
perhaps it would help if I showed the code:
package server2;
import java.io.*;
import java.net.*;
public class Connector implements Runnable {
private ServerSocket server;
public static int numOfPlayers = 0;
public final static int MAX_CONNECTIONS = 10;
private static Player[] players = new Player[MAX_CONNECTIONS];
private DataInputStream[] input;
private DataOutputStream[] output;
public void run() {
try {
System.out.println("Initializing server");
server = new ServerSocket(5000);
System.out.println("Server started!");
while(true) {
System.out.println("Waiting for connection, currently there are "+numOfPlayers+" online.");
(players[numOfPlayers] = new Player(server.accept(), numOfPlayers, players)).start();
numOfPlayers++;
System.out.println("Connection Accepted.");
}
} catch(IOException e) {e.printStackTrace();}
}
}
package server2;
public class Main {
public static void main(String[] args) {
Thread server = new Thread(new Connector());
server.start();
while(true) {
// System.out.println(Connector.numOfPlayers);
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
}
}
package server2;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Player extends Thread {
public Socket socket;
private DataInputStream input;
private DataOutputStream output;
private Player[] players;
private int num;
String request;
Player(Socket socket, int num, Player[] players) {
this.socket = socket;
this.num = num;
this.players = players;
try {
this.input = new DataInputStream(this.socket.getInputStream());
this.output = new DataOutputStream(this.socket.getOutputStream());
} catch (Exception e) { }
}
public DataOutputStream out() {
return this.output;
}
public DataInputStream in() {
return this.input;
}
public void run() {
int i = 0;
try {
output.writeUTF("OKAY READY!");
while (true) {
request = input.readUTF();
// System.out.println("REQUEST:" + request);
// System.out.println("socket:" + this.socket);
if(request.startsWith("/quit")) {
break;
}
for(i = 0; i<=Connector.MAX_CONNECTIONS-1; i++){
if (players!=null && i != num){
System.out.println(this.num + " writing to :" + i);
players[i].output.writeUTF("<"+i+"> "+request + ""); // con:" + i + " Socket:"+ players[i].socket);
} else if (players[i]!=null && i == num) {
System.out.println(this.num + " writing to : itself");
players[i].output.writeUTF("<YOU> "+i +" "+request);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return;
}
}
}
edit: please excuse the random variables being public, i was playing with them while debugging. Also apologies for and terrible mess this code maybe in advance.
Edited by: Normik on May 25, 2010 4:20 PM