DatagramSocket.bind (); Socket exception: cannot assign the requested address. Android emulator

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}); //The message I am sending should be broadcasted } catch (UnknownHostException e) { ...... } sendPacket = new DatagramPacket(buf, buf.length, addr, loc_addr.getPort()); try { soc.send(sendPacket); } catch (IOException e) { ...... } 

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

+4
source share
2 answers

TMI

[Edited: if you saw my other answer, please ignore it, I made a classic mistake when changing two variables in one test, and another variable did it.]

Regarding this:

I tried to bind it to a socket, and this led to a "SocketException: Invalid Argument". Nevertheless, the program performed the same operation! Do you know what this exception can mean?

You may have decided this now, but I had the same question and answered it myself.

What got rid of this exception for me is to change the way DatagramSocket is created.

From:

 sock = new DatagramSocket(); 

To:

 DatagramChannel channel = DatagramChannel.open(); DatagramSocket socket = channel.socket(); 
+8
source

Usually you see this error message if you try to bind to an IP address that you do not have. Are you sure the IP address of your Android emulator is 192.168.1.240? The IP address of the emulator may be different from the IP address of your host computer.

0
source

Source: https://habr.com/ru/post/1341978/


All Articles