gpt4 book ai didi

java - 简单的套接字 I/O 问题……是吗?

转载 作者:行者123 更新时间:2023-12-01 15:39:59 25 4
gpt4 key购买 nike

这个问题不太可能帮助任何 future 的访客;它只与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the help center .




10年前关闭。




在下面的代码中,DisSemHelper 尝试与所有其他正在运行的 DisSemHelper 进程进行通信,包括它自己。请不要质疑我的动机,除非有什么明显的。 ConnectionListener 线程(在构造函数中启动)监听来自 DisSemHelpers 的连接,并且构造函数启动连接。问题是,我无法让基本的 readLine() 工作:它会导致 ConnectionListener 挂起。我只需要它来阅读一行。如您所见(已注释掉),我也循环尝试了它,但什么也没有。请帮忙!

已解决:我忘记了 autoflush (doink) 这是应该存在的,请注意“真实”:

PrintWriter out = new PrintWriter(helperSocket.getOutputStream(), true);

问题代码:
public class DisSemHelper extends Thread {
private int id;
private int semaphore;
private Clock clock;

private Vector<Integer> connectedHelpers;
private Vector<Socket> helperSockets;
private int localPort;

private int receivedSender;
private String receivedOperation;
private int receivedTimestamp;

/**
* @throws IOException
*/
public DisSemHelper(int id) throws IOException {
this.id = id;
this.semaphore = 0;
this.clock = new Clock();
this.connectedHelpers = new Vector<Integer>();
this.helperSockets = new Vector<Socket>();
this.receivedSender = -1;
this.receivedOperation = null;
this.receivedTimestamp = -1;
this.localPort = Common.portMap.get(id);

new ConnectionListener().start();

/* Create and store connections to all helpers */
for (int i=0; i < Common.NUM_HELPERS; i++) {
Socket helperSocket = null;

/* If not already connected with helper i */
if (!this.connectedHelpers.contains(i)) {

/* Retry connecting every second until target helper socket is ready */
Exception e = new ConnectException();
while (helperSocket == null) {
try {
Thread.sleep(1000);
helperSocket = new Socket("localhost", Common.portMap.get(i));
} catch (ConnectException ce) {
e = ce;
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException ie) {
e.printStackTrace();
}
}

PrintWriter out = new PrintWriter(helperSocket.getOutputStream());
out.println("" + id);

this.connectedHelpers.add(i);

this.helperSockets.add(helperSocket);
System.out.println("Helper " + id + " added socket from outgoing: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort());
}
}

System.out.println(this.helperSockets);
}

private class ConnectionListener extends Thread {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(Common.portMap.get(id));

/* Listen for connections from other helpers */
while (helperSockets.size() < Common.NUM_HELPERS) {
Socket helperSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(helperSocket.getInputStream()));

// String inLine;
// int connectedHelper = -1;
// while ((inLine = in.readLine()) != null) {
// connectedHelper = Integer.parseInt(inLine);
// }

int connectedHelper = Integer.parseInt(in.readLine());
System.out.println("Received helper ID");

if (!connectedHelpers.contains(connectedHelper)) {
connectedHelpers.add(connectedHelper);

helperSockets.add(helperSocket);
System.out.println("Helper " + id + " added socket from incoming: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

最佳答案

出现这样的错误是很常见的。原因可能是由于内容被拆分的方式,以及连接被解释为打开或关闭的方式。

这里对您的问题的一些解决方案有很好的解释:Java, sockets, BufferedReader, and readline hang ... :(

特别要注意关于“分 block ”的部分。我以前也遇到过这个错误——它非常邪恶。

补充:回复您的解决方案

作为对解决方案的回应,关于 autoflush :这很有意义。通过不刷新,很可能是数据根本没有被写出到流中。

关于java - 简单的套接字 I/O 问题……是吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8218994/

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