java.net.SocketException套接字已关闭
我正在将客户端的对象发送到服务器,在服务器端修改该对象并将其重新发送到客户端。将对象从客户机发送到服务器是正常的,它工作正常,但当我将对象发送回服务器时,它给出了异常套接字关闭。这是代码。 IntString和ParentObj是我正在发送对象的类java.net.SocketException套接字已关闭,java,socketexception,Java,Socketexception,我正在将客户端的对象发送到服务器,在服务器端修改该对象并将其重新发送到客户端。将对象从客户机发送到服务器是正常的,它工作正常,但当我将对象发送回服务器时,它给出了异常套接字关闭。这是代码。 IntString和ParentObj是我正在发送对象的类 import java.io.*; import java.net.*; public class Send_recv { Socket s; IntString obj1; public Send_recv(Socket
import java.io.*;
import java.net.*;
public class Send_recv {
Socket s;
IntString obj1;
public Send_recv(Socket s) {
this.s = s;
}
public void sendObj(ParentObj obj) {
try {
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
oos.close();
os.close();
} catch (Exception e) {
System.out.println(e);
}
}
public ParentObj recObj() {
try {
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
obj1 = (IntString) ois.readObject();
ois.close();
is.close();
} catch (Exception e) {
System.out.println(e);
}
return (obj1);
}
}
客户1类:
import java.net.*;
import java.io.*;
public class Client1 {
public static void main(String args[]) {
int arr[] = new int[10];
int length = 6, i, counter_1;
ParentObj obj1;
for (i = 0; i < length; i++) {
arr[i] = i + 10;
}
try {
Socket s = new Socket("localhost", 6789);
IntString obj = new IntString(arr, length);
/*
* OutputStream os = s.getOutputStream();
* ObjectOutputStream oos = new ObjectOutputStream(os);
*
* oos.writeObject(obj);
* oos.close();
* os.close();
*/
Send_recv snd = new Send_recv(s);
snd.sendObj((ParentObj) obj);
if (s.isClosed()) {
System.out.println("Closed");
// s.connect(null);
}
obj1 = snd.recObj();
obj = (IntString) obj1;
if (obj != null) {
for (counter_1 = 0; counter_1 < obj.length_of_row; counter_1++) {
System.out.println(obj.row[counter_1]);
obj.row[counter_1]++;
}
}
// s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
此异常意味着您关闭了套接字,然后继续尝试使用它
os.close();
你把它关在这里了。关闭
套接字的输入流或输出流
将关闭另一个流和套接字。
谢谢您的帮助。
java.net.SocketException socket is closed
os.close();