Booyaches
Booyaches

Reputation: 1719

Getting "java.net.BindException: Adress already in use" while connecting over TCP socket in Android

I am creating a tabletop application with Android client for it. The purpose is to send an image from Android device to tabletop and then, after user performs flick gesture on tabletop ,the same image is send back to the phone.

Sending from android to tabletop works great but I am getting a lot of "java.net.BindException: Adress already in use" in LogCat when the method loops listening to the port (trying to receive the picture). This exception loops all the time but the app is not crushing.

Here is the snippet responisible for sending data from tabletop:

public void sendImage(byte[] buffer)
{
    try {
        Socket s = new Socket("192.168.1.34",8080);

        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;

        dataOutputStream = new DataOutputStream(s.getOutputStream());
        dataInputStream = new DataInputStream(s.getInputStream());

        dataOutputStream.writeInt(buffer.length);
        dataOutputStream.write(buffer,0, buffer.length);

        s.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

And below there is the code for my Android app. When the application starts I'am running "ImageReceiver.class" on the separate thread. "ImageReceiver" is Observable object so when it changes state I can start new activity and display an image.

public class FluidPhotoClientActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Gallery gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));


    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            Toast.makeText(FluidPhotoClientActivity.this, "Sending photo nr: " + position, Toast.LENGTH_SHORT).show();

            try {
                Socket s = new Socket("192.168.1.35",8888);

                DataOutputStream dataOutputStream = null;
                DataInputStream dataInputStream = null;

                dataOutputStream = new DataOutputStream(s.getOutputStream());
                dataInputStream = new DataInputStream(s.getInputStream());

                Resources res = getResources();
                Drawable draw = null;

                switch(position) // HARDCODED IMAGE POSITION
                {
                case 0:
                    draw = res.getDrawable(R.drawable.testimage);
                    break;
                case 1:
                    draw = res.getDrawable(R.drawable.testimage2);
                    break;
                }

                if(draw!=null)
                {
                    Bitmap bitmap = (Bitmap)((BitmapDrawable) draw).getBitmap();
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

                    byte[] buffer = stream.toByteArray();

                    dataOutputStream.writeInt(buffer.length);
                    dataOutputStream.write(buffer,0, buffer.length);

                    s.close();

                    dataInputStream.close();
                    dataOutputStream.close();
                } else {
                    Toast.makeText(FluidPhotoClientActivity.this, "Error", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {
                Toast.makeText(FluidPhotoClientActivity.this, "IOException", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
    });

    ImageReceiver ir = new ImageReceiver();
    Thread imageListener = new Thread(ir);
    imageListener.start();

    class ImageObserver implements Observer {
        @Override
        public void update(Observable arg0, Object arg1) {
            Log.d("MESSAGE:" , "hello");
            showIncomingImage();
        }
    }
    ImageObserver io =  new ImageObserver();
    ir.addObserver(io);
}
public void showIncomingImage() {
    startActivity(new Intent(this, SingleViewActivity.class));
}

}

ImageReceiver.class - I guess this is where error is comming from.

public class ImageReceiver extends Observable implements Runnable {

private String responseImage;

public String getResponseImage() {
    return responseImage;
}


@Override
public void run() {
    while(1==1)
    {
        try {
            ServerSocket ss = new ServerSocket(8080);
            Socket s = ss.accept();

            DataInputStream dataInputStream = new DataInputStream(s.getInputStream());
            DataOutputStream dataOutputStream = new DataOutputStream(s.getOutputStream());

            int imageSize = dataInputStream.readInt();
            byte[] buffer = new byte[imageSize];
            dataInputStream.readFully(buffer, 0, imageSize);

            Log.d("MESSAGE:", String.valueOf(buffer.length));

            ss.close();
            s.close();
            if(buffer.length > 0)
            {
                this.responseImage = String.valueOf(buffer.length);
                setChanged();
                notifyObservers();
            }



        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

}

As you can see above I am sending and receiving data using different ports so according to my logic "the address should not already be in use" - if I understand it correctly. I was experimenting with different ports but that didn't help. I will be very thankful for all the tips.

Upvotes: 0

Views: 808

Answers (1)

user207421
user207421

Reputation: 310957

Create the ServerSocket once, outside the loop.

Upvotes: 2

Related Questions