Android illegalstatexception Android illegalstatexception android android

Android illegalstatexception


The exception is telling you exactly what to do: create the Socket object in a separate Thread. You can use an AsyncTask for this as well.

The reasoning behind not allowing Sockets on the main UI thread is that it can cause the app to get the dreaded Application Not Responding message from waiting for a Socket.

edit: http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/

You can just skip down to the client example since you already have a desktop server.

edit2: Since I happen to also be working on a C# server for my Android app, here is how my desktop app creates a listener Socket:

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());IPAddress ipAddress = ipHostInfo.AddressList[0];IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 27015); //Port 27015Socket listener = new Socket(AddressFamily.InterNetwork,    SocketType.Stream, ProtocolType.Tcp); // Create a TCP/IP socket.

I'm almost certain that the problem lies within your server/network. The Android code to send out a simple UDP packet looks correct. You might also want to try connecting over localhost or 127.0.0.1 by writing a simple C# client program.

edit 3:

With this basic app you should be able to connect to the server after you put the correct IP address in. Pressing Send will send some bytes to the server. Use this to make sure that your connection is working. I've confirmed that it works on my end. If this works, then I would open a new SO question for your server issues, otherwise something is wrong with your network configuration.


I think that the problem comes from the do while loop because you are attempting to modifiy a UI component ( label3 ) in the loop which is by the way a infinite loop sins the terminate variable is always false. Try getting the code witch modifies the UI ( label3.*) out of the loop.