10

我正在将客户端的对象发送到服务器,在服务器端修改该对象并将其重新发送给客户端。将对象表单客户端发送到服务器可以正常工作,但是当我将对象发送回时,它会给出异常套接字已关闭。这是代码。IntString 和 ParentObj 是我发送对象的类。

客户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);
        }
    }
}

服务器类

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

public class Server1 {
    public static void main(String args[]) {
        IntString obj;
        ParentObj obj1;
        int port = 6789, counter_1;
        try {
            ServerSocket ss = new ServerSocket(port);
            Socket s = ss.accept();
            Send_recv rev = new Send_recv(s);
            /*
             * InputStream is = s.getInputStream(); 
             * ObjectInputStream ois = new ObjectInputStream(is); 
             * IntString obj = (IntString)ois.readObject();
             */
            obj1 = rev.recObj();
            obj = (IntString) obj1;
            System.out.println(s.getInetAddress());
            System.out.println(s.getLocalAddress());
            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]++;
                }
            }
            if (ss.isClosed()) {
                System.out.println("Closed ss");

            }
            if (s.isClosed()) {
                System.out.println("Closed in Server");

            }
            Send_recv snd = new Send_recv(s);
            snd.sendObj((ParentObj) obj);

        } catch (Exception e) {
            System.out.println(e + "In Server");
        }

    }
}

Send_recv 类用于发送和接收对象。

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

1 回答 1

31
java.net.SocketException socket is closed

这个异常意味着关闭了套接字,然后继续尝试使用它。

os.close();

而你在这里关闭了它。关闭 a 的输入或输出流会Socket关闭另一个流,并且Socket.

于 2013-03-06T05:14:10.933 回答