I'm trying to make a Client/Server program that uses tcp java connection and im testing it on the same machine (both Server and client) using localhost IP and port 12345.
I use ObjectInputStream and ObjectOutputStream to receive and send messages. Sometimes my program works perfect, it send the right messages as i want, but sometimes (in random time/stage of the program) one of my client "loses" the connection and he gets into an infinite loop printing "null" or "invalid type code:54" without doing any code changes.
Im getting no errors just the server prints "Connection reset" in loop and the client the "null" or "invalid type code:54" from the catch (IOException e).
I will show you just the basic of my Code.
server:
public static void main(String[] args) {
//--------BASIC CODE used for connection stuff below----//
server = new ServerSocket(12345);
connection =server.accept();
Thread thread =new Thread(new ClientThread(connection,GRM...));
thread.start();
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
}
--------------//---------thread for each client----------//--------------
do{
try{
message=(String) input.readObject();
if(message.startsWith("SOMENTHING"))//...some tcode
if(message.equals("SEND")){
String message="hi";
output.writeObject("Chat:"+message);output.flush();
UserW.writeObject(message);UserW.flush();
//UserW is an output from another client stored in the server
}
catch(ClassNotFoundException classNotFoundException){
System.out.print("\nUnknown object");
}catch (IOException e) {
System.out.println(e.getMessage());
}
}while(!message.equals("DISCONNECT"));
Client:
public static void main(String[] args) {
connection=new Socket(InetAddress.getByName("127.0.0.1"),12345);
//...... same logic with streams......//
}
//-------thread that receives from server-----//
do{
try{
String message=(String) input.readObject();
//.....some code....//
}catch( ClassNotFoundException classNotFoundException){
System.out.print("\nUnknown object");
}catch (IOException e) {
System.out.println(e.getMessage());
}
}while(true);