i have the problem when i want to try web socket technology on my application.
but i want to ask about requiretment of web socket.
1) is posible i'm using Server with javascript language ex is socket.io
var io = require('socket.io').listen(3001);
io.sockets.on('connection', function (socket) {
socket.emit('news', {hello: 'can you hear me'});
});
and the client using Java language example is socket.io.java
i'm using this method to get message from server
public void onMessage(JSONObject json, IOAcknowledge ack) {
Log.d("We received a message: " , json.toString());
}
with my socket server is
SocketIO socket = new SocketIO("http://127.0.0.1:3001/news");
2) is support to all machine? or browser?i'm newbie on web socket programing, please help me
thanks for your answer...
Yes, you can build the server in any language you want, but web sockets are not a simple socket, they are sockets that "follow" a specific protocol, so make sure you read the specification of this protocol and implement it on your server so any browser will be able to stablish a connection to it.
And you can also write a client in any language, but if you're not using javascript then I don't see why you would use web sockets instead of using just a simple socket. But if you want to, just take a look at what data a browser sends to a web socket server and emulate it from your own client.
To elaborate on Delta's answer, "websockets" is a variation of the HTTP protocol where the client sends an HTTP request message, the server sends an HTTP response, and then the client and server use the still-open TCP/IP connection to do "other things". The request and response contain special headers which allow the client and server to agree to use the connection in this way.
In order for this to work, the client and server both need to understand at least a subset of the HTTP protocol in order to do the initial "handshake". Hence you can't simply use a websocket client to talk to a plain socket server ... or vice versa. (If you try to do that, both ends will see unexpected stuff / protocol errors. And the websocket end should promptly close its end of the TCP/IP connection.)
Having said that, a websocket client and a websocket server can be implemented in just about any modern programming language. (And the same goes for plain socket clients and servers.)
Sockets are an operating system thing. You can use them with any language providing the relevant interface or glue code to the operating system calls implementing them (e.g. on Linux: socket(2), connect(2), accept(2), listen(2), poll(2), recv(2) etc...)
But if you are newbie about sockets, I strongly suggest reading a good network programming book.
Related
I'm developing a Client application which talks to a Server using WebSockets. The Client is in C++ and the Server is in Java.
Can anyone suggest me any library which I can use on both Client side and Server side for communication using web sockets.
I never had experience with WebSocket, but try library cURL (libcurl). It was easiest for me to write clients for HTTP and FTP, using it. It have to help (but curl is useful just for clients, not for server).
If you are talking about sockets, normal sockets that connect on a port and wait for a connection on the server side and that connect to a given address on the client side, then I would recommend the boost asio socket on the c++ side and the standard java socket on the java side.
Just remind yourself of making sure that you transmit the datatype you expect.
Another cool implementation for both, java and c++, is ZeroMQ. I would recommend to take a look at it because it is easy to use and has implemented some really cool communication patterns.
I want to create a web utility which will use a local port for sending a TCP/IP request.
Does it possible to use a client side port to send a TCP/IP request?
I know it is possible if we send a TCP/IP request from a web server and the specific port is allowed on server. But I want to send the request using the client side port.
What would I need to do? Should I create a Java Applet/Plugin or is it possible using PHP/.net?
It depends what you want to do, but fundamentally you can't initiate operations on the client using server side code like PHP.
Javascript is capable of some networking operations using XHR (Ajax) and WebSockets, and wrapping libraries such as socket.io. Perhaps that will suffice for your needs.
A Java Applet will be more capable, but will be restricted in what it can do unless you sign the code. And of course will be slower to start up for the client.
There is an existing service that i would like to write a dummy service (using Netty) for. It will be used for testing purposes.
The existing client code fragment for the service looks like:
Socket socket = new java.net.Socket();
socket.connect(new InetSocketAddress("localhost", 8080), 10000);
socket.setSoTimeout(20000); // set a timeout of 20 seconds
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// write some string to the server and wait for answer
out.println("SomeCommand");
// server has written some answer, read it
char[] c = new char[2];
ir.read(c, 0, 2);
String cs = new String(c);
if ("OK".equals(cs.toString())) {
// write some more string's to the server
}
// we're done, close the connection
out.println("BYE");
out.close();
socket.close();
Is Netty the right framework to create a server for java.net.Socket connections? (If not, which framework should be used, if any?)
I am trying to find a way to start with Netty using the QuoteOfTheMoment example. The QuoteOfTheMomentServerHandler does basically what i want, upon the incoming message, return some answer so that the above snippet can read the answer using the inputstream but the above socket cannot make a connection to the QuoteOfTheMomentServer. The error is "connection refused".
[EDIT] More clarification:
The problem (i think) is not connecting or the port i use. Let me try to better ask the question:
I just started with netty (no nio experience) and am not familiar with the different types of channels, pipelines and what not.
The server should, like a servlet request/response (like, not http or trying to rebuild a http servlet impl), react on a inputString written to the output-stream as in the code fragment and write back some string/bytes to the input-stream as in the code fragment, so the client only then moves on. So the connection should stay open but also be synchronous, the client waits for answer from the server. If i use the example "Writing the Server Side of a Socket" in the java tutorial i am able to get it working for the client. But i want to utilize the thread handling etc. from netty.
The QuoteOfTheMomentServerHandler seems as server side implementation what i want but can that setup handle the given client code ?
So the question is which kind of pipeline, channel or something like that should be used given the way the client works ?
Again, the client and server are existing. I want to build a dummy server implementation to work with the existing client.
Netty is a TCP/IP framework. So yes if you are developing a TCP/IP server this toolkit is good to use.
I assume you are getting a error when trying to connect the client to the server. Also the server should also be running.
When getting a the connection refused error there are a couple of thine to check. First one is the firewall(if any) on the server allowing connections to port 8080? Secondly from your client machine try open a telnet session to the server something like:
Telnet yourserverip 8080
This opens a socket connection to the server. If you get a error message Google it.
The last one is that you might be running a server like tomcat, glassfish, IIS which uses port 8080 already. Try a non standard port like 10810 for example.
UPDATES:
If you are new to netty please read the users guide found here http://netty.io/docs/stable/guide/html/.
I had a look at the Quote of the moment service and I do believe I found part of the problem. The Quote of the moment service is a broadcast UDP/IP client and server. UDP is a much more lightweight "version" of TCP IP. It does not guarantee delivery to the client or server and it is broadcast. UDP is sort of like a radio broadcast as it is generally not targeted to a specific IP but broadcast over the entire network. Thus you normal TCP IP connection will not be able to work on the UDP server.
See this link on how to write a UDP Client http://systembash.com/content/a-simple-java-udp-server-and-udp-client/.
I would suggest that you convert the Quote of the moment server from UDP to TCP/IP server as this will give you some practise in creating a TCP/IP server without getting into too much detail. Once you are comfortable with that you should be able to start once from scratch.
Just remember that Netty handles the NIO part for you. It is a higher level framework based on NIO thus hiding a lot of the detail from you. You dont need to know NIO that well to use netty but you need to understand the Netty concepts well.
I had set up a system that had a Java program running on a server and a Java applet embedded in a page on a client's browser and the two communicating via Java sockets. I'm wondering if I can switch over from a Java applet to just HTML5 and javascript, using a WebSocket on the client side for communication with the Java socket on the server.
Is there a simple way to make a WebSocket communicate with a Java Socket?
Is there a simple way to make a WebSocket communicate with a Java Socket?
From what I understand, WebSocket works by the client side opening a port 80 connect to the server side, and sending a variant HTTP 1.1 request to the server to negotiate a WebSocket connection. If the server recognizes this, it will send a suitable response, and then allow the still open TCP connection to be used for full-duplex client-server interactions.
It looks like it would be possible to quickly put together a server-side that just understood WebSocket negotation and not full HTTP. However, I think you are better off looking at existing WebSocket implementations, including those embedded in HTTP servers / protocol stacks.
This Wikipedia page compares a number of WebSocket implementations, and should help you in deciding which server-side implementation to use.
But to directly answer your literal question, a WebSocket client can only connect to a WebSocket-aware server; i.e. that one that can perform the initial negotiation. (On the client side, you could implement starting from a bare Socket, but you would need to implement all of the "HTTP stuff" on top of that ... for the setup phase.)
Nope, you cannot communicate using regular sockets with client WebSockets.
WebSockets are special HTTP requests, with an upgrade in the HTTP Header, and a standard protocol to establish a connection (see the official RFC doc).
I am looking into trying to do UDP/TCP hole punching using a servlet running on Google's AppEngine.
I would be using primarily the Java EE library. But I don't quite see how to forward a network connection request from the client to the other client who is acting as the P2P "host".
Is there something I'm missing in the ServletRequest/ServletResponse classes?
Don't think you're going to be able to handle UDP. However, for TCP, if you override the service method in the servlet and handle the "CONNECT" verb, you can then read from and write to the input and output streams. From the client side, you should be able to utilize this through a HttpURLConnection or something like Apache HTTP Client.