Hello! I have a problem, I’ve been digging for two days, but I haven’t solved it yet. There is a server in Java, and there is a client in the script. The problem is that I cannot send a message from the server to the client, although when I send response data to the client when connecting, everything works (there is a connection), and then I cannot send or receive anything.

Server code:

public class Main { static List<Client> clients = new ArrayList<Client>(); public static void main (String[] args) throws Exception { ServerSocket server = new ServerSocket(1340); while (true) { final Socket client = server.accept(); Client s = new Client(client); clients.add(s); getConnect(clients.get(clients.indexOf(s))); for (Client c:clients ) { c.sendMassege("YOU LALKA!!!");//уже не работает } } } static void getConnect(Client client) throws Exception { String data = new Scanner(client.in,"UTF-8").useDelimiter("\\r\\n\\r\\n").next(); Matcher get = Pattern.compile("^GET").matcher(data); if (get.find()) { Matcher match = Pattern.compile("Sec-WebSocket-Key: (.*)").matcher(data); match.find(); String response = ("HTTP/1.1 101 Switching Protocols\r\n" + "Connection: Upgrade\r\n" + "Upgrade: websocket\r\n" + "Sec-WebSocket-Accept: " + DatatypeConverter .printBase64Binary( MessageDigest .getInstance("SHA-1") .digest((match.group(1) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11") .getBytes())) ) ; client.sendMassege(response); } } } class Client { protected Socket client; protected InputStream in; protected OutputStream out; public Client(Socket client) throws Exception { this.client = client; this.in = client.getInputStream(); this.out = client.getOutputStream(); } protected void sendMassege(String k) throws Exception { if (k != null & k.length() > 0) { out.write((k+ "\r\n\r\n").getBytes(),0,(k+ "\r\n\r\n").length()); out.flush(); } } } 

Client code:

 function connectToServerOne(){ var socket = new WebSocket("ws://localhost:1340/"); socket.onopen = function() { alert("Соединение установлено."); }; socket.onclose = function(event) { if (event.wasClean) { alert('Соединение закрыто чисто'); } else { alert('Обрыв соединения'); // например, "убит" процесс сервера } alert('Код: ' + event.code + ' причина: ' + event.reason); }; socket.onmessage = function(event) { alert(event.data); if(event.data == "YOU LALKA!!!") { socket.Send("NO YOU LALKAQ!"); } }; socket.onerror = function(error) { alert("Ошибка " + error.message); }; } 

I wrote to C # before that, everything is very simple in this respect, but it is necessary to do it on Javʻe, here I stumbled

  • one
    Here is a good library with examples of github.com/TooTallNate/Java-WebSocket - Vadim
  • those. without a third-party library in any way? Thanks for the library, of course I dig it, but this is not exactly the answer to the question. It seems that my example works, but I don’t understand how to organize data exchange, because after the “handshake”, streams don’t transmit anything and do not receive more. In c #, this role is performed by sockets and there all through them ... - chall2
  • one
    there the server part is written, what you are trying to do, if the library is used as a module, on Prakke, the solution to your problem. option for simple and encrypted channel. - Vadim

0