【问题标题】:SocketException: Sending object through SocketSocketException:通过 Socket 发送对象
【发布时间】:2015-09-02 05:52:19
【问题描述】:

我是 Socket 编程的新手。我想使用 Socket 将 Person 类的对象发送到服务器。 Server Socket有一个例外。
班人如下:

import java.awt.Image;
import java.io.Serializable;


public class Person implements Serializable {

    private String name;
    private String id;

    public Person() {}
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public Person(String name, String id) {
        super();
        this.name = name;
        this.id = id;
    }

}

我首先运行我的服务器。服务端代码如下:

import java.io.*;    
import java.net.*;
public class Server extends Thread {
    ServerSocket serverSocket;
    Socket socket;
    ObjectInputStream inStream=null;
    ObjectOutputStream outStream=null;

    public void run(){
        try{
            serverSocket=new ServerSocket(2004);
            socket=serverSocket.accept();
            this.outStream=new ObjectOutputStream(socket.getOutputStream());
            outStream.flush();
            this.inStream=new ObjectInputStream(socket.getInputStream());
            while(true){
                System.out.println("recieving  data");
                Person person=(Person)this.inStream.readObject();
                //queue.put(person);
                System.out.println(person.getName()+"  "+person.getId());
            }

        }
        catch(EOFException e){
        }
        catch(IOException e){
            e.printStackTrace();
        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    public static void main(String args[]){
        new Server().start();
    }
}

然后我运行我的客户端。其代码如下。

import java.io.*;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.tree.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.LinkedList;
public class Client {
    Socket socket;

    ObjectInputStream inStream=null;
    ObjectOutputStream outStream=null;
    String ip="localhost";  
    boolean connected=false;
    public Client(String ip){
            this.ip=ip;
    }
    public Client(){
    }
    public static void main(String args[]){
        new Client().connect();
    }

    public boolean connect(){
        try {
            socket = new Socket(ip, 2004);
            System.out.println("Client starting");
            this.outStream=new ObjectOutputStream(socket.getOutputStream());
            outStream.flush();
            this.inStream=new ObjectInputStream(socket.getInputStream());
            String name;
            System.out.println("sending data");
            outStream.writeObject(new Person("faisal ","232323"));

        }catch(EOFException e){
            JOptionPane.showMessageDialog(null,"EOFException");
            System.out.println("I am inside Connector EOFException"); 
            connected=false;
            //System.out.println("PoolRunner Client side Disconnected");
        }
        catch (IOException e) {
            JOptionPane.showMessageDialog(null, "IOException");
            System.out.println("I am inside Connector IOException");
            System.out.println(e);
            connected=false;
        }

        finally{
            return(connected);
        }
    }

    public ObjectInputStream getObjectInputStream(){
        return this.inStream;
    }
    public ObjectOutputStream getObjectOutputStream(){
        return this.outStream;
    }
    public void disconnect(){
        try{
            this.inStream.close();
            this.outStream.close();
            this.socket.close();
        }catch(IOException e){
            JOptionPane.showMessageDialog(null,"Problem in closing streams or socket");
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,"Problem in closing streams or socket");
            e.printStackTrace();
        }
    }
}

在服务器端抛出以下异常。

java.net.SocketException: Connection reset

【问题讨论】:

  • 您能否提供堆栈跟踪(或至少在服务器上引发异常的哪一行)?另外,服务器是否打印了任何对象/接收数据/等?
  • 这可能是因为客户端关闭了连接,而您的服务器仍在尝试从输入流中读取。
  • 其实他从来没有关闭过连接...
  • @QuakeCore 客户端程序在写入对象后将退出,但服务器将继续读取,因为它使用无限循环。
  • 大声笑我有一个答案,但无论我做什么,由于格式问题,我无法发布它

标签: java sockets


【解决方案1】:

你的代码有一些缺陷,比如没有关闭连接,错误的while循环,你知道通常的东西:)

服务器.java

import java.io.*;
import java.net.*;

public class Server extends Thread {
    ServerSocket serverSocket;
    Socket socket;
    ObjectInputStream inStream = null;
    ObjectOutputStream outStream = null;

    public Server() {
    }

    public void run() {
        try {
            serverSocket = new ServerSocket(2004);
            while (true) {
                socket = serverSocket.accept();
                this.outStream = new ObjectOutputStream(
                        socket.getOutputStream());
                outStream.flush();
                this.inStream = new ObjectInputStream(socket.getInputStream());

                System.out.println("recieving  data");

                Person person = (Person) this.inStream.readObject();
                // queue.put(person);
                System.out.println(person.getName() + "  " + person.getId());
            }

        }

        catch (EOFException e) {

        } catch (IOException e) {
            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        }

    }

    public static void main(String args[]) {
        new Server().start();

    }
}

Client.java

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.JOptionPane;

public class Client {
    Socket socket;

    ObjectInputStream inStream = null;
    ObjectOutputStream outStream = null;
    String ip = "localhost";
    boolean connected = false;

    public Client(String ip) {
        this.ip = ip;
    }

    public Client() {
    }

    public static void main(String args[]) {
        new Client().connect();
    }

    public void connect() {
        try {
            socket = new Socket(ip, 2004);
            System.out.println("Client starting");
            this.outStream = new ObjectOutputStream(socket.getOutputStream());
            connected = true;

            String name;
            System.out.println("sending data");
            outStream.writeObject(new Person("faisal ", "232323"));
            outStream.flush();
        } catch (EOFException e) {
            JOptionPane.showMessageDialog(null, "EOFException");
            System.out.println("I am inside Connector EOFException");
            connected = false;
            // System.out.println("PoolRunner Client side Disconnected");
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "IOException");
            System.out.println("I am inside Connector IOException");
            System.out.println(e);
            connected = false;
        }

        finally {
            if (connected) {
                disconnect();
            }
        }
    }

    public ObjectOutputStream getObjectOutputStream() {
        return this.outStream;
    }

    public void disconnect() {
        try {
            this.outStream.close();
            this.socket.close();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null,
                    "Problem in closing streams or socket");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Problem in closing streams or socket");
            e.printStackTrace();
        }
    }
}

【讨论】:

  • 哈哈哈,发布答案花了我更多的时间来弄清楚答案,Crazy Formatters!
【解决方案2】:

您的客户端在发送对象后不会对套接字做任何事情,它只是退出。如果您在结束进程之前没有正确关闭连接,操作系统会感到困惑,只会重置连接。

您需要在套接字上调用 shutdownOutput() 和 close()。

另见purpose of socket.shutdownOutput()

【讨论】:

  • 是的,你完全正确!我已经修复了错误
【解决方案3】:

在 Client.java 中,将 main 更改为以下内容:

public static void main(String args[]){
    Client cl = new Client();
    cl.connect();
    cl.disconnect();
}

这会使您的客户端在关闭之前关闭其与服务器的连接。这有助于防止操作系统重置依他普仑所述的连接

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 2012-11-29
    • 2010-12-28
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    相关资源
    最近更新 更多