从服务器到套接字的WriteObject错误

时间:2016-05-06 08:55:28

标签: java sockets serversocket serializable objectoutputstream

当从服务器向客户端发送对象时,有人帮我解决了这个异常 它是NotSerializableException<< 我尝试解决错误并实现接口Serializable和相同的异常>> >>> >

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Haz
 */
public class Server {

    boolean isRunning = true;
    public static final int Port = 500;
    public static final String Address = "127.0.0.1";
    ObjectOutputStream outToClient;
    Socket Client;
    ArrayList<ConnectionHandler> Handlers;
    HashSet<Socket> Callers;

    public Server() throws IOException {

        ServerSocket socketSer = new ServerSocket(500);
        ExecutorService service = Executors.newFixedThreadPool(50);

        Handlers = new ArrayList<>();
        Callers = new HashSet<>();
        while (isRunning) {
             Client = socketSer.accept();
             System.out.println("Client Connect on Sever");
            ConnectionHandler handler = new ConnectionHandler(Client,socketSer);
            Handlers.add(handler);
            Callers.add(Client);
            SendConnectToAll(Handlers);


        }
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        new Server();
    }

    private void SendConnectToAll(ArrayList<ConnectionHandler> Handlers){


        try {
            outToClient = new ObjectOutputStream(Client.getOutputStream());
            outToClient.writeObject(Handlers);
            outToClient.flush();
            outToClient.close();   
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

}





/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Haz
 */
public class SocketClient {
    Socket Client;
    ObjectInputStream inputFromServer;

    public SocketClient(String Address,int Port) {
        try {
            Client = new Socket(Address,Port);
        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        new Thread(new Runnable(){
            @Override
            public void run() {
                         Object temp =null;

                try {
                    inputFromServer = new ObjectInputStream(Client.getInputStream());
                    temp =inputFromServer.readObject();
                    while((temp)!=null){
                          temp = inputFromServer.readObject();
                        System.out.println(temp);
                    }
                    inputFromServer.close();
                } catch (IOException ex) {
                    Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }).start();
    }



}

和类ConnectionHandler它是空的但我实现了

public class ConnectionHandler implements Runnable,Serializable {

    private Socket Client;
    private ServerSocket Server;

    public ConnectionHandler(Socket Client, ServerSocket Server) {
        this.Client = Client;
        this.Server = Server;
    }

1 个答案:

答案 0 :(得分:2)

ConnectionHandler不可序列化,因为它包含对Socket和ServerSocket的引用,这些引用不可序列化。您必须编写自己的序列化和反序列化方法以使其可序列化。

然而,无论如何使其可序列化是没有意义的,因为它没有任何可序列化的数据通过网络传输。