I'm making an online game using ObjectOutputStream... to exchange data. Since I have different types of data I'm using the write/readObject() functions only. I was wondering if sending a String for commands was good practice or if there is a better, safer solution.
When I say I send commands with a String, for example I have a chat and I want to ignore a user, so I send to the server "block +username"; if I want to add a friend I send "addfriend +username", etc.
Well, using serialized objects might create lot of interoperability work if you are going for a serious installation. It also can become a bottleneck. I would (besides the obvious of using any other messeging protocol) stick to DataOutputStream if you look for a compact home grown protocol.
Sending strings as serialized java objects is the most suprising thing to do (and wont easily allow you to have client or servers in different languages).
If you want to be cool, use JSON and Websocket. :)
Related
I have a project that requires me to create a Java server which will be connected to a MySQL database. This server is going to handle requests from clients, to send them data from the database.
The request from the clients will be:
check if a User is registered in the database
add User to the database
get a list of Users and which of them are on-line(this is where I use the HashMap)
After some searching I've concluded in using NIO, so I won't get too many threads to handle multiple Client requests. My problem is that I can't understand how you can retrieve data from the channel when you want to send, for example, a List or a HashMap. I mean, I've seen how the read(buffer) method works. I just can't understand -for example- how do you get th HashMap object back from the buffer, or how you retrieve any kind of "structured" data for that matter. If someone could explain(maybe with an example), that would be fantastic.
Maybe there is another way to communicate the data that I need, that would be easier for me to understand. I don't know. Your insight is greatly appreciated.
P.S. : My problem isn't that I don't get it because of the NIO, I have the same problem with the typical Input/Output Streams.
I should mention that the actual project is to create a Java server and the clients will be android devices. But since I'm a bit of a newbie, I thought I'd start off by testing the communication between two desktop, Java, applications before going for the android.
I mention this because I've seen something about Java RMI that allows you to use methods of your server remotely, but I think that you can't use it in Android.
You can read and write objects using the serialisation mechanism. The classes that are involved are ObjectOutputStream and ObjectInputStream. They are stream based though, so they don't fit well in the nio model. They are covered in the official tutorial: http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html
An alternative is using Google protocol buffers.
I need to pass data between c++ program and a Java GUI that is showing that data. I can put that data in a class but the c++ program could be running on linux(raspberry pie) and java may or may not be on windows. What options do i have?
Kindly help me for same machine processes and also if they are on different machines.
P.S.
On different machines internet connection is available.
You may want to implement some serialization.
I suggest using a simple textual serialization format like JSON (but you might consider also YAML or even XML). There are many JSON libraries available, like jansson (in C), JsonCpp (in C++) and several for Java.
Of course, you need some form of Inter-Process Communication. This can be sockets or pipes. Read e.g. Advanced Linux Programming or some other tutorial. Maybe have some Event Loop (e.g. libev, libevent) or even use JSON-RPC (or perhaps some HTTP server library)
You could use binary serialization like XDR or using libs11n but it is usually not worth the trouble.
What is the best way to send "messages" from PHP script to Java program in real time. PHP script and Java programs are both working at the same work station with OS Windows. Maybe some kind of client/server? The main feature is real time; that's why I don't want to use files.
PS: I'm going to send logger messages (php) and display (java) them at OS system tray tooltip.
PPS: I'm real noob in Java; it will be my first Java program. :)
Thank you.
You could use sockets (probably UDP, but depends on your needs). This way, if in the future you will need to put scripts and Java programs on different machines, you'll be able to do that without modifing the code.
In addition, once you established a communication protocol between client and server, this solution is language independent. So it's easy switch from PHP to another scripting language (the same for Java).
This depend on how heavy weight your application is.
If it is your first program and it is just a little project, a possibility is to open a socket on the server, connect to it with a client and send the data as a string, make your php program the client and java program the server.
Their are things that you can borrow to avoid doing everything on the low level. But they will add weight to your program, for example using a JSON/XML parser to serialize(make the messages into bytes readable on both side) the message instead of using your own format.
Or, use a framework like JAX-RS to quickly and easily (for people familar with it, you may need some time to understand it because it is quite different from writing plain java program) to build a little web service like professionals would do.
Possibilities are:
Send your data as a POST to jsp page.
Make your java code read your php logs.
use queuing systems like RabbitMQ, AciveMQ, Redis etc.
For simplicity use a database table as exchange medium.
It is also easier to debug.
(It is asynchrone, one side, PHP or Java, may be down. Performance is fast, as DB-Server will keep as much in memory.)
My universities peer to peer communication course uses an in house client/server program for demonstration and (i think) extending it is part of the assessment. The program we use is written in java and uses serialisation for the network communication.
To get a better grip I want to try reimplementing the protocol used in objective c, but googling around I cant find any information on using serialised data between languages. I would like to keep this as simple as possible, ideally be able to drop my replacement server/client onto a network and have it behave.
Edit Didnt actually ask a question there.
Is it possible to communicate between the two serialised formats, How can I make this work without reverse engineering the format java uses.
I would recommend avoiding writing (de)serialization support of java's native serialization in another language.
If you can change the existing Java server and clients, use a more language agnostic serialization format.
Assuming that you are not allowed to make that sort of change, I would define the new protocol, and implement a bridge in Java. The bridge (process) would establish a connection on behalf of each client that connects to it, and translate messages between the Java serialized and language agnostic form. This will provide a good migration strategy.
Java serialization protocol (if it's built-in default Java serialization) is documented, so you won't have to reverse engineer it - check this article and this link. However, if you can, use JSON, XML or XML-RPC; it will be much simpler than creating Java serializer/deserializer in another language.
I know that the Java can use the Socket Programming to send an Object. Apart from socket programming, anything other way to do it?
Java's Remote Method Invocation (RMI) is probably the easiest and most widely supported way.
Java's Advanced Socket Programming describes marshalling objects over a socket.
Control a high-speed robot hand to type it in.
Pretty well every other thing you can do will be a layer built on sockets.
RFC1149!
Attach rubber band to computer A
Put Object in rubber band.
Pull back, aim at Computer B.
Let go.
Via web service for example. But its build on top on sockets again.
SOAP (Simple Object Access Protocol).
Serialize the object, write to a file, copy the file if the computers are network connected if not use a removable disk and deserialize it.
Objects can be transferred via a shared database.
I work near a production system that's been doing this for 10 years.
Ironically, except in rare cases, DB connections are also implemented via sockets.
No. Sockets are how computers communicate. Other than writing the data to some media and physically transporting it between computers, you will have to use sockets.
At the lowest level, data is transferred over sockets as bytes. So first you need to serialize your object to bytes, then you can send it, then on the other side you need to deserialize the object from the bytes.
Approaching your question less literally, there are Java libraries that handle the serialization automatically and hide the nastiness of dealing directly with sockets. I recommend KryoNet. KryoNet can do remote method invocations, and a lot simpler and more efficiently than Java's built-in RMI support.