编辑:,如果我从ServerWriter类中的run方法中删除代码并将其放入发送顺序方法,删除线程代码,它就会开始正常运行agian,插座不关闭。但我需要它是多线程的,我不明白为什么套接字用线程关闭。代码如下所示
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()时打印关闭的服务器输出中推断出这一点,在客户端一样它说不结束
我有一个套接字程序。我在客户端1中有两个用于打开套接字的方法,另一个用于读取数据。每次我尝试运行此代码时,我都会继续获得eofexception,这意味着套接字已关闭。
我上次在几天前处理这段代码时出现了这个错误,所以我刚刚从read中删除了所有代码并将其粘贴在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();
}
答案 0 :(得分:3)
该异常意味着你关闭套接字,然后继续使用它。与同伴没什么关系。
可能你不知道关闭输入流或套接字的输出流会关闭另一个流和套接字。