I'm attempting to venture into non-blocking networking using Java and I had decided on using the Netty Networking Library to do so. I currently have a fully working server/client application that uses Strings to handle the networking functions. The server/client will dissasemble the strings and get the data from it; However this is not what I need to be doing.
I've been looking at the DataOutputStream and it seems it has the functions that I would like to be able to use over a network.
writeByte/readByte
writeBytes/readBytes
writeChar/readChar
writeDouble/readDouble
writeShort/readShort
writeFloat/readFloat
writeInt/readInt
writeUTF/readUTF.
Currently everything I've been doing has been in UTF Format, basically. I've just been sending bytes using (bytes[])String
I'm having a very hard time figuring out how to implement a system like this. I don't want to continue using this inefficient string based networking. It's a waste of bandwidth and I'm not learning anything doing it.
So, I found what I was looking for built into netty, I've been using it for a few days now and haven't posted back.
Inside of netty there is a ChannelBufferInputStream and a ChannelBufferOutputStream these both take in the ChannelBuffer as a parameter and allow similar functions to that of a DataStream. It has really helped me with my networking.
Related
I am trying to stream media from a server to a client. I was going to use RTSP, but in RTSP the client tells the server what to do.
I need to be able to control the media the client gets from the server. The Server tells the Client what music he wants to play.
Is there an other protocol I can use or an other way I can work around? Maybe I am wrong and you can still do this using RTSP. I need to use UDP, as I need to use multicasts, and I dont want to have to build my own protocol to keep the client synchronized.
If you need more infos, dont hestiate to use the comments field below!
Edit
This application is done in Java. I would be happy, if you could maybe tell me a mechanism that works well with Java. I am currently using the Netty library, which looks to be pretty useful (although I have not figured out how to use it yet). I don't need an answer that is based on pure network knowledge, I know how I could build a whole streaming ecosystem. I need to get this working on Java 8, without having to do the handling of the basic protocol stuff myself.
Take a look at libjitsi which is an open source Java audio/video library which can be used for streaming with encryption. It does not use Netty but it looks to be mature.
I have a WebSocket server in C (using libwebsockets) which differentiates between a few subprotocols. I now need a Java Client which is able to use different subprotocols. I tried TooTallNates Java-Websocket, but i think it dosent support different protocol types. Also i tired jetty but i cant make it work properly also it look a bit to complex for my task. i just want the possiblity to read and write messages using websocket and i dont need all those fancy extra features. Which libraries would you suggest?
If you just want to create a basic Server, you can use the ServerSocket already in java, which is pretty easy. I am currently working on a sort of groupchat in a command-prompt and I've been using this method. My first answer, so feel free to ask back. (Or give feedback)
I'm developing a distributed application, and I need to connect a client Java based to a server C++ based. Both of them will need to send information to each other, but I need them to be able to do things while waiting for the information, and they don't know when they are gonna get new information, or send information.
How can I achieve this? Now I'm trying to implement a basic communication with Sockets, but I don't really get to communicate them. I have read that using sockets + threads is usually a good approach for client-server apps.
Could you please recommend me some web or book to read about this, or send me some example code to learn?
Do you think that i should use other approach, better than sockets? maybe a higher level library (i would need it for c++ and java) or a totally different way?
EDIT:
I will add some extra information.
What I would love to achieve is the following:
My C++ program has a main loop, where I would like to have a call like GetUpdatedDataFromRemoteDevice() where I read the new values of some numerical variables that previously got updated from the net (the socket, for example).
Eventually, the C++ program will need to send a message to the remote device, to tell him to send other kind of data, and after that, keep getting the updated values.
From the Java program (remote device) the application running is an interactive touchable screen, that cant get blocked by the network transmissions, because it must keep working for the user, so all the networking should be done in a separated thread.
That thread, should connect to the server, and when a button is pushed, start to send the data (4 changing numerical values) in a loop until another event happens.
It would be nice also to be easily re-connectable to the server.
ICE is a modern and good library for distributed applications:
many languages as C++ and Java
many platforms
GNU GPL
good performance
easy to use
First, you define the messages you want to exchange between server and client.
Then, you implement the C++ and Java source code to handle these messages.
More info at http://zeroc.com/ice.html
Have fun ;-)
EDIT: I have to use ACE in some projects. I can tell ACE is very old, maybe mature, but uses outdated C++ coding rules :-(
Therefore ACE is not as easy to use as STL or BOOST. Moreover, ACE is not really efficient... I prefer ICE ;-)
I don't know what your application is but robust client server socket programming is pretty hairy task to do properly. Hardware byte order, String encoding, Network errors, retries, duplicate messages, acks etc.. require lots of good design and careful programming. You need to get it work well as single-threaded before even thinking using multiple threads.
Unless you need instant notifications from server to client I suggest that you use HTTP as protocol between client and server. Client can poll server occasionally for new messages.
Anyway the problem has been solved multiple times already.
http://activemq.apache.org/
http://www.rabbitmq.com/devtools.html
http://www.cs.wustl.edu/~schmidt/ACE-overview.html
I did something of this sort once. In my case it was easier to connect my C++ app to a local Java app using JNI and then have the two Java apps talk to each other.
I'm currently considering using java in one of my projects(for reasons unrelated to networking). At the moment I'm using C++ and a custom protocol built on top of UDP. My problem here is that while the added efficiency is nice for sending large amounts of realtime-data, I'd rather have something along the lines of RPCs for pure "logic actions" such as login. RPC's in C++ are hard to do though, since standard C++ itself has no notion of serialization.
In another answer, I found Java's RMI, which seems to be similar to RPCs, but I couldn't find how efficient/responsive it is, nor whether it could be plugged into my existing UDP socket, since I don't want to have two ports open on my server program.
Alternatively, since I think Java has serialization, I could implement RPC's myself, depending on how straightforward deserializing an arbitrary stream of objects in java is. Still, if this would require me to spend days on learning the intrinsics of java, this wouldn't be an option for me.
If you're interested in RPC, there is always XML-RPC and JSON-RPC, both of which have free/open-source C++ implementations. Unfortunately, most of my development has been in Java, so I can't speak to how usable or effective they are, but it might be something to look into since it sounds like you have already done some work in C++ and are comfortable with it. They also have Java implementations, so you might even be able to support both Java and C++ applications with XML-RPC or JSON-RPC, if you want to go down that route.
The only downside is that it looks like most of these use HTTP connections. One of the things you wanted to do was to reuse the existing connection. Now, I haven't looked at all of the implementations, but the two that I looked at might not meet that requirement. Worst case is that perhaps you can get some ideas. Best case if that there might be another implementation out there somewhere that does what you need and you now have a starting point to find it.
The use of RPCs as an abstraction do not preclude the use of UDP as the transport layer: RMI is an RPC abstraction that generally used TCP under the hood (last time I looked).
I'd suggest just coding up a Java layer to talk your UDP protocol: you can use any one of many libraries to do it and you don't have to discard all your existing work. If you want to wrap an RPC layer around your protocol no reason why you can't do that: create a login method that sends the login UDP packet and receives the appropriate response and returns it.
If it's a remotely serious project, you should probably take a look at Netty.
It's a great library for developing networked systems, has a lot of proven production usage and is well suited for things like TCP or UDP client-server communication. I wouldn't go reinventing this wheel unless you really have to :-)
As a bonus they have some good examples and documentation too.
I'm writing a watchdog-style program in Java - that is, it will be running constantly.
I want to be able to somehow send input and receive a response using PHP.
What is the best way to do this?
Thanks!
EDIT: Just to clarify, the Java and PHP are running on the same machine.
A really simple solution,not as simple as the "file solution" by Itay), but more light-weight than HTTP or XML-RPC would be using a plain socket-based solution, as simple as illustrated here.
That would fit nicely for your "String back and forth" protocol.
There are a million ways to do this: one comparatively easy way is using a networked RPC system such as XML-RPC. You can quite cleanly call from PHP and receive the responses back.
very crud and not the most efficient
A socket like way:
Create file A with permissions to both the Java process and php process.
Java writes to the file, only when it is empty (or concatenate)
PHP reads the file and deletes it.
Do the same but the other way around for the PHP to send input to Java.
Again, this is crud and as you can see in the search result above, there are ready made solutions.
I'd do this by running the Java program in a servlet container (Jetty or Tomcat would do just fine) and use HTTP request/response between the PHP and Java. The Servlet will effectively provide an HTTP interface to your Java program. On the PHP side you can use cURL, file, or even fopen to perform GET requests, or cURL to perform POSTs and retrieve responses.
The data format you use for transactions is up to you, JSON seems like an easy choice, there's built-in support in PHP and excellent libraries for Java.
Maybe I'm missing something with all those giant libraries floating around, but what about using a socket and communicating over TCP? Can't get much simpler for transmitting simple data like strings and should be fairly performant.