wood burning stoves 2.0*
The moose likes Android and the fly likes Android Emulator as server in Socket communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Android Emulator as server in Socket communication" Watch "Android Emulator as server in Socket communication" New topic
Author

Android Emulator as server in Socket communication

Asif Kadiwala
Greenhorn

Joined: Jun 10, 2008
Posts: 17
Hi Mark,

In the Socket communication , android emulator as socket client working fine.

But if I configure emulator as Android server and desktop as socket client, then first requirement is to assign an IP address to this emulator instance,

for that I had tried to follow follwing steps to achieve n/w redir but could not succeed.

1). telnet localhost 5554 : works fine and emulator console opened
2). redir add tcp:4444:4444 - it fails to execute then I tried
3).redir tcp:4444:4444 but again the same problem.

So can you please tell me how to achieve IP of the emulator and redirect the ports?? Should I configure default IP 10.0.2.15 to emulator or it takes automatically?? If I have to configure then how??

Please help
Mark L. Murphy
Author
Ranch Hand

Joined: Feb 11, 2009
Posts: 131
Sorry, but I have not experimented with socket/port redirection, so I do not have any advice.


Mark Murphy (a Commons Guy)
Author of The Busy Coder's Guide to Android Development http://commonsware.com/Android/
Fred Grott
Ranch Hand

Joined: Oct 05, 2002
Posts: 346
question, did you do a port scan to make sure those ports were open before the redirection?

At them moment that is the only thing I can think of why your command did not work..


MobileBytes blog - Sharing Technology - My Programming Knols
Asif Kadiwala
Greenhorn

Joined: Jun 10, 2008
Posts: 17
Hello,

Can you please tell me how to do port scan before network redirection???

I have not done port redirection , but I had used the same ports for socket communication between Android as client and Desktop as server.It works fine for me. I had released the ports after that.
Jason Gosling
Greenhorn

Joined: Oct 18, 2009
Posts: 5
I can do Socket Programming on Android with UDP like this







But it can just run onto 1 emulator. I really want to run onto 2 emulators

On the other hand, with Socket Programming, I can not do with TCP
Anthony Wang
Greenhorn

Joined: Nov 21, 2010
Posts: 3
When I tried to run this progarm I got exception error in creating sockets (both server and client): (in Windows XP professional)
11-21 14:31:45.773: DEBUG/UDP(217): S: Connecting...
11-21 14:31:46.613: DEBUG/UDP(217): C: Connecting...
11-21 14:31:47.323: ERROR/UDP(217): S: Error
11-21 14:31:47.323: ERROR/UDP(217): java.net.SocketException: Permission denied (maybe missing INTERNET permission)
11-21 14:31:47.323: ERROR/UDP(217): at org.apache.harmony.luni.platform.OSNetworkSystem.createDatagramSocketImpl(Native Method)
11-21 14:31:47.323: ERROR/UDP(217): at org.apache.harmony.luni.platform.OSNetworkSystem.createDatagramSocket(OSNetworkSystem.java:90)
11-21 14:31:47.323: ERROR/UDP(217): at org.apache.harmony.luni.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:148)
11-21 14:31:47.323: ERROR/UDP(217): at java.net.DatagramSocket.createSocket(DatagramSocket.java:209)
11-21 14:31:47.323: ERROR/UDP(217): at java.net.DatagramSocket.<init>(DatagramSocket.java:107)
11-21 14:31:47.323: ERROR/UDP(217): at com.net.udpconnection.Server.run(Server.java:24)
11-21 14:31:47.323: ERROR/UDP(217): at java.lang.Thread.run(Thread.java:1060)
11-21 14:31:47.563: ERROR/UDP(217): C: Error
11-21 14:31:47.563: ERROR/UDP(217): java.net.SocketException: Permission denied (maybe missing INTERNET permission)
11-21 14:31:47.563: ERROR/UDP(217): at org.apache.harmony.luni.platform.OSNetworkSystem.createDatagramSocketImpl(Native Method)
11-21 14:31:47.563: ERROR/UDP(217): at org.apache.harmony.luni.platform.OSNetworkSystem.createDatagramSocket(OSNetworkSystem.java:90)
11-21 14:31:47.563: ERROR/UDP(217): at org.apache.harmony.luni.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:148)
11-21 14:31:47.563: ERROR/UDP(217): at java.net.DatagramSocket.createSocket(DatagramSocket.java:209)
11-21 14:31:47.563: ERROR/UDP(217): at java.net.DatagramSocket.<init>(DatagramSocket.java:88)
11-21 14:31:47.563: ERROR/UDP(217): at java.net.DatagramSocket.<init>(DatagramSocket.java:71)
11-21 14:31:47.563: ERROR/UDP(217): at com.net.udpconnection.Client.run(Client.java:21)
11-21 14:31:47.563: ERROR/UDP(217): at java.lang.Thread.run(Thread.java:1060)

How to correct it?
DaLi Zhang
Greenhorn

Joined: Nov 23, 2010
Posts: 2
11-21 14:31:47.323: ERROR/UDP(217): java.net.SocketException: Permission denied (maybe missing INTERNET permission)

It seems like you have not add the permission onto your application. try to do this way
In your manifest.xml file, add this sentence
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
you can find other examples from android SDK doc

By the way, the DatagramSocket CAN be created when you are using '127.0.0.1' in emulator, but if you change it to your host machine's IP address, it still report
errors. I have not figure out the reason yet. Hope some other people can help.

Best,
Echo
Anthony Wang
Greenhorn

Joined: Nov 21, 2010
Posts: 3
Setting up Redirections through the Emulator Console

Each emulator instance provides a control console the you can connect to, to issue commands that are specific to that instance. You can use the redir console command to set up redirections as needed for an emulator instance.

First, determine the console port number for the target emulator instance. For example, the console port number for the first emulator instance launched is 5554. Next, connect to the console of the target emulator instance, specifying its console port number, as follows:

telnet localhost 5554

Once connected, use the redir command to work with redirections. To add a redirection, use:.

redir add <protocol>:<host-port>:<guest-port>

where <protocol> is either tcp or udp, and <host-port> and <guest-port> sets the mapping between your own machine and the emulated system, respectively.

For example, the following command sets up a redirection that will handle all incoming TCP connections to your host (development) machine on 127.0.0.1:5000 and will pass them through to the emulated system's 10.0.2.15:6000.:

redir add tcp:5000:6000

Or UDP
redir add udp:5000:6000

To delete a redirection, you can use the redir del command. To list all redirections for a specific instance, you can use redir list. For more information about these and other console commands, see Using the Emulator Console.

Note that port numbers are restricted by your local environment. this typically means that you cannot use host port numbers under 1024 without special administrator privileges. Also, you won't be able to set up a redirection for a host port that is already in use by another process on your machine. In that case, redir generates an error message to that effect.
DaLi Zhang
Greenhorn

Joined: Nov 23, 2010
Posts: 2
Hi Anthony,

I used to try the port redirection. The same way as you have written. But I still can not get any incoming message. There will be an error when the system try to do this:

DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);

it only works when the serverAddr is '127.0.0.1'.

If you have successfully set up the connection, can you show me some part of your code?? Thanks a lot.

Best,
Echo
Anthony Wang
Greenhorn

Joined: Nov 21, 2010
Posts: 3
I use 10.0.2.15.
It works.
 
Don't get me started about those stupid light bulbs.
 
subject: Android Emulator as server in Socket communication