views:

71

answers:

2

Okay, so I'm writing a test program to send a vector of objects from server to client.

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

-Client-

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

Port 4444 has been opened.

Getting this error:

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)
+1  A: 

You got this error because client terminates with System.exit(0); call in line 21.

I see that it's just a test but it is good form to write careful code in tests too. -use right visibility modificators -close streams at finally block -use ArrayList instead of Vector if you don't need thread safety during operations with it -etc

This code contains many potential bugs that is why real (quite simple) problem wasn't noticed at first site.

Donz
+1  A: 

You got this error because the client terminated with System.exit(0) in line 21, which closed the connection, and you kept writing to the connection at one end after it had been closed by the other end. This is either not a realistic situation, in which case you shouldn't do it, or it is a realistic situation, in which case you should expect to get connection resets when writing.

EJP