gpt4 book ai didi

apache-spark - 为什么流无法连接到Java套接字客户端?

转载 作者:行者123 更新时间:2023-12-03 15:03:43 24 4
gpt4 key购买 nike

我正在研究Spark流处理以处理实时数据,并且构建了示例SparkCount的wordCount,然后可以运行以下示例:

/bin/run-example org.apache.spark.streaming.examples.JavaNetworkWordCount local[2] localhost 9999

而且,如果我在另一个终端中运行 nc -L -p 9999,那么我可以在该终端中键入字母,并且该示例可以接收字母并给出正确的结果。

但是我开发了一个Java套接字客户端来将内容发送到9999端口-该示例为什么不能接收它?我认为该示例仅监视9999端口,并从该端口接收任何内容。

这是Java代码:
    File file = new File("D:\\OutputJson.dat");
long l = file.length();
socket = new Socket();
boolean connected = false;
while (!connected) {
//not stop until send successful
try {
socket.connect(new InetSocketAddress("localhost", 9999));
connected = true;
System.out.println("connected success!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("connected failed!");
Thread.sleep(5000);
}
}
dos = new DataOutputStream(socket.getOutputStream());
fis = new FileInputStream(file);
sendBytes = new byte[1024];
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
sumL += length;
System.out.println("sent:" + ((sumL / l) * 100) + "%");
dos.write(sendBytes, 0, length);
dos.flush();
}
if (sumL == l) {
bool = true;
}

此Java函数始终返回以下错误:
java.net.SocketException: Socket closed

我已经开发了另一个Java类来接收来自此发送套接字的数据,并且它可以正常工作,为什么不能通过此套接字触发接收?

最佳答案

从内存来看,我认为我使用了ServerSocket。该代码是这样的:

public void sendMsg(String msg) throws IOException {
ServerSocket serverSocket = null;
Socket clientSocket = null;
try {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
out.write(msg);
out.flush();
out.close();
} finally {
try {
clientSocket.close();
serverSocket.close();
} finally {
clientSocket = null;
serverSocket = null;
}
}
}

关于apache-spark - 为什么流无法连接到Java套接字客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23128461/

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