java.io.StreamCorruptedException:无效类型代码:04
我的客户机-服务器应用程序有点问题。当我想连接多个客户端并发送smth,或者我在客户端注销并尝试再次连接时,我遇到异常: “java.io.StreamCorruptedException:无效类型代码:04” 有什么问题吗?谢谢你的帮助 服务器代码:java.io.StreamCorruptedException:无效类型代码:04,java,ios,Java,Ios,我的客户机-服务器应用程序有点问题。当我想连接多个客户端并发送smth,或者我在客户端注销并尝试再次连接时,我遇到异常: “java.io.StreamCorruptedException:无效类型代码:04” 有什么问题吗?谢谢你的帮助 服务器代码: class ClientCommunication implements Runnable { private Socket incoming; public ClientCommunication(Socket clientS
class ClientCommunication implements Runnable {
private Socket incoming;
public ClientCommunication(Socket clientSocket) {
incoming = clientSocket;
}
public void run() {
try {
synchronized (this) {
serverObjectOutput = new ObjectOutputStream(
incoming.getOutputStream());
serverObjectInput = new ObjectInputStream(
incoming.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
int operation = -1;
synchronized(this) {
while (true) {
try{
if(serverObjectInput.available() > 0){
operation = serverObjectInput.readInt();
switch(operation) {
case 1:
Employee employee = (Employee) serverObjectInput.readObject();
//CHECK LOGGING DATA
// SEND RESULT = 1 OR RESULT = -1
break;
}
}
} catch(IOException | ClassNotFoundException | SQLException ex)
{
ex.printStackTrace();
}
}
}
}
}
class ServerStart implements Runnable {
private int portNumber;
public ServerStart(int portNumber) {
this.portNumber = portNumber;
}
public void run() {
try {
conn = getConnection();
stat = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
serverSocket = new ServerSocket(portNumber);
} catch (IOException e) {
e.printStackTrace();
}
try {
while (true) {
Socket incoming = serverSocket.accept();
Runnable r = new ClientCommunication(incoming);
Thread t = new Thread(r);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
客户端功能:
public void actionPerformed(ActionEvent e) {
if (isConnected == false) {
String ServerIP = ip.getText().trim();
int ServerPort = Integer
.parseInt(port.getText().trim());
try {
ClientSocket = new Socket(ServerIP, ServerPort);
clientObjectInput = new ObjectInputStream(
ClientSocket.getInputStream());
clientObjectOutput = new ObjectOutputStream(
ClientSocket.getOutputStream());
isConnected = true;
} catch (IOException ex) {
ex.printStackTrace();
}
synchronized (this) {
try {
ClientLoginFrame login = new ClientLoginFrame();
Employee employee = login.getEmployee();
clientObjectOutput.writeInt(1);
clientObjectOutput.flush();
clientObjectOutput.writeObject(employee);
int result = clientObjectInput.readInt();
if(result == 1)
{
// DO SMTH
}
else {
isConnected = false;
ClientSocket.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
});
我怀疑您的问题是在连接之间共享单例
serverInputStream
和serverOutputStream
。在多个线程中同时使用同一个流会损坏流(或使读取无效)如果可以使用更简单的阻塞读取,为什么还要忙着等待数据呢。此外,写入对象之前的刷新也不会起任何作用,写入对象之后的刷新和可能的reset()
可能更有用。当您遇到异常时,我不会假装它没有发生,而是继续。您需要将try/catch放在整个过程中,然后关闭连接。我建议分离GUI和网络处理类,并单独测试网络类。此外,您还应该在Java中为变量使用camelCase
。您对响应执行readInt(),但似乎没有编写这样的响应。