1
I’m creating an app that needs send and receive information of a web page through Sockets, the server in Java I managed to do, but I do not know much about JS, could help me to do the client that sends and receives messages in Javascript for web and connect to this Java server I made?
Server code: Java server.
public static void main(String[] args) throws Exception {
System.out.println("Inicia servidor.");
ServerSocket server = new ServerSocket(3000);
System.out.println("Aguardando conexão.");
Socket socket = server.accept();
System.out.println("Conexão estabelecida.");
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
PrintStream out = new PrintStream(output);
while (true) {
String mensagem = in.readLine();
System.out.println(
"Mensagem recebida do cliente [" +
socket.getInetAddress().getHostName() +
"]: " +
mensagem);
if ("FIM".equals(mensagem)){
break;
}
out.println(mensagem);
}
System.out.println("Encerrando conexão.");
in.close();
out.close();
socket.close();
System.out.println("Encerrando servidor.");
server.close();
}
Possible duplicate of Socket and Websocket
– NoobSaibot