开发者

problem with sockets in java

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.ObjectOutpu开发者_如何学编程tStream.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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜