编辑:在服务器端,如果我从 ServerWriter 类的 run 方法中删除代码并将其放入 send order 方法中,删除线程代码,它再次开始正常工作,套接字不会关闭。但我需要它是多线程的,我不明白为什么套接字用线程关闭。代码如下所示
public void sendOrder(Order o){
// ServerWriter sw=new ServerWriter(serversocket,o);
// Thread t=new Thread(sw);
// t.start();
ObjectOutputStream out=null;
if(serversocket.isClosed()){
System.out.println("closed");
}
else{
System.out.println("notclosed");
}
try {
out=new ObjectOutputStream(serversocket.getOutputStream());
out.writeObject(o);
out.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
编辑:我认为套接字在服务器端关闭,而不是客户端,从服务器的输出中推断出这一点,它在检查时打印关闭isclosed()
,而在客户端它说 Not Closed
我有一个套接字程序。我在客户端 1 中有两种方法用于打开套接字,另一种用于读取数据。每次我尝试运行此代码时,我都会不断收到eofexception
,这意味着套接字已关闭。
几天前我最后一次处理这段代码时遇到了这个错误,所以我只是从阅读中删除了所有代码并将其粘贴到 open 方法中,它就开始工作了(我真的不明白为什么)。
今天我恢复过来,只是将代码移回 read 方法,一切都开始正常工作,连接并没有自行关闭。然后我将读取部分设为线程,但一切正常,我正在处理代码的写入部分,现在当我运行它时eofexception
,我再次得到相同的结果,我没有更改读取代码中的任何内容,所以我不知道为什么会这样。在服务器端,我做了类似的事情,我在一个方法中接受一个套接字并通过一个线程发送数据。在服务器端,我得到一个 SocketException。
我真的可以使用一些帮助来阐明我做错了什么。
以下是客户端代码
public void open(){
try
{
s=new Socket(InetAddress.getLocalHost(),12345);
s.setKeepAlive(true);
}
catch(UnknownHostException u)
{
System.err.println("I don't know host");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read(){
ClientReader cr=new ClientReader(s);
Thread t=new Thread(cr);
t.start();
}
class ClientReader implements Runnable{
Socket s;
ClientReader(Socket s){
this.s=s;
}
public void run(){
InputStream is;
ObjectInputStream ois=null;
try {
is = s.getInputStream();
if(!(s==null)){
System.out.println("not null");
}
**//outputs NotClosed here**
if(s.isClosed()){
System.out.println("CLOSED");
}
else system.out.println("Not Closed")
//Throws EOFException here
ois= new ObjectInputStream(is);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Order order;
while(true){
try{
order=(Order)ois.readObject();
Client_Socket.ll.add(order);
System.out.println(order);
System.out.println(order.getTotal());
}
catch(EOFException e){
try{
ois.close();
s.close();
break;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
调用客户端的主要方法
public static void main(String[] args) {
// TODO Auto-generated method stub
Client_Socket cs=new Client_Socket();
cs.open();
cs.read();
}
客户端异常
not null
Not Closed
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at ClientReader.run(Client_Socket.java:82)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-0" java.lang.NullPointerException
at ClientReader.run(Client_Socket.java:91)
at java.lang.Thread.run(Unknown Source)
服务器端异常
Connection from Socket[addr=/192.168.0.108,port=50380,localport=12345]
closed
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Unknown Source)
at ServerWriter.run(Server_Socket.java:123)
at java.lang.Thread.run(Unknown Source)
服务器端代码片段
public void openSocket(){
try
{
service=new ServerSocket(12345);
serversocket=service.accept();
serversocket.setKeepAlive(true);
System.out.println("Connection from "+serversocket);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("output stream error");
System.out.println(e);
}
}
public void sendOrder(Order o){
ServerWriter sw=new ServerWriter(serversocket,o);
Thread t=new Thread(sw);
t.start();
}
class ServerWriter implements Runnable{
Socket serversocket;
Order o;
ServerWriter(Socket s,Order o){
this.serversocket=s;
this.o=o;
}
public void run() {
ObjectOutputStream out=null;
if(serversocket.isClosed()){
System.out.println("closed");
}
else{
System.out.println("notclosed");
}
try {
//SocketException here
out=new ObjectOutputStream(serversocket.getOutputStream());
out.writeObject(o);
out.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
调用服务器的方法片段
public static void main(String[] args) {
// TODO Auto-generated method stub
Order o=new Order();
Server_Socket ss=new Server_Socket();
ss.openSocket();
ss.sendOrder(o);
ss.close();
}