android - Sending datagram - network unreachable -


i'm trying broadcast udp packets between 2 android devices on hotspot.

i have client set hotspot, , server connected hotspot. sending package client , receiving packet on server working. when try reply server, socketexception, saying network unreachable. i've checked ip address , port number , both should correct (i'm assuming port correct because taken packet itself).

the code server , client below. can see code going wrong or if it's network connectivity issue?

client:

    try {             //open random port send package             c = new datagramsocket();             c.setbroadcast(true);              byte[] senddata = "discover_mv_request".getbytes();              try {                 inetaddress addr = getipaddress();                 datagrampacket sendpacket = new datagrampacket(senddata, senddata.length, getbroadcast(addr), 8888);                 c.send(sendpacket);             } catch (exception e) {                 c.close();                 e.printstacktrace();                 return null;             }              // broadcast message on network interfaces             enumeration interfaces = networkinterface.getnetworkinterfaces();             while (interfaces.hasmoreelements()) {                 networkinterface networkinterface = (networkinterface) interfaces.nextelement();                  if (networkinterface.isloopback() || !networkinterface.isup()) {                     continue; // don't want broadcast loopback interface                 }                  (interfaceaddress interfaceaddress : networkinterface.getinterfaceaddresses()) {                     inetaddress broadcast = interfaceaddress.getbroadcast();                     if (broadcast == null) {                         continue;                     }                      // send broadcast package!                     try {                         datagrampacket sendpacket = new datagrampacket(senddata, senddata.length, broadcast, 8888);                         c.send(sendpacket);                     } catch (exception e) {                         c.close();                         e.printstacktrace();                         return null;                     }                  }             }              //wait response             byte[] recvbuf = new byte[15000];             datagrampacket receivepacket = new datagrampacket(recvbuf, recvbuf.length);             c.receive(receivepacket);              //check if message correct             string message = new string(receivepacket.getdata()).trim();             if (message.equals("discover_mv_response")) {                 //do server's ip (for example, store in controller)                 log.v("socket", "got correct response");                 c.close();                 return "correct";             } else {                 c.close();                 log.v("socket", "wrong response");                 return "wrong";             }          } catch (exception e) {             c.close();             e.printstacktrace();             return null;         } 

server:

        try {             //keep socket open listen udp trafic destined port             socket = new datagramsocket(8888, inetaddress.getbyname("0.0.0.0"));             socket.setbroadcast(true);              while (true) {                 //receive packet                 byte[] recvbuf = new byte[15000];                 datagrampacket packet = new datagrampacket(recvbuf, recvbuf.length);                 socket.receive(packet);                  //see if packet holds right command (message)                 string message = new string(packet.getdata()).trim();                 if (message.equals("discover_mv_request")) {                     byte[] senddata = "discover_mv_response".getbytes();                      //send response                     datagrampacket sendpacket = new datagrampacket(senddata, senddata.length, packet.getaddress(), packet.getport());                     socket.send(sendpacket); //where error happens                     socket.close();                 }              }         } catch (exception e) {             e.printstacktrace();         } 


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -