i created a sever socket program to send a stream data to Apache spark.But data is received by spark after i close the socket or termination of program.i need to send data without closing socket and terminating program.
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class SocketServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();// establishes connection
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
Scanner scanner = new Scanner(System.in);
String s1 = "";
while (!s1.equals("end")) {
s1 = scanner.next();
dout.writeUTF(s1);
dout.flush();
}
ss.close();
} catch (Exception e) {r
System.out.println(e);
}
}
}
i can send data in stream using nc server nc -lk 9999.
EDIT -1 Tried with println
try {
ServerSocket ss = new ServerSocket(6000);
Socket s = ss.accept();// establishes connection
OutputStream ostream = s.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
Scanner scanner = new Scanner(System.in);
String s1 = "";
while (!s1.equals("end")) {
s1 = scanner.next();
pwrite.println(s1);
pwrite.flush();
}
ss.close();
} catch (Exception e) {
System.out.println(e);
}
Still not working.
Please help..
Copyright Notice:Content Author:「BlackPOP」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28980604/how-to-send-data-in-a-java-socket-program-without-closing-socket