I am new to Android and Java, so I apologize if my question is asked in an inappropriate group or forum. I made a .Net application for my company, and recently they asked me to transfer it to Android in order to install it on the Samsung Galaxy tabs.
First of all, I use Eclipse, JDK 6, the target platform of Android 2.2 and the emulator with the GalaxyTab plugin. My operating system is Windows 7.
This application sends and receives messages from a specific controller on the network using UDP. In short, my application uses a "DatagramSocket", binds it to a local "InetSocketAddress", and then starts a thread that listens for datagrams, while another thread sends requests to the controller for a user request. Here is the code snippet:
Here I assign the local address and socket:
try { loc_addr = new InetSocketAddress( Inet4Address.getByAddress( new byte[]{(byte) 192,(byte) 168,1,(byte)240}), 0xBAC0); //192.168.1.240 is the IP of my machine on the network } catch (UnknownHostException e) { ....... } try { soc = new DatagramSocket(); soc.setReuseAddress(true); soc.setBroadcast(true); soc.bind(loc_addr); } catch (SocketException e) { ....... }
Here I listen to incoming datagrams:
try{ buf = new byte[1024]; receive_pac = new DatagramPacket(buf, 1024); soc.receive(receive_pac); if (receive_pac.getData() != null){ ....... } }
Here I send the data:
try { addr = (Inet4Address) Inet4Address.getByAddress (new byte[] {(byte) 192,(byte) 168,1,(byte) 255});
Well, when I use "soc.bind (...)", I get the following exception: cannot assign the requested address
Then I get a debugging message (I don't know how relevant it is):
DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
The application works, I checked through "WireShark" that when I ask the emulator to send data to the controller, the controller correctly answers the expected data. However, the socket, in the application, does not receive anything on the emulator and the call "Get" remains blocked!
Can someone help me figure out what problems or errors I made with the receiving part of my application?
Any help is greatly appreciated