gpt4 book ai didi

java - java中的socket问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:49 24 4
gpt4 key购买 nike

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

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() 的一端,当我尝试在缓冲区中写入时,出现错误。

最佳答案

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

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

什么是java.net.SocketException:软件导致连接中止:套接字写入错误含义?这意味着连接已关闭,但您尝试向套接字写入一些数据。但我可能是错的。

关于java - java中的socket问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com