I'm currently working on a game of sorts in which it is necessary for two Clients to communicate with a Server program (I'm writing both Client and Server). The clients need to be able to send the Server basic integers while the Server needs to send both Strings and more advanced objects.
How should I go about doing this? My programming knowledge is unfortunately not exactly wide. I tried using Sockets at first but became slightly confused with them - particularly with how to exactly send or read information from them and fit that into a program.
Sockets is not that difficult. A while ago I wrote a P2P chat app., in which I used TCP (for sending the mesages) and UDP sockets (for broadcasting, host status, etc.).
The data types used where very basic (later I expanded it to support images), similar to your needs.
You can check the project at source forge: http://sourceforge.net/projects/isytok/
Skip the view classes, and check for NetworkClient and NetworkServer classes in the network package to start.
Good luck!
I think using sockets is fine for your application. I would take a look at the excellent collection of Java socket examples that are available out there:
Client/Server sockets example
Sun/Oracle socket communications docs
Simple echo server from Oracle
Simple socket examples
How do I create a client-server socket communication?
I got those from a google search for 'java socket example`. There are tons more.
If you have more specific questions then I'd come back with another question showing a concise code sample, output, and problems you are having.
Related
I've had a look around and could not find what I'm looking for, so please feel free to redirect me.
I'm writing a card game that uses a Java server that stores the card information. I want to develop an Android app that connects to this server (via TCP/IP is my initial thought) and issues the commands i.e. pick up cards, play cards etc. (as the cards are stored in arraylists within the server app.)
My knowledge of Android is extremely limited and I cant find any good sources that could possibly help me, or explain how android networking works.
You need tcp server socket communication >
http://developer.android.com/reference/java/net/Socket.html
I would recommend to use a REST service and communicate via JSON. You can use for example Robospice in combination with Jackson2 which also brings you async communication and caching out of the box.
https://github.com/stephanenicolas/robospice/wiki/Starter-guide
Socket communication is really old school ;-)
I have following requirements:
User comes on website and posts a request for a task.
There are multiple machines(clients) running an installed software(not web browser) which can perform the task.
web server has to ask these clients, whether they are willing to complete the task. I want the web server to be running on aws.
Here is what I have understood so far:
client can be a java socket client.
There can be a socket server on aws.
webserver talks to socket server and asks it to talk to socket client and get the response back.
other option might be to use websocket, but I need the client to be a installed software not a browser. Can the need of server socket be eliminated in this case?
Please suggest the best approach. Link to some tutorials will be very helpful or atleast I know what to google.
Thanks
I would suggest using RMI (Remote Method invocation) it is robust and seems fitted for what you are trying to accomplish:
http://en.wikipedia.org/wiki/Java_remote_method_invocation
I would also suggest using LipeRMI, which is a good, easy to follow RMI library.
Here is a good link to give you a quick idea on how it works and what your code would look like: http://lipermi.sourceforge.net/documentation.php
It would allow you to call methods on the server and send/receive serialized objects. Only thing is, if you need to do stuff like file transfer, this wouldnt work it only works with java objects.
I need to write an application in java which communicates with a web server.
I know how to do that, using PHP, but I'm afraid it won't cut it in this one.
Here's my situation.
I have multiple clients, when one of them sends a specific message to the server (so far, no problem on PHP), I want it to send a certain message to all other connected clients.
The problem is, I could hardly find any information regarding server socket in PHP, which led me to believe this isn't the proper way of achieving that. I'm using a paid hosting (x10premium) to host my servers so far, so I was thinking of doing it with this one, however, I'm not sure it's even possible with PHP.
At the moment I'm having each of the clients periodically check with the server if he received a message from any of the other clients, but I don't like this solution...
I hope someone could point me to the right direction. I don't know too much about Ruby and other languages which are used to do stuff like that, But if necessary, I would gladly learn it.
Thanks in advance
EDIT:
Forgot to mention, the server (currently the PHP) would also communicate with a MySQL table. If it matters.
This is a good example of PHP socket server/client:
http://tech.navarr.me/2010/07/how-to-create-a-socket-server-in-php.html
You could do it just like with JAVA, but remember that PHP does not support multithreading or multiprocessing so if 10 clients connect at once to your server, you will process them one by one, so eventually 1 will have to wait for the other 9 to finish - everything, database and connection overhead.
If you do it with JAVA or Python for example, you could handle each request in separate thread so that DB & Network communication overhead is handled simultaneosly.
Python has build in socket server components and nice and easy to use mysql component, that would make it a breez to achieve this, without even having to understand threading at all.
For the python socket server see here:
http://docs.python.org/library/socketserver.html
Basically you just define a function that will be executed for each new client connection and tell the server to serve_forever() - until it dies, it will do what you want.
I am currently trying to transfer a file from a Android device to a Java TCP Server, but I am unable to find a good example which explains the structure I would need to implement this. There are many Java Client&Server examples there which explain file transfer but I want to make sure if this will still work once one throws an Android Device in there.
My question is how do I implement this sort of structure? And if it doesn't work, would I be better sending the file over an HTTP connection to a PHP server? I see a lot of examples and documentation online for the later method so I presume it is more reliable. I would however prefer to use a Java server.
The file consists of a large set of coordinates recorded by the Android device which will then be sent to the server. I have not yet established how I will store this data yet but I was originally going to store them in a primitive text file.
Design
The first thing you need is something to allow you to run Java code on your server.
There are a number of options. Two of the most popular technologies are Glassfish and Apache Tomcat.
Crudely speaking Apache Tomcat is sufficient for simple client-server communication and Glassfish is used if you need to do more complex stuff. Both allow Servlets (which are essentially self contained server classes written in Java) to run on the server-side.
They handle communication with the client by launching a JVM (Java Virtual Machine) each time they receive a request. The Java servlet can run inside the JVM and respond do some processing if required before sending a response back to the client.Each new request is run in a new instance of a servlet. This makes dealing with multiple concurrent requests simpler (no need for more complex threading).
Networking (sending data to and from the server)
In networking situations the client can be a PC, an Android phone, or any other device capable of connecting to the internet. As far as the server is concerned, if the client can communicate using HTTP (a standard protocol which it understands) the it doesn't care what sort of device it is. This means that solutions for PC desktop client-server applications are similar to one for a phone.
You can use library such as Apache HTTP Components to make it easier to handle HTTP requests and responses between the device and the server. Of course you could write your own classes to do this using Sockets but this would be very time consuming, particularly if you have never done it before.
Storage of Data
If you have time I would recommend implementing some sort of database to store the information.
They have a number of benefits to such as data recovery mechanisms, indexing for fast searching of data, ensure data integrity, better structuring of data and so on.
If you decide to use a database I recommend MySQL. It is a free and more importantly - well documented.
Aside: JDBC can be used to communicate with the database with Java.
Sorry about the in-line hyperlinks - apparently my repuation isn't high enough to post more than two!
Source: Personal experience from implementing a similar design.
I want to use NAT hole-punching in one of my java applications, but I don't know where to start. I'd like some sample code to start off with....
(I have access to a PHP server with sockets, cURL, and server sockets, so some PHP server code would be appreciated too...)
Thanks in advance.
EDIT: I am sorry, I was not clear enough. I want to know how to create a PHP server that can be "the middleman" for hole punching. I also want to know how to make a java application that can connect to the PHP server, and maintain that connection while another java application connects to the first one on the same port. (Which is my impression of how NAT hole punching works)
You need to learn about STUN. You also need to learn about conditions under which nat traversal is possible (and when it is not). You can read chapter 4 of the Practical JXTA II book available online at scribd. If STUN is not possible, then you need to rely on TURN.
I don't have PHP code examples to provide, but when you will understand how it works, you'll see it is not that complicated to implement. You only need to retrieve translated IP addresses and ports from the middleman, and that's basically it.