Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Socket Server with JavaScript Client

I put together a Java socket server and client that can send messages to each other, I want to use JavaScript as a client, but... Here is what happens when I'm hosting the Java server and load the JavaScript client.

JavaScript:

    var connection = new WebSocket('ws://127.0.0.1:9005');
connection.onopen = function () {
  connection.send('Ping');
};

This Prints in the chrome console:

WebSocket connection to 'ws://127.0.0.1:9005/' failed: Error during WebSocket handshake: Invalid status line
WebSocket Error [object Event]

What I'm trying to do is send "Ping" to the Java server, but instead it sends all this stuff than disconnects...

This prints in the Java server console:

Bread Server running...
New Connection From: 127.0.0.1:51948
127.0.0.1:51948: GET / HTTP/1.1
127.0.0.1:51948: Host: 127.0.0.1:9005
127.0.0.1:51948: Connection: Upgrade
127.0.0.1:51948: Pragma: no-cache
127.0.0.1:51948: Cache-Control: no-cache
127.0.0.1:51948: Upgrade: websocket
127.0.0.1:51948: Origin: http://markstuff.net
127.0.0.1:51948: Sec-WebSocket-Version: 13
127.0.0.1:51948: User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36
127.0.0.1:51948: Accept-Encoding: gzip, deflate, sdch
127.0.0.1:51948: Accept-Language: en-US,en;q=0.8
127.0.0.1:51948: Sec-WebSocket-Key: D4Epyc7LwvPdfeDWG0sY2g==
127.0.0.1:51948: Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
127.0.0.1:51948:
ERROR: Software caused connection abort: recv failed

JavaScript Client: markstuff.net/socket/ - markstuff.net/socket/client.js
Java Server: pastebin com cdZjUWQp
Java Client: pastebin com efEeezcR

like image 584
Mark Mark Avatar asked Feb 07 '16 20:02

Mark Mark


People also ask

Can JavaScript open a socket?

The JavaScript Socket object lets you connect to any server on the Internet and to exchange data with this server. The Socket object provides basic functionality to connect to a remote computer over a TCP/IP network or the Internet.

How do you send data from client to server in socket programming?

Create a socket with the socket() system call. Initialize the socket address structure as per the server and connect the socket to the address of the server using the connect() system call. Receive and send the data using the recv() and send(). Close the connection by calling the close() function.

Is socket A server or client?

Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines.

What is the difference between socket and WebSocket?

WebSocket is a protocol, Socket is an endpoint - got it thanks. Also, a simple google search returned "The current API specification allowing web applications to use this protocol(WebSocket) is known as WebSockets" (note the plural).


1 Answers

Sounds like you are trying to connect a Websocket client to a bare socket listener in server side. notice that Websocket is not equal to socket. Websocket is a protocol on top of a tcp connection (like how http is on top of tcp) .
so you should either change your server implementation to a Websocket (you can find many java Websocket libraries in internet) or change your client to a socket.
I'm not sure how to create a socket connection in JS but this topic maybe helpful for this problem: Connecting to TCP Socket from browser using javascript

like image 81
ayyoob imani Avatar answered Sep 18 '22 13:09

ayyoob imani