Java 通过套接字发送多个对象时发生EOFEException
我正在制作一个程序,使用套接字连接到其他程序。当我发送字符串或字符串数组时,它工作正常。但当我试图通过套接字发送对象时,它不起作用,出现了EOFEException。 我试着自己找出答案,但对我来说太难了 以下是客户端中的输入函数: (我在这个函数中有很多case,但它们工作得很好,所以只有case有问题) 以下是服务器获取输入,然后将输出写入每个客户机的函数(以下代码中的用户):Java 通过套接字发送多个对象时发生EOFEException,java,arrays,sockets,eofexception,Java,Arrays,Sockets,Eofexception,我正在制作一个程序,使用套接字连接到其他程序。当我发送字符串或字符串数组时,它工作正常。但当我试图通过套接字发送对象时,它不起作用,出现了EOFEException。 我试着自己找出答案,但对我来说太难了 以下是客户端中的输入函数: (我在这个函数中有很多case,但它们工作得很好,所以只有case有问题) 以下是服务器获取输入,然后将输出写入每个客户机的函数(以下代码中的用户): 谢谢 为什么users.out.flush被注释掉了?我会说冲洗小溪是个好主意。哦,因为我离开了,但也没用。我认为
谢谢 为什么users.out.flush被注释掉了?我会说冲洗小溪是个好主意。哦,因为我离开了,但也没用。我认为这是问题所在,但不是!
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;
}
}
}
private void createBtnClick( java.awt.event.ActionEvent evt )
{
try
{
out.writeByte(2);
out.flush();
}
catch (IOException ex)
{
System.out.println(ex.toString);
}
}
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;
}
}
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;
}
}