在Android中通过TCP套接字连接时获取“java.net.BindException:Address in in use”

时间:2012-04-18 17:34:51

标签: java android sockets tcp

我正在使用Android客户端创建一个桌面应用程序。 目的是将图像从Android设备发送到桌面,然后,当用户在桌面上执行轻弹手势后,相同的图像将被发送回手机。

从android发送到桌面效果很好,但是当方法循环监听端口(试图接收图片)时,我在LogCat中得到了很多“java.net.BindException:Adress已经在使用”。这个异常一直循环,但应用程序没有破碎。

以下是从桌面发送数据时可回复的代码段:

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();
    }
}

下面是我的Android应用程序的代码。当应用程序启动时,我在单独的线程上运行“ImageReceiver.class”。 “ImageReceiver”是Observable对象,因此当它改变状态时,我可以开始新的活动并显示图像。

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 - 我想这是错误来自的地方。

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();
        }
    }

}

}

正如您在上面所看到的,我使用不同的端口发送和接收数据,因此根据我的逻辑“地址不应该已经使用” - 如果我理解正确的话。我正在尝试不同的端口,但这没有帮助。 我将非常感谢所有提示。

1 个答案:

答案 0 :(得分:2)

在循环外创建一次ServerSocket。