Implementing a protocol for a client-server socket communication in Java - java

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 :)

Related

Custom UDP protocol encryption without DTLS

Background Info
I'm writing a custom UDP protocol which is targeted for general-purpose use, but will probably be used in a game development setting. My protocol handles all the issues of UDP, reliability, ordering and fragmentation are all handled. I'm using UDP because of the flexibility as I can send some packets unreliable and others reliable.
The Problem
I want my protocol to be encrypted, and I'm also very concerned about MITM. I've read several questions of people who also want to encrypt their UDP protocols and most people recommend DTLS. However the problem with DTLS is that it seems that no one uses it. I can't find decent guides or documentation on how to set up a client/server program for my chosen language (Java). It looks like the only option is bouncycastle, however considering the fact that their client/server test programs won't work with each other, it's probably not a good idea.
I then decided to write the low-level packet receiving and sending code in C and use OpenSSL for the DTLS implementation. I would then call my C code using JNA. However, once again I could not find decent guides or tutorials on how to do DTLS. I could only find two ones which were somewhat helpful, the first one just went over the general C functions to call in which order. I got the impression that your application had to do the client verification yourself and since I have no idea how to do that it wasn't very helpful. The second one was just a raw client/server program which worked when ran but with closer inspection it seems to disable client verification.
The actual question
How would I go about creating my own encrypted transport system over UDP? I read a little about DHKE, but I don't know how to write a good implementation of it in Java using UDP and closer inspection seems that it doesn't prevent MITM. For my specific application I actually probably don't need a key exchange, the clients can actually have the key pair with the server installed beforehand. Would going this route work well? (I would probably just encrypt each packet body with the pair and send it to the server/client)

Pipelining or Otherwise Transferring Data Between Languages in Realtime

I'm working on a project, which I am not at liberty to discuss the core, but I have reached a stumbling block. I need data to be transferred from C++ to some other language, preferably Java or Python, in realtime (~10ms latency).
We have a sensor that HAS to be parsed in C++. We are planning on doing a data read/output through bluetooth, most likely Java or C# (I don't quite know C#, but it seems similar to Java). C++ will not fit the bill, since I do not feel advanced enough to use it for what we need. The sensor parsing is already finished. The data transferring will be happening on the same machine.
Here are the methods I've pondered:
We tried using MatLab with whatever the Mex stuff is (I don't do MatLab) to access functions from our C++ program, to retrieve the data as an array. Matlab will be too slow (we read somewhere that the TX/RX will be limited to 1-20 Hz.)
Writing the data to a text, or other equivalent raw data, file constantly, and opening it with the other language as necessary.
I attempted to look this up, but nothing of use showed in the results.
It seems like you are looking for a IPC (Inter-process communication). The easiest ones to implement are the socket and Pipes. I have added links which will help you decide which one to use. I have implemented named pipes and socket to transfer data at every 1ms. I did these implementations in python, c and pascal.
Here is the descriptions of all the IPC types
What's the difference between pipes and sockets
We had same issue where we had to share sensor data between one Java app to other multiple apps including Java,Python and R.
First we tried Socket connections but socket communication were not fault tolerant. Restarting or failure in one app affected other.
Then we tried RMI calls between them but again we were unhappy due to scalability.
We wanted system to be reliable, scalable, distributed and fault tolerant. So, finally we started using RabbitMQ where we created one producer and multiple consumers. It worked well for 2 years. you may consider using Apache Kafka.
You have options like Socket pipes, RMI calls, RabbitMQ, Kafka, Redis based on your system requirements now and in near future.

Sockets or RMI - perfomance and scalability

I am currently decide what kind of communication method/network protocol I am going to use for a new project.
What I can tell you about this project is that:
- It is Android/java based, using X amount of Android devices
- These devices should be able to send strings to each other over a local network. We are talking about small strings here. Small as in less than 100 characters.
- The amount of packages/transmissions being sent can vary "A LOT". I can't say how much unfortunately, but the network protocol needs to be as scalable as possible.
I have researched different kinds of possible solutions and is now deciding wether to use "Sockets" or "RMI"
As I have understood about RMI:
It is easier than Java sockets to implement and maintain (smaller amount of code)
It is "a bit slower" than sockets, as it is a new "layer" build on top of Sockets
There may be some scalability issues (if this is true, how "serious" is it?) as it creates a lot of new sockets, resulting in Exceptions.
Obviously the system needs to run as smooth as possible, but the main objective is to make it scalable so it can handle more Android devices.
EDIT: The system the system is not "peer-to-peer". All of the android devices should be able to be configured as the server.
None of your concerns are the real issue, in my view.
RMI has a pre-defined protocol, raw sockets do not.
If you use raw sockets, you have to do all the work to define what messages and protocols are exchanged by client and server.
There are so many good existing protocols (RMI, HTTP, etc.) that I'd wonder why you feel the need to invent your own again.
Android devices communicating over HTTP - tell me why it won't be fast or scalable enough. HTTP is good enough for the Internet - why not you and your solution?
I would suggest you to expose some kind of webservice (SOAP or REST) in your application server. For example, people frequently expose their data to mobile devices as a REST webservice API returning some kind of JSON format in order to make it easier to marshal it again in the client device.
This way you take profit of the underlying implementation of HTTP communication in every application server; any other way, you would have to write your own worker thread pool using nio primitive operations in order to achieve performance... Not a thing to be done in a real production environment - maybe in an academic one?

http server in php

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.

hosting a java based server

i want to write a game that will utilize java applets as client programs and will run a server application to operate the game (control the game handle the chat etc) is there a way to host such an application on a free server, or does it require a specialized server?
also is there a way to use php for tcp connection so it will receive the data and send it using tcp to the users (using a db to store user information from request to request) (for instance will forward chat massages)
If you are planning to use Java, make it completely on Java based, it will provide you the security and the performance would be much better.
If you are looking for a Free Java Server, I can provide you the Java server to host your Java based application for free.
A little remark about PHP: If you looking at PHP as a possible replace for server-side Java, I think that's not a good idea, cause PHP may be much slower (up to 1000 times according to the benchmarks I've seen, but that might not be absolutely correct).
I've seen a free java hosting called 'MyJavaServer' and that's all that I've found at that moment (couple of years ago). So you'll have to figure out how much java hostings are available now. And of course, there still makes sense to buy/rent a dedicated java server.
Addition: You do not really have to use DB to store intermediate information, you could do that even with PHP through things like 'memcached'.

Categories