所以我有一个程序连接到主服务器然后我要求从主服务器打开新服务器(让我们称之为小服务器)然后我连接到他到目前为止很好:)
然后我使用以下代码行发送到小服务器字符串,int和对象:
ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
到目前为止一直很好:)
当我想使用以下代码行从小型服务器读回我的客户端时,问题就出现了:
ObjectInputStream fromServer = new ObjectInputStream(socket.getInputStream());
我的问题是我的客户端没有从小服务器(bool和int)获取数据..
我收到了这个错误:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.readBoolean(ObjectInputStream.java:2737)
at java.io.ObjectInputStream.readBoolean(ObjectInputStream.java:884)
at AddEmployee.AddEmployee(AddEmployee.java:168)
at AddEmployee.access$5(AddEmployee.java:136)
at AddEmployee$1.run(AddEmployee.java:110)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
是的,非常哈哈:)
这是我的客户代码:
* Global.toserver和Global.fromserver是我到主服务器的连接器!!!
try
{
int port;
Global.toServer.writeInt(btnAddEmp.getTag());
Global.toServer.flush();
try{
port=Global.fromServer.readInt();
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Socket socket= new Socket("localhost",port );
ObjectOutputStream toServer = new ObjectOutputStream(socket.getOutputStream());
toServer.flush();
ObjectInputStream fromServer = new ObjectInputStream(socket.getInputStream());
toServer.writeInt(2);
toServer.flush();
toServer.writeUTF(PasswordGenereator());
toServer.flush();
toServer.writeInt(Global.IDcompany);
toServer.flush();
toServer.writeObject(newEmp);
toServer.flush();
System.out.println(fromServer.available());//idk if its part of the problem but its give me a 0
if(fromServer.readBoolean())
{
//toServer.flush();
int num=fromServer.readInt();
System.out.println(num);
}
socket.close();
}
catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}catch(IOException e3)
{
e3.printStackTrace();
}
现在是小型服务器代码:
if(whatToDo==2)
{
try {
String pass=inputFromClient.readUTF();
int idcomp=inputFromClient.readInt();
Employee newEmp=(Employee)inputFromClient.readObject();
System.out.println(String.format("INSERT INTO `Employees`(`empNumber`, `Fname`, `Lname`, `BirthDate`, `Address`, `Email`, `Password`, `IDCompany`) VALUES ('%d','%s','%s','%s','%s','%s','%s','%d')",newEmp.getEmpNumber(),newEmp.getFname(),newEmp.getLname(),newEmp.getBirthDate(),newEmp.getAddress(),newEmp.getEmail().getEmailAddress(),pass,idcomp));
statement=con.prepareStatement(String.format("INSERT INTO `Employees`(`empNumber`, `Fname`, `Lname`, `BirthDate`, `Address`, `Email`, `Password`, `IDCompany`) VALUES ('%d','%s','%s','%s','%s','%s','%s','%d')",newEmp.getEmpNumber(),newEmp.getFname(),newEmp.getLname(),newEmp.getBirthDate(),newEmp.getAddress(),newEmp.getEmail().getEmailAddress(),pass,idcomp));
int result1=statement.executeUpdate();
System.out.println(result1);
if(result1==1)
{
System.out.println("poooo");
outputToClient.writeBoolean(true);
statement=con.prepareStatement(String.format("SELECT IDemp FROM Employees WHERE empNumber=%d",newEmp.getEmpNumber()));
result=statement.executeQuery();
if(result.next())
{
System.out.println(result.getInt(1));
int send=result.getInt(1);
System.out.println(send);
outputToClient.writeInt(send);
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我该怎么办? 感谢帮助者:)
好的,所以我设法在小服务器端每次发送后通过这条小线在4小时后解决它:
outputToClient.flush();
答案 0 :(得分:1)
这意味着没有什么可读的。这就是错误所说的。
在尝试阅读之前,您应确保已写出数据。
在实际写出某些内容之前,你有很多条件。检查是什么 这些条件评估,也许你甚至没有达到写作的目的。
“ObjectInputStream不工作”是一个非常强烈的声明,
实际上你使用ObjectInputStream的程序不起作用。