绑定失败:EADDRINUSE(地址已在使用中)

时间:2016-06-23 09:44:36

标签: android android-wifi

我正在尝试使用套接字通信从两端发送文件。我创建了一个serverSocket对象并创建了连接。     client = serverSocket.accept(); 现在我能够从两侧发送文件,但只能发送一次。当我断开连接并重新连接时,我收到错误::绑定失败:EADDRINUSE(地址已在使用中)

我已正确关闭服务器套接字和客户端套接字。但我仍然得到这个错误..

你能否告诉某人我在哪里做错了。

protected String doInBackground(String... params) {
            try {
                CommonMethods.e("File Async task port", "File Async task port-> " + PORT);
                // init handler for progressdialog
                serverSocket = new ServerSocket();

                serverSocket.bind(new InetSocketAddress(PORT));
                serverSocket.setReuseAddress(true);
                Log.d(CommonMethods.Tag, "Server: Socket opened");
                client = serverSocket.accept();

                Log.d("Client's InetAddresssss  ", "" + client.getInetAddress());

                WiFiClientIp = client.getInetAddress().getHostAddress();

                ObjectInputStream ois = new ObjectInputStream(
                        client.getInputStream());
                WiFiTransferModal obj = null;
                // obj = (WiFiTransferModal) ois.readObject();
                String InetAddress;
                try {
                    obj = (WiFiTransferModal) ois.readObject();
                    InetAddress = obj.getInetAddress();
                    if (InetAddress != null
                            && InetAddress
                            .equalsIgnoreCase(FileTransferService.inetaddress)) {
                        CommonMethods.e("File Async Group Client Ip", "port-> "
                                + WiFiClientIp);
                        SharedPreferencesHandler.setStringValues(mFilecontext,
                                "WiFiClientIp", WiFiClientIp);
                        CommonMethods
                        .e("File Async Group Client Ip from SHAREDPrefrence",
                                "port-> "
                                        + SharedPreferencesHandler
                                        .getStringValues(
                                                mFilecontext,"WiFiClientIp"));
                        //set boolean true which identifiy that this device will act as server.
                        SharedPreferencesHandler.setStringValues(mFilecontext,
                                "ServerBoolean", "true");
                        ois.close(); 

                        serverSocket.close();

                        return "Demo";
                    }
                } catch (ClassNotFoundException e) {;
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
                final Runnable r = new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        mProgressDialog.setMessage("Receiving...");
                        mProgressDialog.setIndeterminate(false);
                        mProgressDialog.setMax(100);
                        mProgressDialog.setProgress(0);
                        mProgressDialog.setProgressNumberFormat(null);
                        mProgressDialog.setCancelable(false);
                        mProgressDialog
                        .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        mProgressDialog.show();
                    }
                };
                handler.post(r);
                Log.e("FileName got from socket on other side->>> ",
                        obj.getFileName());

                final File f = new File(
                        Environment.getExternalStorageDirectory() + "/"
                                + FolderName + "/"
                                + obj.getFileName());

                File dirs = new File(f.getParent());
                if (!dirs.exists())
                    dirs.mkdirs();
                f.createNewFile();

                System.out.println("dirs==========="+dirs);
                /*
                 * Recieve file length and copy after it
                 */
                this.ReceivedFileLength = obj.getFileLength();

                InputStream inputstream = client.getInputStream();


                copyRecievedFile(inputstream, new FileOutputStream(f),ReceivedFileLength);

                ois.close(); 
                serverSocket.close();

                this.Extension = obj.getFileName();
                this.EncryptedFile = f;

                return f.getAbsolutePath();
            } catch (Exception e) {
                Log.e(WiFiDirectActivity.TAG, e.getMessage());
                return null;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null) {
                FileServerAsyncTask FileServerobj = new
                        FileServerAsyncTask(mFilecontext,FileTransferService.PORT); 
                if(!result.equalsIgnoreCase("Demo")){


                    try{
                        serverSocket.close();
                    }catch (Exception e) {
                        // TODO: handle exception
                    }
                }
                else{

                    if(FileServerobj != null) { 
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                            FileServerobj.executeOnExecutor (AsyncTask.THREAD_POOL_EXECUTOR, new String[] { null }); 

                        }
                        else FileServerobj.execute();

                    }

                }

            }

        }

1 个答案:

答案 0 :(得分:0)

这种逻辑似乎很好。至于它是异步代码 - 我会说错误是在这个代码之外的某个地方。尝试检查你是否真的关闭连接之前打开新连接(因此它们不会在时间上重叠)。