• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps sending datagram crashes without exception

Bozebo

Lurker
edit: Problem solved, solution in 3rd post

Hi. I have developed a simple UDP connection protocol in C++ that I am currently using fine on windows machines. I want to proceed to develop a java client class for use in android applications.

Everything seems to be simple enough (except for eclipse constantly bugging out) but I appear to have hit a brick wall.

Here is the code that I run when a button is pressed (all I want at this stage is to get a packet through):
Code:
DatagramPacket sendPacket = new DatagramPacket("HELLO!".getBytes(), 6, servAddr, server_port);
Log.d("UdpTest", "blah");
try {
  Log.d("UdpTest", "blah2");
  servSock.send(sendPacket);
  Log.d("UdpTest", "blah3");
} catch (IOException e) {
  Log.d("UdpTest", "FUUUU3");
  e.printStackTrace();
}
txtStatus.setText("Status: Connecting...");
And the logcat output is:
blah
blah2
Shutting down VM
dalvikvm(549): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
[wall of red text...]

I am trying to catch the exception, so I am not sure how it is uncaught. This is practically my first attempt at Java so I could be mistaken in that the uncaught exception is not my problem and is just mentioned because of however Android is deciding to fail on the send() method.

The only permission my app should require is android.permission.INTERNET is it not? Oddly, if I remove the permission the app crashes in exactly the same way which would suggest to me that it needs some other permission, the permissions are not applying correctly or that the problem has nothing to do with permissions (it is failing before it gets far enough to care).

Somebody else seems to have the same issue too: Issue 30182 - android - Datagram send failure on Galaxy Running Ice Cream Sandwich - Android - An Open Handset Alliance Project - Google Project Hosting (I replied)

So far, I think it could be a strange ICS problem, but that wouldn't make sense because I haven't seen any other app that is likely to use UDP simply stop responding on my phone (edit: I have actually, see my next post). The other alternative is that my Java is just horribly wrong (i hope so).

Here is my code:
Code:
public class UdpTestActivity extends Activity {
    String text;
    int server_port = 25505;
    byte[] message = new byte[3000];
    DatagramSocket servSock;
    InetAddress servAddr;
    TextView txtStatus;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            servAddr = InetAddress.getByName("192.168.1.231");
        } catch (UnknownHostException e) {
            Log.d("UdpTest", "FUUUU1");
            e.printStackTrace();
        }

        try {
            servSock = new DatagramSocket();
        } catch (SocketException e) {
            Log.d("UdpTest", "FUUUU2");
            e.printStackTrace();
        }
        
        txtStatus = (TextView)findViewById(R.id.tvStatus);
        txtStatus.append("Disconnected");

        Log.d("UdpTest", "blah");
    }


    public void butConnectPressed(View view){
        DatagramPacket sendPacket = new DatagramPacket("HELLO!".getBytes(), 6, servAddr, server_port);
        Log.d("UdpTest", "blah");
        try {
            Log.d("UdpTest", "blah2");
            servSock.send(sendPacket);
            Log.d("UdpTest", "blah3");
        } catch (IOException e) {
            Log.d("UdpTest", "FUUUU3");
            e.printStackTrace();
        }
        txtStatus.setText("Status: Connecting...");
    }
}
I have no idea what I am doing wrong.
 
Ok, turns out that on ICS the call to setLength is mandatory on datagrampacket objects. (my exact code worked on a 2.0 emulator and a friend's older android phone)

Interesting that it doesn't seem to be commonly discussed online as I could imagine a lot of developers would have had to find that out. I recall some things simply crashing on my nexus like OnLive (which no doubt will use udp) which then worked after an update.

Hopefully anybody else with the same issue will find the answer in this thread.
 
Ok, turns out that on ICS the call to setLength is mandatory on datagrampacket objects. (my exact code worked on a 2.0 emulator and a friend's older android phone)

Interesting that it doesn't seem to be commonly discussed online as I could imagine a lot of developers would have had to find that out. I recall some things simply crashing on my nexus like OnLive (which no doubt will use udp) which then worked after an update.

Hopefully anybody else with the same issue will find the answer in this thread.

Thanks for coming back to let everyone know your solution. =)
 
Back
Top Bottom