Spark: why streaming cannot connect java socket client

I study Spark streams for real-time data processing, and I built a wordCount example of a spark stream, and I can run the example after: / bin / run -example org.apache.spark.streaming.examples.JavaNetworkWordCount local [2] localhost 9999

And I run "nc -L -p 9999" in another terminal, then I can type letters in this terminal, and the example can get the letters and give the correct result.

But I developed a java socket client to send content to port 9999, why can't it get it? I think the example just controls port 9999 and gets something from the port.

following java parts:

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

this java function always returns an error: java.net.SocketException: socket is closed

java , , ?

+4
1

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

Source: https://habr.com/ru/post/1536914/


All Articles