如何解决java.net.BindException:绑定失败:EADDRINUSE(地址已在使用中)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何解决java.net.BindException:绑定失败:EADDRINUSE(地址已在使用中),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4803字,纯文字阅读大概需要7分钟。
内容图文
![如何解决java.net.BindException:绑定失败:EADDRINUSE(地址已在使用中)](/upload/InfoBanner/zyjiaocheng/725/7ac79bc880d34772815481fb7d80d218.jpg)
我使用套接字从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<File> filesList = new ArrayList<File>();
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<File> 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<File> files = new ArrayList<File>(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(地址已在使用中)
如何解决此异常?任何建议将不胜感激.
谢谢
解决方法:
I am starting service every time to send files but not stop it, is there any need to stop the service before sending again?
是的,当然有,否则它仍在侦听端口8988,所以你无法启动另一个实例.
if yes then what is the best way to do that?
杀死旧的.但是,为什么你在它已经运行时启动它?
内容总结
以上是互联网集市为您收集整理的如何解决java.net.BindException:绑定失败:EADDRINUSE(地址已在使用中)全部内容,希望文章能够帮你解决如何解决java.net.BindException:绑定失败:EADDRINUSE(地址已在使用中)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。