I am making a program that uses socket to connect to others. When I send String or array of String it works fine. But when I try to send Objects over socket it doesnt work and EOFException appeared. I tried to find out by my own but its seem too hard for me!
Here is the input function in the Client: (I have many case in this function but they work fine so just the case has the problem)
public void run()
{
while(true)
{
try
{
int dataType = (int)in.readByte(); // this line is where the exception appeared. every works fine with another case
switch(dataType)
{
case 2:
{
try
{
int nRoom = (int)in.readByte(); // read the number of room are created
for (int i = 0; i < nRoom; i++)
{
Server.Room temp = (Server.Room) in.readObject();
nRooms[temp.ID] = temp;
}
}
catch (ClassNotFoundException ex)
{
System.out.println(ex.toString());
}
}
break;
}
}
catch (EOFException e)
{
System.out.println(e.toString());
}
catch (IOException ex)
{
System.out.println(ex.toString());
break;
}
}
}
Here is the outPut function in the Client:
private void createBtnClick( java.awt.event.ActionEvent evt )
{
try
{
out.writeByte(2);
out.flush();
}
catch (IOException ex)
{
System.out.println(ex.toString);
}
}
Here is the function the server take the input then write output to every Client ( its the user in the code below ):
public void run()
{
while(true)
{
try
{
int command = users[index].in.readByte();
switch ( command)
{
case 2:
// create the room
for (int i = 0; i < 5; i++)
{
if(nRooms[i] == null)
{
nRooms[i] = new Room(users[i].name,"Beginner", 1, i);
users[index].State = 1;
System.out.println("SV : create room ok " + i);
break;
}
}
for (int i = 0; i < 10; i++)
{
if(users[i] != null && i != index && ) // Send the room list to everybody except the room host!
{
try
{
users[i].out.writeByte(2);
int count = 0;
for (int j = 0; j < 5; j++)
{
if(nRooms[j] != null)
count++;
}
users[i].out.writeByte(count);
for (int j = 0; j < 5; j++)
{
if(nRooms[j] != null);
{
users[i].out.writeObject( nRooms[j]);
break;
}
}
//users[i].out.flush();
}
catch (IOException ex)
{
System.out.println(ex.toString());
}
}
}
break;
}
}
catch (IOException ex)
{
System.out.println(ex.toString());
users[index].in = null;
users[index].out = null;
users[index] = null;
break;
}
}
Room class is just a class that holds some infomation : (Define in Server)
public class Room implements Serializable
{
public String hostName;
public String gameMode;
public int nPlayers;
public int ID;
public Room(String hostName, String gameMode, int nPlayers, int ID)
{
this.hostName = hostName;
this.gameMode = gameMode;
this.nPlayers = nPlayers;
this.ID = ID;
}
}
Thanks!