socket已经启动,但是我登陆或者注册都status都报空值,求解,急!!!!
- Java code
/** * 注册处理 */ @SuppressWarnings( { "unchecked", "deprecation" }) private void serverRegiste() { try{ int flag=0; //是否重名判断标志 Register_Customer clientMessage=(Register_Customer)obj; File flist=new File("user.txt"); if(flist.length()!=0){//判断是否是第一个注册用户 ObjectInputStream objInput=new ObjectInputStream(new FileInputStream(flist)); vList=(Vector)objInput.readObject(); //判断是否有重名 for(int i=0;i<vList.size();i++){ Register_Customer reg=(Register_Customer)vList.elementAt(i); if(reg.custName.equals(clientMessage.custName)){ toClient.println("用户名已经存在,请重新输入"); flag=1; break; } if(reg.custName.equals("所有人")){ toClient.println("禁止使用此用户名,请另外选择"); flag=1; break; } if(reg.custName.equals("admin")||reg.custName.equals("Administrator")){ toClient.println("禁止使用此用户名,请另外选择"); flag=1; break; } } } if(flag==0){ // 添加新注册用户 vList.addElement(clientMessage.custName+"注册成功"); //将量中的类写回到文件中 FileOutputStream file=new FileOutputStream(flist); ObjectOutputStream objout=new ObjectOutputStream(file); objout.writeObject(vList); //发送注册成功信息 toClient.println(clientMessage.custName+"注册成功"); Date t=new Date(); log("用户"+clientMessage.custName+"注册成功,"+"注册时间:"+t.toLocaleString()+"\n"); file.close(); objout.close(); formClient.close(); } }catch (ClassNotFoundException e) { System.out.println(e); }catch (IOException e) { System.out.println(e); } }
- Java code
//连接到服务器 Socket toServer; toServer=new Socket(strServerIp,1001); ObjectOutputStream streamToServer=new ObjectOutputStream(toServer.getOutputStream()); //写客户详细资料到服务器socket streamToServer.writeObject((Register_Customer)rc); //读取自服务器socket的状态 BufferedReader formServer=new BufferedReader(new InputStreamReader(toServer.getInputStream())); String status=formServer.readLine(); //显示成功消息 JOptionPane op=new JOptionPane(); op.showMessageDialog(null, status); if(status.equals(rc.custName+"注册成功")){ txtUserName.setText(""); pwdPassword.setText(""); pwdConfirmPass.setText(""); txtAge.setText(""); txtEmail.setText(""); }
------解决方案--------------------
记得在每次ObjectOutputStream 的writeObject后面flush()一下