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.
Related
So, forgive me if I'm too ambitious and this isn't possible, but I am wondering if it's possible to like set a variable while my program is running, have it closed, have the computer shutdown, and have the app start up again, and have that variable the same as it was.
I've only ever heard of people using servers or files, and so I'm wondering if this is possible.
It is not possible to store a variable forever in side your application. You'll have to either store in the HDD or send a web request to a server where they store values for you.
Build your own website using PHP. There are many free web hosting services. Host your website and your database. Send a HTTP request and you may write a JSON response from your server side.
If that's a lot of trouble, file saving method would be the easiest.
You'll need to write the state of your application out to disk somehow, there's no way around that. Note though this doesn't necessarily have to be a disk on the same machine that your app is running on.
Usually this is accomplished (in the Java land) by using a dB (mysql for instance), then using either plain JDBC to fire off SQL queries, or using an ORM such as hibernate (which will then use SQL underneath.)
You can use something called object serialisation to save the state of your objects to disk directly, and then recall them later. However, this is generally considered an ill advised, obsolete approach (and Oracle are planning to remove it entirely in a future version of Java, so definitely one to stay away from.)
I'm developing a multi-player turn based game for Android - a poker game.
I'm almost finished with the clinet game-play. Now I need to implement the multi-player thing.
I thought about using Web Services but I have no experience with that and I undertsand that socket-communication is faster.
I have some experience with writing socket-communication in Java:
Using ObjectOutputStream and ObjectInputStream to exchange data (game-states and turn actions in this case).
But I have a concern with this approach - the server-side is platform-dependent.
If I want to have an iPhone developer create the app for iPhone, he could not do this because the server expects Java code. Am I right?
And another question please:
Can I implement normal-socket communication and then later easily change it to secure-socket communication? It's just that I need to learn how to use SSL and I don't want to go into it right now.
Thanks!
This is an old question but relevant for me and hopefully others so here goes. I'm currently working on a project that involves multiple computers on different platforms using Java sockets. I'm using sockets because eventually I can use any language as long as I adhere to the protocol I wrote. I too have some predefined integer constants that control what data is expected in what order. For example, before I start some particular function, I send a particular integer, followed by the expected data. Anything that doesn't come in in the expected order results in an error. So as long as the language/runtime offers the ability to write integers and strings over a TCP socket, it should work. I think that covers the vast majority of major programming languages.
As for SSL sockets, I'm doing that also :) I started out using non-SSL sockets and then refactored to make it work with SSL sockets. I was surprised at how easy it is. I would recommend this git repo for sample code: https://github.com/jawi/ssl-socket-demo.
Eventually I plan to have the user who uses my app generate the key store and self-signed certificate locally when they first run the app as part of its initial setup. As long as they import that same key store into the app on every other node that they use in their network, they'll be able to talk to each other. This will ensure that the communication is encrypted in a way that is unique to their network. Of course this will require that they keep the key store file securely stored in their local file systems :)
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. :)
I want to create http socket connection for server in php for multiple client . how can I do that ? I need some resource .
First I was trying to create server in java .I create a server in java . And trying to reach from android application .But server can't find any client.But when I create client in java .It was working. How can I solve that problem ???
Take a look at this article:
Writing Socket Servers in PHP by Zend
Also give a try with Google:
http://www.google.com/search?aq=0&oq=php+socket+server+mul&sourceid=chrome&ie=UTF-8&q=php+socket+server+multiple+clients
Personally I think this would be a pretty bad idea, as already mentioned it lacks Threading and it's Socket support (imo) isn't really that adaptable.
The only plus side is that you can use fork to fork off another PHP process to handle a client, but you're getting very complex.
Another language would be much more suited for this type of development.
Note that even if you did do this in PHP, you'd probably have to rely on external services anyway, and possibly even end up writing at least some code in another language anyway.
You're trying to use PHP to do what? Mind you, I like PHP and work with it almost every day, but please do remember PHP in and on itself is based on request and response, and not very suitable for long running processes. In a manner of exercise, it might be interesting, but if you're trying to write a webserver from scratch using PHP, you might want to reconsider your choice of language.
That said, you can create a socket acting as a server, and listen to incoming packets. I do still think you're reinventing the wheel though.
though i love php and java, i wrote my socket servers in c++ running under lamp in an amazon ec2 cloud server. it is very, very simple to code and debug and safe and you can practically just copy/paste examples.
in the long run, i will probably develop a java solution because of portability and scalability, but the initial effort to get a c++ solution working is just so much less than implementing a java solution...
the first thing you must ascertain (find out) is whether your server allows you to open custom ports. amazon ec2 does and at this point in time (feb13), can be used for free for 12 months.
so, this is for you if you are in a hurry:
this here set of examples has all that you need to be up and running in no time.
Judging from the question title (the rest only makes it more confusing) you could use an existing package like http://pear.php.net/package/HTTP_Server to implement a webserver in PHP. It already contains all the socket code to accept client connections and stuff.
So what i have to do to find the server from different client
"Finding" is too broad a topic. Depends on your actual setting. On a LAN there are some protocols for discoverability. Otherwise you should just rely on a fixed machine name and port number for your instantiated server. You can connect to it as e.g. http://localhost:8007/ or whatever you've predefined.
We are trying to connect to a SQL Server instance used by the ACT CRM system. They have managed to lock things down so it isn't possible to connect to the SQL back-end using ODBC (there is some special utility that will add ODBC support that you can install if you purchase the primo version of the software, but that's just crazy).
The recommended method of connecting to theses databases is using an OLEDB connection.
Does anyone have any tricks/ideas/etc... for how to make and use an OLEDB connection from Java?
This doesn't have to be JDBC if that's not possible. All we really need to do is execute a SELECT query that returns two fields and parse those field values out for each row. I have very little experience with OLEDB, so 'use JACOB' might be a good answer, but I'd appreciate some details on what the COM calls would actually have to be.
I know this is old, but could help someone to know how I did it
I described in more detail how to do it Here.
That's not your problem. The problem is the way they have locked down the server. Basically on startup it looks for logins other than ACTUSER and removes them.
You can unlock it pretty easily though, then you will be able to connect in the usual way.
https://serverfault.com/questions/77712/sqlserver-need-to-access-an-act-database-for-data-migration
I've managed to unlock mine but I forget how... I think I started it in single-user mode then did some funny stuff involving decrypting a stored proc in the master database and editing it to remove that "functionality". That in turn involved using SQLTrace to see what commands ACT was sending.
I suggest you ask on Server Fault.
Java can not access OLEDB directly. You need to do this in another language like C++ or C#. Then you can access via JNI or external process. If you does not want write the native part of JNI self then you can use JACOB how you suggest it. But I think an external process take the request seem be simpler.
Or use: http://uda.openlinksw.com/jdbc/mt/jdbc-sqlserver-mt/
They develop all kinds of drivers. I used this company before...
Two ways to solve this issue.
Spawn from java an external proc (c#, c++, etc) that connects to SQLSrv using OLEDB and redirect the stdin, stdout and stderr to your java program.
Create a C# listener on a particular port and have java pass all the requests via a client to that listener.