Problems with Sockets

Asked

Viewed 130 times

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();

}

1 answer

1


There are several ways to do this and ready libs. I’ll put here an example js native.

function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               alert("WebSocket is supported by your Browser!");

               // Let us open a web socket
               var ws = new WebSocket("ws://localhost:3000");

               ws.onopen = function()
               {
                  // Web Socket is connected, send data using send()
                  ws.send("Message to send");
                  alert("Message is sent...");
               };

               ws.onmessage = function (evt) 
               { 
                  var received_msg = evt.data;
                  alert("Message is received...");
               };

               ws.onclose = function()
               { 
                  // websocket is closed.
                  alert("Connection is closed..."); 
               };

               window.onbeforeunload = function(event) {
                  socket.close();
               };
            }

            else
            {
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
         }

In this function it checks whether your browser supports Websocket. Then it opens the connection in the url you should configure.

ws.onopen is the event that happens as soon as you log into the socket.

ws.onmessage is the event when you receive a message

ws.onclose is the event when the connection is closed

  • It would be interesting for you to make an explanation !! After all the AP made it clear that you don’t understand much about Javascript.

  • But I get an error using it in my browser next to the connected server: "Exception in thread "main" java.net.Socketexception: Software caused Connection abort: recv failed "

  • This problem is because your java code is closing the connection. System.out.println("Encerrando conexão.");

 in.close();

 out.close();

 socket.close();

 System.out.println("Encerrando servidor.");

 server.close();

  • Independent, it generates an error and closes the application, with or without connection termination scripts...

  • " Message received from client [0:0:0:0:0:0:0:1]: at java.net.Socketinputstream.socketRead(Socketinputstream.java:116) at java.net.Socketinputstream.read(Socketinputstream.java:171) at java.net.Socketinputstream.read(Socketinputstream.java:141)... "

  • what you put here was a message of success from received message

  • I managed to solve my problem, but thank you for your cooperation!

  • it would be interesting you put here what was happening, in case the problem is in javascript, correct in my reply

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.