Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Android Emulator as server in Socket communication

 
Asif Kadiwala
Greenhorn
Posts: 17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I have not experimented with socket/port redirection, so I do not have any advice.
 
Fred Grott
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Asif Kadiwala
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use 10.0.2.15.
It works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic