菜鸟的EOFException

内部类BlockDataInputStream.的方法peekByte抛出的异常,意思就是到达了文件的末尾,程序却没有正常结束读取文件内容


Client:
  streamToServer.println(userName+";"+strPwd);  //向Server发送name与password;
  String frmServer = (String)streamFromServer;  //Client接收Server返回的信息;
  if(frmServer.equals("Welcome"))
  {
     new ClientInt(userName);   //start the chat screen;
     this.dispose();
  }

Server:
  msg = streamFromClient.readLine();     //接收Client的登录信息
  int ver = verify(msg);                 //验证信息从txt文件中读
  if(ver==1)
  {
     String colon = new String(";");
     int index = ((String)msg).lastIndexOf(colon);
     String userName = (String)msg.substring(0,index);
     if(!(vctrList.indexOf((String)userName)>0))
     {
       streamToClient.writeObject("Welcome");
       streamToClient.flush();
       vctrList.addElement((String)userName);
     }
--------------------------------------------
     else                          //为上过线的用户返回信息
     {
streamToClient.writeObject("Welcome");
streamToClient.flush();
     }
-------------------------------------------
  }


        int verify(String mesg)
{


try{
RandomAccessFile RAS = new RandomAccessFile("UserPwd.txt","r");

System.out.println(RAS.getFilePointer()+" "+RAS.length());

RAS.seek(0);
int i = 0;
String str = "";
while(!((RAS.getFilePointer())==(RAS.length())))  //getFilePointer:返回此文件中的当前偏移量。到此文件开头的偏移量(以字节为单位)
{

str = RAS.readLine();
if(str.equals(mesg))
{

System.out.println(RAS.getFilePointer()+" "+RAS.length());
ctr = 1;
break;
}

}//end of while

RAS.close();

}catch(Exception e){
System.out.println("Connect.java verify(String mesg)");
System.out.println("Connect.java 58 Exception Occurred:"+e);
}

return ctr;

}//end of verify()

解决这问题时花费时间很多,这就是经验的问题。引发这异常的问题点是寻到的并解决,可这问题点触发这异常里的过程却还不是很清楚。当Client发送登录信息时,Server接收并验证在验证函verify()中所使用的是RandoAccesFile类进行读取后流关闭。Hashtable vctrList(用于存贮在线用户)是问题的关键,当Client关闭时Server的vrtrList并没有删除对应用户,所以再次登录检验用户时,server端并没有相应的streamToClient.writeObject(),才引发java.io.EOFException。具本为什么这样我也很迷惑,没有相应的返回信息它会返回上一步再次读取登录信息吗?可文件流有关闭呀,且每次读前我都有从新定位的。看来Socket与线程还要好好读读。

你可能感兴趣的:(socket)