客户端部分代码如下publicvoidinitsocket(){try{Socketsocket=newSocket(hostname,port);objectOutputStream=newObjectOutputStream(socket.getOutputStream());objectInputStream=ne...
客户端部分代码如下
public void initsocket(){
try {
Socket socket = new Socket(hostname, port);
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectInputStream = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void closesocket(){
try {
objectInputStream.close();
objectOutputStream.close();
//socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public Datas doLogin(String username, String password){
try {
initsocket();
User user = new User();
user.setUsername(username);
user.setPassword(password);
Datas datas = new Datas();
datas.setFlag(SysConstants.SYS_LOGIN);
datas.setUser(user);
objectOutputStream.writeObject(datas);
datas = (Datas) objectInputStream.readObject();
return datas;
} catch (Exception e) {
e.printStackTrace();
} finally{
closesocket();
}
return null;
}
程序运行时 socket.close();报空指针异常 这是为什么
注释这行代码不影响我程序的运行 但是 不关闭套接字有什么坏处?
展开