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.
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 to make an android application which is basically a chat application.*In this chat app the user can interact with single user as well in a group that means Group chat is also there. I am using php as the back end for database . I have never done this type of work earlier so need some suggestions or guidance.
The options i have seen is as follow :
1. Web-services
2. Socket Programming
3. XMPP using the smack api
Now i am confused in them that which is better to implement chat feature in android. Also i didn't get any sample code for that. One of my team member is asking me to use the socket programming but i didn't get any sample app or anything for socket programming in android. I don't know socket programming this time. How we can connect the our phone to the server and then some other device. Hope you get me what i want to Conway you.
So can any one please guide me and show me the right direction to move on. Also if you can provide me any sample code, that can be really helpful. Any help is appreciated
Another option (besides XMPP, which is certainly a valid approach for chat, since it was designed for that) would be using WebSocket. Using WebSocket would open the possiblity of having a pure browser client as well.
For WebSocket on Android, there is AutobahnAndroid, part of the Autobahn family of WebSocket and WAMP implementations.
WAMP is a RPC and PubSub over WebSocket protocol with multiple implementations. There is also a PHP implementation of WebSocket/WAMP: Ratchet.
Disclaimer: I am author of Autobahn/WAMP and work for Tavendo.
For a basic Socket programming tutorial, you can check HERE and HERE.
However, I suggest using XMPP, as I have mentioned HERE, as it is a protocol designed from the ground up to be used for Messaging.
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'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.
I need a remote computer to communicate with my android/iPhone apps to do things like promote, get stats... What's the fastest way to achieve this? No html needed, it's just for short (most likely JSON) messages between me and myself.
I have an account on my university's CS unix-based server on which I can host content. Tutorials greatly appreciated, thank you.
Now sure why you want to reinvent the wheel. Writing a web server would involve low level socket communication, putting up a protocol and bunch of code to handle these things. Please check Tiny Java web server, or Jetty .
You'll have to make a Web Application (maybe in PHP or Java), and then you'll have to host this application on a web server.