简体   繁体   English

SocketException:通过套接字发送对象

[英]SocketException: Sending object through Socket

I am new to Socket programming.I want to send an Object of class Person to the server using Sockets. 我是Socket编程的新手,我想使用Sockets将Person类的Object发送到服务器。 There is an exception of Server Socket. 服务器套接字是一个例外。
Class person is as follows: 班级人员如下:

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;
    }

}

I first run my server . 我首先运行服务器。 The server code is as follows: 服务器代码如下:

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();
    }
}

Then I run my client. 然后,我运行我的客户端。 Its code is as follows. 其代码如下。

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();
        }
    }
}

Following Exception is thrown at the server side. 服务器端抛出以下异常。

java.net.SocketException: Connection reset

well your code had some flaws in it, such as not closing the connections, misplaced while loops, you know the usual stuff :) 很好,您的代码中有一些缺陷,例如不关闭连接,while循环放错位置,您知道通常的东西:)

Server.java 服务器.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 客户端.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();
        }
    }
}

Your client doesn't do anything with the socket after sending the object, it just quits. 您的客户端在发送对象后不对套接字执行任何操作,只是退出了。 If you don't close a connection properly before ending the process, the operating system gets confused and will just reset the connection. 如果您在结束该过程之前未正确关闭连接,则操作系统会感到困惑,并将重置连接。

You'll need to call shutdownOutput() and close() on the socket. 您需要在套接字上调用shutdownOutput()和close()。

See also purpose of socket.shutdownOutput() 另请参见socket.shutdownOutput()的用途

In Client.java, change main to the following: 在Client.java中,将main更改为以下内容:

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

This makes your client close its connections to the server before closing. 这使您的客户端在关闭之前关闭其与服务器的连接。 This helps prevent the OS from resetting the connection as mentioned by escitalopram 如escitalopram所述,这有助于防止操作系统重置连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM