1

I have the following thread which accepts for incoming connections at a specific port:

public class ClientThread implements Runnable {
    ServerSocket serverSocket;
    Socket clientSocket;
    int serverPort = 6500;
    private String serverIpAddress = "127.0.0.1";
    DataInputStream is;
    ObjectOutputStream os=null;
    Coordinate coord;

    protected BlockingQueue queue = null;

    public ClientThread(BlockingQueue queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            InetSocketAddress serverAddr = new InetSocketAddress(
                    serverIpAddress, serverPort);
            serverSocket = new ServerSocket();
            serverSocket.bind(serverAddr);
            System.out.println("s-a creat");
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to host");
        }

        try {
            clientSocket = serverSocket.accept();
            System.out.println("S-a conectat clientul de monitorizare!");

            os=new ObjectOutputStream(clientSocket.getOutputStream());
            try{
                while(true){
                    coord=(Coordinate)queue.take();
                    System.out.println(coord.getLat()+coord.getLon()+coord.getwId());
                    os.writeObject(coord);
                    os.flush();
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            System.out.println(e);

            try {
                clientSocket.close();
                os.close();
            }catch(Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

The object I'm trying to send is an instance of the following class:

public class Coordinate implements Serializable{
    private final int lon;
    private final int lat;
    private final int workerId;

    public Coordinate(int lat, int lon, int workerId) {
        this.lat = lat;
        this.lon = lon;
        this.workerId=workerId;
    }

    public int getLon() {
        return lon;
    }

    public int getLat() {
        return lat;
    }

    public int getwId() {
        return workerId;
    }
}

But when I start the thread and I accept for connection I get the following error:

java.net.SocketException: Software caused connection abort: 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 
    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 servers.ClientThread.run(ClientThread.java:55)
    at java.lang.Thread.run(Unknown Source)

Anyone anyidea of what is wrong?


This error java.net.SocketException: Software caused connection abort appears when one of the socket end crashes....in my case was the end that connected to ClientThread() and when I was trying to write in the buffer the error appeared.

2
  • Which line is line 55 of ClientThread.java? Commented May 29, 2011 at 14:09
  • os.writeObject(coord); this one,but don't bother with it cause my problem is a different one...I have a Geocoder on the other side of the socket that makes my connection crash...I'll try to fix it and if it doesn't work I'll post another question.Thx Commented May 29, 2011 at 14:18

1 Answer 1

1

You did not put the client side code and it is hard to understand what can client to do, but I see one part of code where can be problem. It is

 while(true){
                    coord=(Coordinate)queue.take();
                    System.out.println(coord.getLat()+coord.getLon()+coord.getwId());
                    os.writeObject(coord);
                    os.flush();
                }

What is java.net.SocketException: Software caused connection abort: socket write error meaning? It means that connection was closed BUT you tried to write some data to socket. But I can be wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

No u are noot wrong...the connection was closed from the other side;).....u are good at this;)
It will be better to analyze all code, not just a one side, please in future post all scope of code :) Cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.