I want to send and receive datagram socket but I'm getting exception java.net.BindException: Cannot assign requested address. I passed the correct ipaddress of the server which I want to communicate and correct port no.
try {
SocketAddress sockaddr = new InetSocketAddress("203.100.77.54", 8000);
DatagramSocket sock = new DatagramSocket(sockaddr);
DatagramPacket pack = new DatagramPacket(bData, bData.length);
sock.send(pack);
} catch (FileNotFoundException fnfe) {
Log.e(LOG_TAG, "FileNotFoundException");
} catch (SocketException se) {
Log.e(LOG_TAG, "SocketException");
} catch (UnknownHostException uhe) {
Log.e(LOG_TAG, "UnknownHostException");
} catch (IOException ie) {
Log.e(LOG_TAG, "IOException");
}
Please help me.
DatagramSockets aren't created with a target address. They are created with their own local bind-address, or none, which causes a default bind when first used. The target address is specified when constructing the DatagramPacket, or in the connect() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With