Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

i am trying to create a little server in java using sockets. The sockets are connected but when i send data from my client to my server it gives an exception.

stacktrace (UPDATED):

    IOException on socket listen: java.net.SocketException: Connection reset
    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at org.codox.game.network.NetworkController$doComms.run(NetworkController.java:81)
    at java.lang.Thread.run(Unknown Source)

if you need more information tell me

any suggestions are welcome

thx

-- EDITED --

full server code:

class doComms implements Runnable
{
    private Socket server;
    PackageHandler packageHandler;
    doComms(Socket server)
    {
        this.server = server;
        packageHandler = new PackageHandler();
    }

    public void run()
    {

        try
        {
            ObjectInputStream ois = new ObjectInputStream(server.getInputStream());     
            ClientPackage clientPack = (ClientPackage) ois.readObject();

            RespondPackage rp = packageHandler.handlePackage(clientPack);

            ObjectOutputStream ous = new ObjectOutputStream(server.getOutputStream());

            ous.writeObject(rp);
            ous.flush();

            server.close();
            System.out.println("Connection was finished and closed.");

        } catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException ioe)
        {
            System.out.println("IOException on socket listen: " + ioe);
            ioe.printStackTrace();
        }
    }
}

full client code:

SocketHints hints = new SocketHints();
        hints.keepAlive = true;
        hints.connectTimeout = 2000;

        Socket socket;
        try
        {
            socket = Gdx.net.newClientSocket(Protocol.TCP, "127.0.0.1", 12346, hints);

            ClientPackage client = new ClientPackage();
            client.setRequestType(RequestType.ping);

            ObjectOutputStream ous = new ObjectOutputStream(socket.getOutputStream());

            ous.writeObject(client);
            ous.flush();

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

            RespondPackage response = (RespondPackage) ois.readObject();
            System.out.println("Respone type = " + response.getRequestType());

        } catch (UnknownHostException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (Exception e)
        {

        }
share|improve this question
    
The sockets are connected but the error says they are not. For further debugging you can try using some tool to monitor network packets. you can try Wireshark – Abdullah Shaikh Oct 31 '13 at 11:23
    
please share the server code too, the possibility is may you close either output or input streams, so closing the stream(in || out) causes closing the socket. – user2511414 Oct 31 '13 at 11:24
    
post updated with full code – user1971401 Oct 31 '13 at 11:29
    
Why all the ByteArrayInput/OutputStreams? Why not just write the objects directly to the socket? And read them directly from it? Why the PrintWriter? Why test isConnected() at a point where it can't possibly be false? – EJP Oct 31 '13 at 22:22
    
@EJP i updated my code so it direclty writes/read objects. but im still getting the connection reset. any suggestions on what it might be? – user1971401 Nov 1 '13 at 22:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.