我正在爲它創建一個帶有Android客戶端的桌面應用程序。 目的是將圖像從Android設備發送到桌面,然後在用戶在桌面上執行輕彈手勢後,將同一圖像發送回手機。在Android中通過TCP套接字連接時獲取「java.net.BindException:地址已在使用中」
從android發送到桌面的工作很好,但是當方法循環監聽端口(嘗試接收圖片)時,LogCat中出現了很多「java.net.BindException:地址已在使用中」。這個異常循環所有的時間,但應用程序不是粉碎。
這裏是從桌面發送數據片段responisible:
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();
}
}
}
}
正如你可以在上面看到我發送和接收數據使用不同的端口,因此按照我的邏輯「地址不應該已在使用中」 - 如果我理解正確。我正在嘗試不同的端口,但這並沒有幫助。 我將非常感謝所有提示。
這有幫助。我沒有得到這個例外了。在** ImageReceiver.class **中,我把** while **循環裏面** try **和** ServerSocket **現在在** try **裏面,但在** while循環外面。不幸的是,現在我沒有得到任何例外,它不起作用。代碼甚至不會進入循環。 – Booyaches 2012-04-19 10:14:31
@Boyoyaches定義'它不工作'。如果你聲稱你的'while(1 == 1)'循環從未輸入,恐怕我只是不相信你。 – EJP 2012-04-19 10:46:32