我在c/s模式中...
客户端发送消息和服务器接受和反馈消息,为什么readUTF()抛出异常?
相关代码如下:
客户端注册部分:
try{
    Socket socket=new Socket(InetAddress.getByName(host),port);
    DataInputStream inData=new DataInputStream(socket.getInputStream());
    DataOutputStream outData=new DataOutputStream(socket.getOutputStream());

                    //这里向服务器发送用户注册的信息,以“INFO”标志此为注册信息,以区别其他客户请求服务
    outData.writeUTF("INFO"+"!"+givenName.getText()+"!"+
surName.getText()+"!"+address.getText()+"!"+
city.getText()+"!"+postCode.getText()+"!"+
province.getText()+"!"+countryStr+"!"+timeZoneStr+
"!"+phoneNumber.getText()+"!"+email.getText()+
"!"+userName.getText()+"!"+password.getText()
);
    outData.flush(); //刷新缓冲                       //接受服务器的消息反馈
    String inStr=inData.readUTF();//这里报错
//at java.io.DataInputStream.readUnsignedShort(Unknown Source)
//at java.io.DataInputStream.readUTF(Unknown Source)
//at java.io.DataInputStream.readUTF(Unknown Source)
    if(inStr.equalsIgnoreCase("ok")){//如果服务器发送来的消息是“OK”说明注册成功
JOptionPane.showMessageDialog(this, "Register success!");
this.dispose();
this.setVisible(false);
     }
     else if(inStr.equalsIgnoreCase("invald")) 
JOptionPane.showMessageDialog(this, 
                                            "user Name has registered,please try another one.");
        }catch(IOException e){
e.printStackTrace();
}//服务器端消息处理
//此处是服务器监听到客户的链接,新建的线程处理客户请求服务。
                  inData=new DataInputStream(clientSocket.getInputStream());
while(true){
//接受客户端发送来的请求消息
String inStr=inData.readUTF();

//依据请求消息的起始部分,判断客户端请求服务的内容

if(inStr.startsWith("INFO")){ //消息为用户注册信息
String ss="";
// System.out.println(inStr);
String[] InfoArrayStr=inStr.substring(5).split("!");
// System.out.println(""+InfoArrayStr.length);

//先定为所有的用户注册信息必须填写;所以循环次数定义为12
for(int i=0;i<11;i++)
ss+="'"+InfoArrayStr[i]+"',";
ss+="'"+InfoArrayStr[11]+"')";

try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   try {
    Connection c=
                                            DriverManager.getConnection("jdbc:odbc:onlineJudge");
Statement s=c.createStatement();

ResultSet rs=s.executeQuery(" select * from userInfoTable " 
                                                     + "where userName='" +InfoArrayStr[10]+"'");
rs.last(); //将指针移动到ResultSet对象的最后一行

DataOutputStream outData=new
DataOutputStream(clientSocket.getOutputStream());

//如果最后一行的行号不等于0,则用户名已经被占用
if(rs.getRow()>0) {
outData.writeUTF("invalid");
}else{
//建立新用户并填写用户注册信息
    s.executeUpdate(" insert into userInfoTable values( "+ss);

//向用户发送注册成功的消息
outData.writeUTF("ok");
//刷新缓冲
outData.flush();
}

//关闭数据库链接
s.close();
c.close();
}catch (SQLException se) { //未找到数据库,捕获异常
while (se != null) {
    String SQLState = se.getSQLState();
    String SQLMessage = se.getMessage();
    System.out.println("Error = "+SQLState);
    System.out.println(SQLMessage);
    se = se.getNextException();
}
}
}catch(ClassNotFoundException e){ //未找到数据源驱动,捕获异常
e.printStackTrace();
}
}else if(){
                 //进行其他的判断处理
                }
         }//end while不知道为什么报错。是不是readUTF()和writeUTF()用错了?