java中出现绑定异常,如何解决java.net.BindException:绑定失败:EADDRINUSE(地址已在使用中)...

我正在使用套接字从Android到Android使用WiFi Direct传输文件。我开始使用以下代码发送文件

String[] filesPath = data.getStringArrayExtra("all_path");

Intent serviceIntent = new Intent(getActivity(), FileTransferService.class);

serviceIntent.setAction(FileTransferService.ACTION_SEND_FILE);

serviceIntent.putExtra(FileTransferService.EXTRAS_FILE_PATH, filesPath);

serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS, info.groupOwnerAddress.getHostAddress());

serviceIntent.putExtra(FileTransferService.EXTRAS_GROUP_OWNER_PORT, 8988);

getActivity().startService(serviceIntent);

服务代码是:

@Override

protected void onHandleIntent(Intent intent)

{

Context context = getApplicationContext();

if (intent.getAction().equals(ACTION_SEND_FILE))

{

//String fileUri = intent.getExtras().getString(EXTRAS_FILE_PATH);

String[] files = intent.getExtras().getStringArray(EXTRAS_FILE_PATH);

String host = intent.getExtras().getString(EXTRAS_GROUP_OWNER_ADDRESS);

Socket socket = new Socket();

int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);

try

{

Log.d(WiFiDirectActivity.TAG, "Opening client socket - ");

socket.bind(null);

socket.connect((new InetSocketAddress(host, port)), SOCKET_TIMEOUT);

Log.d(WiFiDirectActivity.TAG, "Client socket - " + socket.isConnected());

ArrayList filesList = new ArrayList();

for (String file : files)

{

filesList.add(new File(Uri.parse("file://" + file).getPath()));

}

send(filesList, socket);

}

catch (IOException e)

{

e.printStackTrace();

Log.e(WiFiDirectActivity.TAG, e.getMessage());

}

finally

{

if (socket.isConnected())

{

try

{

socket.close();

}

catch (IOException e)

{

// Give up

e.printStackTrace();

}

}

}

}

}

发送文件方法:

public void send(ArrayList files, Socket socket)

{

try

{

//DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

System.out.println(files.size());

//write the number of files to the server

dos.writeInt(files.size());

dos.flush();

//write file names

for (File file1 : files)

{

dos.writeUTF(file1.getName());

dos.flush();

}

//buffer for file writing, to declare inside or outside loop?

int n;

byte[] buf = new byte[1024 * 8];

//outer loop, executes one for each file

for (File file : files)

{

System.out.println(file.getName());

FileInputStream fis = new FileInputStream(file);

dos.writeLong(file.length());

dos.flush();

while ((n = fis.read(buf)) != -1)

{

dos.write(buf, 0, n);

dos.flush();

}

}

dos.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

接收方代码是:

@Override

protected String doInBackground(Void... params)

{

try

{

ServerSocket serverSocket = new ServerSocket();

serverSocket.setReuseAddress(true);

serverSocket.bind(new InetSocketAddress(8988));

//ServerSocket serverSocket = new ServerSocket(8988);

Log.d(WiFiDirectActivity.TAG, "Server: Socket opened");

Socket client = serverSocket.accept();

Log.d(WiFiDirectActivity.TAG, "Server: connection done");

receive(client);

return "";

}

catch (IOException e)

{

e.printStackTrace();

return null;

}

}

public void receive(Socket socket)

{

try

{

DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

//read the number of files from the client

int number = dis.readInt();

ArrayList files = new ArrayList(number);

System.out.println("Number of Files to be received: " + number);

//read file names, add files to arraylist

for (int i = 0; i < number; i++)

{

File file = new File(dis.readUTF());

files.add(file);

}

int n;

byte[] buf = new byte[1024 * 8];

for (File file : files)

{

System.out.println("Receiving file: " + file.getName());

final File f = new File(Environment.getExternalStorageDirectory() + "/WiFiDirect/" + file.getName());

File dirs = new File(f.getParent());

if (!dirs.exists())

{

dirs.mkdirs();

}

f.createNewFile();

FileOutputStream fos = new FileOutputStream(f);

long fileSize = dis.readLong();

while (fileSize > 0 && (n = dis.read(buf, 0, (int) Math.min(buf.length, fileSize))) != -1)

{

fos.write(buf, 0, n);

fileSize -= n;

}

fos.close();

}

}

catch (IOException e)

{

e.printStackTrace();

}

finally

{

if (socket.isConnected())

{

try

{

socket.close();

}

catch (IOException e)

{

// Give up

e.printStackTrace();

}

}

}

}

所有代码都运行良好,当我第一次在两个应用程序上启动应用程序时,文件传输成功。但在第二次文件传输失败的接收方,

ServerSocket serverSocket = new ServerSocket();

serverSocket.setReuseAddress(true);

serverSocket.bind(new InetSocketAddress(8988));

上面第三行代码serverSocket.bind(new InetSocketAddress(8988));抛出异常

java.net.BindException:绑定失败:EADDRINUSE(地址已在使用中)

如何解决此异常?任何建议将不胜感激。

感谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值