Skip to content Skip to sidebar Skip to footer

Doinbackground Gets Stuck

I'm trying to create a server that uses multiple ports so different data and information can be sent and received with ease, but in my doInBackground method, my code gets stuck on

Solution 1:

From the Javadoc for DatagramSocket.receive:

This method blocks until a datagram is received

So your code is 'stuck' because it is waiting for data packets to arrive. This means the client that is connecting to this server/port hasn't yet sent another packet so the socket is blocking by design.

Traditionally if you want to listen on multiple ports at the same time you have two options:

  1. Open each blocking socket on a different thread (this won't scale well if you will have large number of clients concurrently due to one client per thread but works fine for a small number of connections)
  2. Use NIO which is non-blocking IO for Java

Post a Comment for "Doinbackground Gets Stuck"