title: How can i use socket communication between java server and javascript client?
body: <p>I'm trying to connect java Server and Javascript client with socket.io. When i see the debugger at browser, it looks like the data is being received, but i'm getting this error: "Reason: CORS header 'Access-Control-Allow-Origin' missing" and i am not being able to print data at client-side.</p>
  
  <pre><code>import...
  public class MeuServerSocket {
      //initialize socket and input stream 
      private Socket socket = null;
      private ServerSocket server = null;
      private DataInputStream in = null;
  
      public MeuServerSocket(int port) {
          // starts server and waits for a connection 
          try {
              while(true){
              server = new ServerSocket(port);
              System.out.println("Server started");
  
              System.out.println("Waiting for a client ...");
  
              socket = server.accept();
              System.out.println("Client accepted");
              ObjectOutputStream saida = new ObjectOutputStream(socket.getOutputStream());
              saida.flush();
              // send available data from server to client
              saida.writeObject("Texto enviado 123...");
  
              // takes input from the client socket 
              in = new DataInputStream( 
                  new BufferedInputStream(socket.getInputStream())); 
  
              String line = ""; 
  
              // reads message from client until "Over" is sent 
              boolean fim = false;
              while (!line.equals("Over") &amp;&amp; !fim) 
              { 
                  try
                  { 
                      line = in.readUTF(); 
                      System.out.println(line); 
  
                  } 
                  catch(IOException i) 
                  { 
                      fim = true;
                      System.out.println(i.toString());
                  } 
              } 
              System.out.println("Closing connection");
  
              // close connection 
              socket.close();
              saida.close();
              in.close();
              }
          } catch (IOException i) {
              System.out.println(i);
          }catch(Exception e){
              System.out.println(e.toString());
          }
      }
  
      public static void main(String[] args) {
          MeuServerSocket server = new MeuServerSocket(5000);
      }
  }
  </code></pre>
  
  <pre><code>
     var socket = io('http://localhost:5000');
     socket.on('connect', function () {
      socket.send('hi \nOver');
  
      socket.on('get', function (msg) {
        // my msg
        console.log('msg: '+msg)
      })
  
      socket.on('disconnect',()=&gt;{
          console.log('disconnected')
      })
    })
  </code></pre>
  
  <p>When i look at Firefox network, i see that the data was sent inside one of the packages...</p>
  
  <p><a href="https://imgur.com/vDAS00B" rel="nofollow noreferrer">https://imgur.com/vDAS00B</a></p>