通过网络发送对象矢量

时间:2010-09-28 05:21:02

标签: java networking vector

好的,所以我正在编写一个测试程序,用于从服务器向客户端发送对象向量。

-Server -

import java.net.*;
import java.util.Vector;
import java.io.*;

public class TestServer{
    public static void main(String[] args) throws IOException {

        Vector<Obj> obj = new Vector<Obj>();
        obj.add(new Obj(5));
        obj.add(new Obj(4));
        obj.add(new Obj(7));
        obj.add(new Obj(8));
        obj.add(new Obj(2));

        ServerSocket serverSocket = new ServerSocket(4444);
        boolean listening = true;

        while(listening){
            new ClientThread(serverSocket.accept(), obj).start();
        }

        serverSocket.close();
        System.exit(0);
    }
}

class ClientThread extends Thread implements Runnable{
    Socket acceptedSocket;
    Vector<Obj> obj;

    public ClientThread(Socket acceptedSocket, Vector<Obj> obj){
        super("ClientThread");
        this.acceptedSocket = acceptedSocket;
        this.obj = obj;
    }

    public void run(){
        try{
            Socket clientSocket = acceptedSocket;
            clientSocket.setTcpNoDelay(true);
            System.out.println("Accepted. Now creating I/O.\n");
            DataInputStream in = new DataInputStream(clientSocket.getInputStream());
            ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
            System.out.println("player connected. \n");
            out.writeObject(obj);
            out.flush();

            while(true){
                out.writeObject(obj);
                out.flush();

               sleep(15);
            }

        }

        catch(Exception e){
            e.printStackTrace();
            System.exit(1);
        }


    }
}

class Obj implements Serializable{
    int i;

    public Obj(int i){
        this.i = i;
    }
}

- 客户端 -

import java.net.*;
import java.util.Vector;
import java.io.*;

import javax.swing.JOptionPane;


public class TestClient {

    static Vector<Obj> obj;
    static ClientThread ct;

    public static void main(String[] args){
        obj = new Vector<Obj>();
        connect();

        for(int i = 0; i < obj.size(); i++){
            System.out.println("Obj: " + obj.get(i).i);
        }

        System.exit(0);
    }

    public static void connect(){
        String ip = JOptionPane.showInputDialog("Input server IP.");
        ct = new ClientThread(ip, obj);
        ct.start();
        ct.setPriority(Thread.MAX_PRIORITY);
    }
}

class Obj implements Serializable{
    int i;

    public Obj(int i){
        this.i = i;
    }
}

class ClientThread extends Thread implements Runnable{
    Socket socket;
    Vector<Obj> obj;
    DataOutputStream out;
    ObjectInputStream in;
    boolean loop = true;

    @SuppressWarnings("unchecked")
    public ClientThread(String ip, Vector<Obj> obj){
        super("ClientThread");

        try{
            socket = new Socket(ip, 4444);
            socket.setTcpNoDelay(true);
            out = new DataOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
            this.obj = obj; 
            this.obj = (Vector<Obj>) in.readObject();
        }

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

    public void run(){
        try{
            while(loop){
                try{
                    if(!socket.isClosed() && socket.isConnected()){         
                        obj = (Vector<Obj>) in.readObject();
                        this.sleep(15);
                    }

                    else
                        loop = false;

                }
                catch(Exception e){
                        e.printStackTrace();
                        socket.close();
                }  
            }



        }

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

港口4444已经开通。

出现此错误:

Accepted. Now creating I/O.

player connected. 

java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
    at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at ClientThread.run(TestServer.java:49)

2 个答案:

答案 0 :(得分:1)

您收到此错误,因为客户端已终止          System.exit(0); 在第21行打电话。

我发现这只是一个测试,但在测试中编写谨慎的代码也是一种很好的形式。 - 使用正确的可见性修改器 - 最后一个块关闭流 如果在使用它时不需要线程安全,请使用ArrayList而不是Vector -etc

此代码包含许多潜在的错误,这就是为什么在第一个站点没有注意到真正(非常简单)的问题。

答案 1 :(得分:1)

您收到此错误是因为客户端在第21行终止了System.exit(0),关闭了连接,并且在另一端关闭后,您仍然在一端写入连接。 这可能不是一个现实的情况,在这种情况下你不应该这样做,或者这是一个现实的情况,在这种情况下你应该期望在写作时获得连接重置。 / p>