java中的socket问题

发布于 2024-11-10 01:39:48 字数 3417 浏览 4 评论 0原文

我有以下线程,它接受特定端口处的传入连接:

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

我尝试发送的对象是以下类的实例:

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

但是当我启动线程并接受连接时,我收到以下错误:

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)

任何人都知道有什么问题吗?


当其中一个套接字端崩溃时,会出现此错误 java.net.SocketException:软件导致连接中止......在我的情况下是连接到 ClientThread() 的一端,当我尝试在缓冲区中写入时,出现错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-11-17 01:39:48

您没有放置客户端代码,很难理解客户端可以做什么,但我看到代码的一部分可能存在问题。它是

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

什么 java.net.SocketException: Software Caused Connection abort: socket write error 含义?这意味着连接已关闭,但您尝试向套接字写入一些数据。但我可能是错的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文