Java multi client game server - java

I am having trouble making a multi client server for a game that i have made. I am able to create a ServerSocket, make a connection and send data through means of a ObjectInputStream and ObjectOutputStream. My problems lies when it comes to multiple clients, from what i understand a new thread needs to be created for each client but the data i would like to send and receive is hard coded into the client class. How would I allow my Client to send changeable data?
Here is my client class:
public class Client extends Thread {
private Server server = new Server();
private Socket connection;
private ObjectOutputStream out;
private ObjectInputStream in;
public Client(Socket connection) {
this.connection = connection;
this.out = server.objOutStream(this.connection);
this.in = server.objInStream(this.connection);
}
public void run() {
while(this.connection.isConnected()) {
//
List<String> list = new ArrayList<String>();
list.add("hello there");
server.sendData(out, list);
//
List<String> recive = new ArrayList<String>();
recive = server.reveiveData(in);
}
}
public Socket getConnection() {
return connection;
}
}
and Here is the server side:
public ServerSocket createServer(Integer serverPort, Integer serverSize) {
ServerSocket server = null;
try {
server = new ServerSocket(serverPort, serverSize);
System.out.println("created server");
} catch (IOException e) {
e.printStackTrace();
}
return server;
}
public Socket makeConnection(ServerSocket server) {
Socket connection = null;
try {
connection = server.accept();
Client client = new Client(connection);
client.start();
System.out.println("connection made with " + connection.getInetAddress().getHostAddress());
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
public ObjectInputStream objInStream(Socket connection) {
ObjectInputStream in = null;
try {
in = new ObjectInputStream(connection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
public ObjectOutputStream objOutStream(Socket connection) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(connection.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
public void sendData(ObjectOutputStream out, List<String> list) {
try {
out.writeObject(list);
out.flush();
System.out.println("sending " + list);
} catch (IOException e) {
e.printStackTrace();
}
}
public List<String> reveiveData(ObjectInputStream in) {
List<String> list = null;
try {
list = (List<String>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return list;
}
public Socket joinServer(String serverIP, Integer serverPort) {
Socket connection = null;
try {
connection = new Socket(InetAddress.getByName(serverIP), serverPort); System.out.println("joined");
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
public void stop(Socket connection) {
System.out.println("closing");
try {
if(connection.isInputShutdown() == false) {
connection.getInputStream().close();
}
if(connection.isOutputShutdown() == false) {
connection.getOutputStream().close();
}
if(connection.isConnected() == true) {
connection.close();
}
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
}
In my mian class I run this:
try {
if(connection == null) {
connection = server.makeConnection(serverSocket);
in = server.objInStream(connection);
out = server.objOutStream(connection);
}else {
//readlist
List<String> readList = (List<String>) in.readObject();
System.out.println("reciving: " + readList);
//writelist
List<String> writeList = new ArrayList<String>();
writeList.add("hi");
out.writeObject(writeList);
System.out.println("sending: " + writeList);
}
}catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}

Related

Java how to read with ObjectInputStream

It's my first time working with sockets, in order to get a better understanding of what's going on I decided to build a client server chat application which can support several users.
At first, I used DataInputStream / DataOutputStream to communicate and everything works well. But I would like to switch to an ObjectStream and that's where the problem occurs. Once I replace all the DataInputStream / DataOutputStream by ObjectInputStream / ObjectOutputStream, I'm no longer able to print the retrieved data.
This is the code that I used before, which works (DataStream) :
SERVER:
try {
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("HI FROM SERVER");
while (!socket.isClosed()) {
try {
if (in.available() > 0) {
String input = in.readUTF();
for (ClientThread thatClient : server.getClients()){
DataOutputStream outputParticularClient = new DataOutputStream(thatClient.getSocket().getOutputStream());
outputParticularClient.writeUTF(input + " GOT FROM SERVER");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
CLIENT:
try {
socket = new Socket("localhost", portNumber);
DataInputStream in = new DataInputStream(socket.getInputStream());
new Thread(()->{
while(!socket.isClosed()){
try {
if (in.available() > 0){
String input = in.readUTF();
System.out.println(getUserName() + " > " + input);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
And this is how I tried to perform the same idea with ObjectStream :
SERVER:
try {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
while (!socket.isClosed()) {
try {
if (in.available() > 0) {
Message input;
try {
input = (Message)in.readObject();
if (input.equals(null)){
System.err.println("SERVER RETRIEVED NULL OBJECT");
}
for (ClientThread thatClient : server.getClients()){
ObjectOutputStream outputParticularClient = new ObjectOutputStream(thatClient.getSocket().getOutputStream());
outputParticularClient.writeObject(input);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
CLIENT:
try {
socket = new Socket(getHost(), portNumber);
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
new Thread(()->{
while(!socket.isClosed()){
try {
if (in.available() > 0){
Message input = null;
try {
input = (Message)in.readObject();
if (input.equals(null)){
System.err.println("CLIENT RETRIEVED NULL OBJECT");
}
System.out.println("CLIENT " + input.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
I feel like it has something to do with this if statement if (in.available() > 0) but I cannot say precisely what's going on.
available() doesn't do what you may think it does and it is almost never useful in production code (and that's particularly true for ObjectInputStream). The reason you don't receive any data is in fact that in.available() always returns 0 as you already suspected.
As noted in the comments, the StreamCorruptedException is caused by writing to an existing ObjectInputStream that has already been written to using another instance of ObjectOutputStream. Cf. the answer StreamCorruptedException: invalid type code: AC for further explanation.
Here is some quick & dirty example code that has a server echoing the messages from two clients. It's not clean but it may give you an idea how to approach your problem:
public class SO56493162 {
private static final class Message implements Serializable {
private static final long serialVersionUID = 1L;
private static int cnt = 0;
private final int id;
public Message(int id) {
++cnt;
this.id = id;
}
public String toString() {
return "Msg from " + id + " : " + cnt;
}
}
private static final class Client implements Runnable {
private InetSocketAddress addr = null;
private int id = -1;
Client(InetSocketAddress addr, int id) {
this.addr = addr;
this.id = id;
}
public void run() {
int timeout = 3000;
Socket s = null;
try {
s = new Socket();
s.connect(addr, timeout);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
System.out.println("Client " + id + " connected");
while (true) {
Thread.sleep(new Random().nextInt(2000));
Message hello = new Message(id);
oos.writeObject(hello);
oos.flush();
Message reply = (Message) ois.readObject();
System.out.println("Reply: " + reply.toString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (Exception ignore) {
}
}
}
}
private static final class Server implements Runnable {
private ServerSocket sock = null;
Server(ServerSocket sock) throws IOException {
this.sock = sock;
}
public void run() {
System.out.println("starting server");
try {
while (true) {
final Socket client = sock.accept();
System.out.println("connection accepted");
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
while (!client.isClosed()) {
try {
Message input = (Message) ois.readObject();
oos.writeObject(input);
oos.flush();
} catch (EOFException eof) {
System.err.println("EOF!");
client.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.setDaemon(true);
t.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException, InterruptedException {
final int port = 9876;
Thread ts = new Thread(new Runnable() {
#Override
public void run() {
try {
new Server(new ServerSocket(port)).run();
} catch (Exception e) {
e.printStackTrace();
}
}
});
ts.setDaemon(true);
ts.start();
InetSocketAddress addr = new InetSocketAddress("localhost", port);
for (int i = 0; i < 2; ++i) {
Client cl = new Client(addr, i);
Thread tc = new Thread(cl);
tc.setDaemon(true);
tc.start();
}
Thread.sleep(10000);
System.err.println("done");
}
}

Java Sockets sending multiple objects to the same server

I'm trying to send multiple Objects through a socket to a java server.
To have a gerneral type I convert my messages into an instance of the class Message and send this object to the server.
I wrote a little testclass, which sends three objects to the server.
The problem is, only one objects reaches the server.
I tried nearly everything, without success.
My Server:
public class Server {
private ServerConfig conf = new ServerConfig();
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
Logger log = new Logger();
ServerSocket socket;
Chat chat = new Chat();
public static void main(String[] args) {
Server s = new Server();
if (s.runServer()) {
s.listenToClients();
}
}
public boolean runServer() {
try {
socket = new ServerSocket(port);
logToConsole("Server wurde gestartet!");
return true;
} catch (IOException e) {
logToConsole("Server konnte nicht gestartet werden!");
e.printStackTrace();
return false;
}
}
public void listenToClients() {
while (true) {
try {
Socket client = socket.accept();
ObjectOutputStream writer = new ObjectOutputStream(client.getOutputStream());
Thread clientThread = new Thread(new Handler(client, writer));
clientThread.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void logToConsole(String message) {
System.out.print(message);
}
public class Handler implements Runnable {
Socket client;
ObjectInputStream reader;
ObjectOutputStream writer;
User user;
public Handler(Socket client, ObjectOutputStream writer) {
try {
this.client = client;
this.writer = writer;
this.reader = new ObjectInputStream(client.getInputStream());
this.user = new User();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
while (true) {
Message incomming;
try {
while ((incomming = (Message) reader.readUnshared()) != null) {
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
logToConsole(
"Vom Client: \n" + incomming.getType() + "-----" + incomming.getValue().toString());
handle(incomming);
}
} catch (SocketException se) {
se.printStackTrace();
Thread.currentThread().interrupt();
} catch (IOException ioe) {
ioe.printStackTrace();
Thread.currentThread().interrupt();
} catch (ClassNotFoundException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
private void handle(Message m) throws IOException {
String type = m.getType();
if (type.equals(config.ConstantList.Network.CHAT.toString())) {
chat.sendMessage(m);
} else if (type.equals(config.ConstantList.Network.LOGIN.toString())) {
System.out.println(user.login(m.getValue().get(0), writer));
System.out.println(m.getValue().get(0));
}
}
}
}
The Client:
public class Connect {
Socket client = null;
ObjectOutputStream writer = null;
ObjectInputStream reader = null;
private Config conf = new Config();
//private String host = conf.loadProp("ServerIP");
String host = "localhost";
private int port = Integer.parseInt(conf.loadProp("ServerPort"));
public boolean connectToServer() {
try {
client = new Socket(host, port);
reader = new ObjectInputStream(client.getInputStream());
writer = new ObjectOutputStream(client.getOutputStream());
logMessages("Netzwerkverbindung hergestellt");
Thread t = new Thread(new MessagesFromServerListener());
t.start();
return true;
} catch (Exception e) {
logMessages("Netzwerkverbindung konnte nicht hergestellt werden");
e.printStackTrace();
return false;
}
}
public boolean isConnectionActive() {
if (client == null || writer == null || reader == null){
return false;
}else{
return true;
}
}
public void sendToServer(Message m) {
try {
writer.reset();
writer.writeUnshared(m);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
And I try to send the objects with the class:
public void sendChatMessage(String username, String message) throws InterruptedException {
ChatMessage cm = new ChatMessage();
cm.setChat(username, null, message);
Message m = new Message(cm);
conn.sendToServer(m);
System.out.println("SENDED");
}
public static void main(String[] args) throws InterruptedException {
String username = "testuser";
String chatmessage = "Hallo Welt!";
connect.connect();
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
sendChatMessage(username, chatmessage);
}
I know that this is always the same message, but it is only for test purposes.
The messages are the objects they are Serializable and with only one object it works as designed.
Does anyone can see where I made my mistake?
while ((incomming = (Message) reader.readUnshared()) != null) {
Here you are reading an object, and blocking until it arrives.
logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n");
Here you are reading another object, and blocking till it arrives, and then erroneously logging it as the object you already read in the previous line.
Instead of logging reader.readObject(), you should be logging the value of incoming, which you have also misspelt.
And the loop is incorrect. readObject() doesn't return null at end of stream: it throws EOFException. It can return null any time you write null, so using it as a loop termination condition is completely wrong. You should catch EOFException and break.
Found the solution, the line logToConsole("Vom Client: \n" + reader.readObject().toString() + "\n"); in the Server class, blocks the connection.

Broken pipe error - while existing client terminate and new client join to ther server

I am getting the broken pipe error while the client stopping the build process and new client connect with server and sending message to the server.
Here my Server code:
public class Server {
ArrayList<ObjectOutputStream> ListOfclientOutputStreams;
public Server() {
ListOfclientOutputStreams = new ArrayList<ObjectOutputStream>();
try {
ServerSocket serverSocket = new ServerSocket(7503);
while(true) {
Socket serverCommunicationSocket;
serverCommunicationSocket = serverSocket.accept();
ObjectOutputStream eachClientobjectOutStream = new ObjectOutputStream(serverCommunicationSocket.getOutputStream());
ListOfclientOutputStreams.add(eachClientobjectOutStream);
Thread threadForSpecificClient = new Thread(new SpecificClient(serverCommunicationSocket));
threadForSpecificClient.start();
System.out.println("Got a connection");
}
}catch(IOException ex) {
ex.printStackTrace();
}
}
private class SpecificClient implements Runnable {
private ObjectInputStream eachClientObjectInputStream;
private Socket sock;
public SpecificClient(Socket specificSocket) {
try {
sock = specificSocket;
eachClientObjectInputStream = new ObjectInputStream(sock.getInputStream());
}catch (IOException ex) {
ex.printStackTrace();
}
}
public void run() {
Message msgObject;
try {
while((msgObject = (Message) eachClientObjectInputStream.readObject()) !=null) {
System.out.println("Client thread on the server read" + msgObject);
broadCast(msgObject);
}
}catch (IOException ex) {
ex.printStackTrace();
}catch(ClassNotFoundException ex){
System.out.println("Unable to read the message object in server");
ex.printStackTrace();
}
}
public void broadCast(Message msgObject) {
Iterator<ObjectOutputStream> iter = ListOfclientOutputStreams.iterator();
try {
while(iter.hasNext()) {
ObjectOutputStream out = iter.next();
out.writeObject(msgObject);
out.flush();
}
}catch(IOException ex) {
System.out.println("Unable to add the Message object to the outputstream in the server");
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
Server server1 = new Server();
}
}
This is my client coding:
public class Client {
private ObjectOutputStream clientOutStream;
private ObjectInputStream clientInStream;
private Socket clientSocket;
public Client() throws InterruptedException {
establishConnection();
createStreams();
Thread threadToRead = new Thread(new Incoming());
threadToRead.start();
}
public void establishConnection() {
try {
int port = Integer.parseInt(Session.port);
clientSocket = new Socket("127.0.0.1", port);
System.out.println("Network connection Established");
}catch (IOException ex) {
ex.printStackTrace();
}
}
public void createStreams() {
try {
clientInStream = new ObjectInputStream(clientSocket.getInputStream());
clientOutStream = new ObjectOutputStream(clientSocket.getOutputStream());
System.out.println("Connection streams established");
}catch (IOException ex) {
ex.printStackTrace();
}
}
private class Incoming implements Runnable {
public void run() {
String personIn;
Message msgObject = new Message();
try
{
personIn=personLoggedIn.getText().toString();
while((msgObject= (Message) clientInStream.readObject()) !=null)
{
//updateWhoIsOnline();
System.out.println("Client is Reading the object from the server");
String postedTo = msgObject.getPostedTo().replace(",", "").replace("[", "").replace("]", "");
System.out.println("Posted To : "+postedTo);
String[] result = postedTo.split(" ");
int len = result.length;
System.out.println("Length : "+len);
System.out.println("First string : "+result[0].toString());
//System.out.println("Second string : "+result[1].toString());
for(int i=0;i<len;i++) {
if(result[i].toString().equals("All") || result[i].toString().equals(personIn) || msgObject.getPostedBy().equals(personIn)) {
chatDetailsViewArea.append(msgObject.getPostedBy() + " ->" + result[i].toString() + ":" + msgObject.getContent() + "\n");
}
}
}
}
catch(IOException ex)
{
System.out.println("Error in figuring out who to post the message to");
ex.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
}
}
}

Java socket client connection and disconnection issue

I made this script:
public class Server {
ServerSocket serv = null;
ObjectInputStream in = null;
ObjectOutputStream out = null;
Socket conn = null;
public Server() {
setLogger(getClass());
setupSocketServer();
listen();
}
public void listen() {
try {
while (true) {
conn = serv.accept();
getLogger().log(new LogRecord(Level.INFO, "Connection established from: " + conn.getInetAddress().getHostAddress()));
out = new ObjectOutputStream(conn.getOutputStream());
in = new ObjectInputStream(conn.getInputStream());
}
}
catch (IOException ex) {
getLogger().log(new LogRecord(Level.SEVERE, "Connection dropped from: " + conn.getInetAddress().getHostAddress()));
}
}
public void setupSocketServer() {
try {
serv = new ServerSocket(Config.PORT_NUMBER, Config.MAX_CONNECTIONS);
getLogger().log(new LogRecord(Level.INFO, "Starting Server on: " + serv.getInetAddress().getHostAddress() + ":" + serv.getLocalPort()));
}
catch (IOException e) {
getLogger().log(new LogRecord(Level.SEVERE, "Socket can not connect to host address"));
System.exit(0);
}
}
public static void main(String[] args) {
new Server();
}
}
But whenever I open my client connection, then close it again and try to re-open, the server has already closed out. I want to be able to keep an infinite connection which allows multiple people to connect. How would I go about doing this?
Try this code for your server,
its made up for multiple client, and the server will remain listening always.
public class ServerTest {
ServerSocket s;
public void go() {
try {
s = new ServerSocket(44457);
while (true) {
Socket incoming = s.accept();
Thread t = new Thread(new MyCon(incoming));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class MyCon implements Runnable {
Socket incoming;
public MyCon(Socket incoming) {
this.incoming = incoming;
}
#Override
public void run() {
try {
PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
true);
InputStreamReader isr = new InputStreamReader(
incoming.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inp = null;
boolean isDone = true;
System.out.println("TYPE : BYE");
System.out.println();
while (isDone && ((inp = br.readLine()) != null)) {
System.out.println(inp);
if (inp.trim().equals("BYE")) {
System.out
.println("THANKS FOR CONNECTING...Bye for now");
isDone = false;
s.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ServerTest().go();
}
}
Move try/catch block into 'while' loop. Not that it' will make a goot server, bit should survive client disconnects.

StreamCorruptedException: invalid type code: 00

Server code:
while (true) {
Socket sock = serv.accept();
try {
new ClientSession(sock, outQueue, activeSessions);
System.out.println("CS");
} catch (IOException e) {
System.out.println("Sock error");
sock.close();
}
}
ClientSession:
class ClientSession extends Thread {
private Socket socket;
private OutboundMessages outQueue;
private ActiveSessions activeSessions;
private ObjectInputStream netIn;
private ObjectOutputStream netOut;
int n = 0;
boolean inGame = false;
boolean ready = false;
Player p;
public ClientSession(Socket s, OutboundMessages out, ActiveSessions as)
throws IOException {
socket = s;
outQueue = out;
activeSessions = as;
netOut = new ObjectOutputStream(socket.getOutputStream());
netOut.flush();
netIn = new ObjectInputStream(socket.getInputStream());
System.out.println("ClientSession " + this + " starts...");
while (true) {
Object nameMsg = null;
try {
nameMsg = netIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (nameMsg instanceof NameMessage) {
this.setName(((NameMessage) nameMsg).name);
break;
}
}
start();
}
public void run() {
try {
activeSessions.addSession(this);
while (true) {
Object inMsg = null;
try {
try {
inMsg = netIn.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
if (inMsg instanceof ReadyMessage) {
ready = true;
} else if (inMsg instanceof DirMessage) {
p.setDir(((DirMessage)inMsg).dir);
}
}
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
public void sendMessage(Message msg) {
try {
if (!socket.isClosed()) {
netOut.writeObject(msg);
netOut.flush();
} else {
throw new IOException();
}
} catch (IOException eee) {
try {
socket.close();
} catch (IOException ee) {
}
}
}
Creating input and output on client side:
public void connect() {
try {
InetAddress serverAddr = InetAddress.getByName(serverName);
try {
System.out.println("Connecting with "
+ serverAddr.getHostName() + ":" + port);
socket = new Socket(serverAddr, port);
try {
System.out.println("Connected to "
+ serverAddr.getHostName());
netOut = new ObjectOutputStream(socket.getOutputStream());
netOut.flush();
netIn = new ObjectInputStream(socket.getInputStream());
netOut.writeObject(new NameMessage(name));
netOut.flush();
} finally {
}
} catch (ConnectException e) {
System.out.println("Cannot connect to server");
} catch (IOException e) {
System.out.println("Input error");
}
} catch (UnknownHostException e) {
System.out.println("Unknown server: " + e.getMessage());
}
}
receiver on client end:
public void run() {
while (true) {
Object a = null;
try {
a = netIn.readObject();
netIn.reset();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (a != null && a instanceof CoordMessage) {
setXY((CoordMessage)a);
}
}
}
Stacktrace:
java.io.StreamCorruptedException: invalid type code: 00
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at twoPlayerClient.Receiver.run(Receiver.java:28)
After creating input and output I keep passing them on to other classes and not creating new ones.
I have read other similar questions but can't find an answer to why this keeps happening.
new ClientSession(sock, outQueue, activeSessions);
I think, there is a new session for each client, and so you cannot use a stream variable, with global scope. Since its used by other session threads also.

Categories

Resources