Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to both Android and Java so I beg your pardon if my question is asked at the inappropriate group or forum. I made a .Net application for my company and recently they asked me to port it on Android so as to install it on Samsung Galaxy Tabs.

First of all, I am using Eclipse, JDK 6, target platform android 2.2 and an Emulator with the GalaxyTab plugin. My operating system is Windows 7.

This application, sends and receives messages to and from a certain controller on the network using UDP. In short my application uses a "DatagramSocket", binds it to a local "InetSocketAddress" and then launches a thread that listens for datagrams, while another thread sends requests to the controller upon the user's request. Here is a some code snippet:

This is where I assign the local address and the 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) {
     .......
}

This is where I listen for incoming datagrams:

try{
     buf = new byte[1024];
     receive_pac = new DatagramPacket(buf, 1024);
     soc.receive(receive_pac);
     if (receive_pac.getData() != null){
     .......
     }
}

This is where I send 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 receive the following exception: cannot assign requested address

Then I receive a debug message (I don't know if it is relevant):

DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol

The application is working, I verified through "WireShark" that when I ask from the emulator to send the data to the controller, the controller replies back with the expected data correctly. However, the socket, in the application, on the emulator doesn't receive anything and stays blocked on the "Receive" call!

Can anyone help me figure out what problem or error I have committed with the receiving part of my application!

Any help is much appreciated,

like image 431
TMI Avatar asked Feb 25 '23 22:02

TMI


1 Answers

TMI,

[Edited: If you saw my other answer, please disregard, I made the classic mistake of changing two variables in one test and it was the other variable that made the difference.]

In regards to this:

I tried binding it to the socket and it resulted with the "SocketException: Invalid Argument". Still the program delivered the same operation! Do you have any idea what this exception might mean?

You may have solved this by now, but I just had the same question and answered it myself here.

What got rid of this exception for me was to change the way I was creating the DatagramSocket.

From:

sock = new DatagramSocket();

To:

DatagramChannel channel = DatagramChannel.open();
DatagramSocket socket = channel.socket();
like image 160
Lance Avatar answered Mar 05 '23 19:03

Lance