Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.net.BindException:绑定失败:EADDRINUSE_Java_Android_Sockets_Serversocket - Fatal编程技术网

java.net.BindException:绑定失败:EADDRINUSE

java.net.BindException:绑定失败:EADDRINUSE,java,android,sockets,serversocket,Java,Android,Sockets,Serversocket,我是android新手,我正在尝试创建一个服务器套接字。代码如下 我不断收到警告。能修好吗?我可以忽略它吗 03-28 15:47:34.460: W/System.err(3185): java.net.BindException: bind failed: EADDRINUSE (Address already in use) 03-28 15:47:34.460: W/System.err(3185): at libcore.io.IoBridge.bind(IoBridge.j

我是android新手,我正在尝试创建一个服务器套接字。代码如下

我不断收到警告。能修好吗?我可以忽略它吗

03-28 15:47:34.460: W/System.err(3185): java.net.BindException: bind failed: EADDRINUSE (Address already in use)

03-28 15:47:34.460: W/System.err(3185):     at libcore.io.IoBridge.bind(IoBridge.java:89)

03-28 15:47:34.460: W/System.err(3185):     at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:150)

03-28 15:47:34.460: W/System.err(3185):     at java.net.ServerSocket.<init>(ServerSocket.java:100)

03-28 15:47:34.470: W/System.err(3185):     at java.net.ServerSocket.<init>(ServerSocket.java:69)

03-28 15:47:34.470: W/System.err(3185):     at <path>$server.run(<filename>.java:302)

03-28 15:47:34.470: W/System.err(3185):     at java.lang.Thread.run(Thread.java:856)
03-28 15:47:34.470: W/System.err(3185): Caused by: libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)

03-28 15:47:34.470: W/System.err(3185):     at libcore.io.Posix.bind(Native Method)
03-28 15:47:34.470: W/System.err(3185):     at ibcore.io.ForwardingOs.bind(ForwardingOs.java:39)
03-28 15:47:34.470: W/System.err(3185):     at libcore.io.IoBridge.bind(IoBridge.java:87)
03-28 15:47:34.470: W/System.err(3185):     ... 5 more

03-28 15:47:34.470: W/System.err(3185): java.lang.NullPointerException
03-28 15:47:34.490: W/System.err(3185):     at <path>Provider$server.run(<filename>.java:315)

03-28 15:47:34.490: W/System.err(3185):     at java.lang.Thread.run(Thread.java:856)

您的异常处理已经到了极点

创建
ServerSocket
之后的catch根本不应该在那里:它应该在使用
ServerSocket
的代码之后,在这种情况下,它可以与现有的第二个catch组合。依赖先前try块中代码成功的代码应位于该try块内

但是,持续出现错误的唯一方法是持续启动线程,这没有任何意义,因为在任何给定的TCP端口上只能有一个侦听器

因此,请调查一遍又一遍地启动线程的原因,并停止它,

您可以从另一个线程调用,accept()调用将抛出SocketException。这样,在下一次调用ServerSocket构造函数时,您将不会得到“:bind failed:EADDRINUSE”

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"`enter code here`
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="project.vasile.emanuel.gresanu.test_server_socket.MainActivity">

<Button
    android:id="@+id/btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click"/>
</RelativeLayout>
我的服务器

public class My_Server extends AsyncTask<Void, Void, Void> {

    private static final String TAG = My_Server.class.getSimpleName();
    private static final int PORT = 8091;
    private static boolean isExiting;
    private ServerSocket serverSocket;

    @Override
    protected Void doInBackground(Void... params) {
        isExiting = false;
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Socket clientSocket=null;
        if(null!=serverSocket)
        while (!isExiting) {
            try {
                Log.i(TAG, "Waiting for a new connection");
                clientSocket=serverSocket.accept();
                Log.i(TAG, "Connction accepted");
                //Do something with the new client
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
                if(null!=this.serverSocket&& !serverSocket.isClosed()) {
                    try {
                        Log.i(TAG,"Closing Server connection");
                        this.serverSocket.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        }
        try {
            Log.i(TAG,"Closing the connection");
            if(null!=serverSocket && !serverSocket.isClosed()) {
                serverSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG,e.getMessage());
        }
        return null;
    }

    public void cancelAccept() {
        Log.i(TAG, "cancelAccept()");
        isExiting = true;
        if(null!=this.serverSocket&& !serverSocket.isClosed())
        {
            try {
                Log.i(TAG,"Closing Server connection in onCancelled");
                this.serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.gc();
    }
}
公共类My_服务器扩展异步任务{
private static final String TAG=My_Server.class.getSimpleName();
专用静态最终int端口=8091;
私有静态布尔Isexit;
私有服务器套接字服务器套接字;
@凌驾
受保护的Void doInBackground(Void…参数){
isExiting=false;
试一试{
serverSocket=新的serverSocket(端口);
}捕获(IOE异常){
e、 printStackTrace();
}
套接字clientSocket=null;
if(null!=服务器套接字)
而(!IsExit){
试一试{
Log.i(标记“等待新连接”);
clientSocket=serverSocket.accept();
Log.i(标签“接受连接”);
//对新客户做些什么
}捕获(例外e){
e、 printStackTrace();
Log.e(标记,e.getMessage());
if(null!=this.serverSocket&!serverSocket.isClosed()){
试一试{
Log.i(标记“关闭服务器连接”);
这个.serverSocket.close();
}捕获(IOE2异常){
e2.printStackTrace();
}
}
}
}
试一试{
Log.i(标记“关闭连接”);
if(null!=serverSocket&!serverSocket.isClosed()){
serverSocket.close();
}
}捕获(IOE异常){
e、 printStackTrace();
Log.e(标记,e.getMessage());
}
返回null;
}
公共作废取消接受(){
Log.i(标记“cancelAccept()”);
isExiting=true;
if(null!=this.serverSocket&!serverSocket.isClosed())
{
试一试{
i(标记“在onCancelled中关闭服务器连接”);
这个.serverSocket.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
gc();
}
}

看起来应用程序已经在您尝试创建新套接字的端口上运行。不应忽略此错误,因为它意味着代码未按预期工作。选择一个不同的端口(而不是
10000
)将是一个很好的开始。。。为此奋斗了1小时。。。终于开始工作了。。尝试捕捉仪式并删除了一个额外的开始校准。。
public class MainActivity extends AppCompatActivity {

    private static final String TAG=MainActivity.class.getSimpleName();
    private static boolean isClick;
    private My_Server My_Server;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button) findViewById(R.id.btn);
        isClick=false;
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if(!isClick)
                 {
                     Log.i(TAG,"Initilize and Running My_Server");
                     My_Server =new My_Server();
                     if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB)
                     {
                         My_Server.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,(Void[])null);
                     }
                     else
                     {
                         My_Server.execute((Void[])null);
                     }
                     isClick=true;
                 }
                else
                 {
                    if(null!= My_Server)
                    {
                        My_Server.cancelAccept();
                        isClick=false;
                    }
                 }
            }
        });
    }
}
public class My_Server extends AsyncTask<Void, Void, Void> {

    private static final String TAG = My_Server.class.getSimpleName();
    private static final int PORT = 8091;
    private static boolean isExiting;
    private ServerSocket serverSocket;

    @Override
    protected Void doInBackground(Void... params) {
        isExiting = false;
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Socket clientSocket=null;
        if(null!=serverSocket)
        while (!isExiting) {
            try {
                Log.i(TAG, "Waiting for a new connection");
                clientSocket=serverSocket.accept();
                Log.i(TAG, "Connction accepted");
                //Do something with the new client
            } catch (Exception e) {
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
                if(null!=this.serverSocket&& !serverSocket.isClosed()) {
                    try {
                        Log.i(TAG,"Closing Server connection");
                        this.serverSocket.close();
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                }
            }
        }
        try {
            Log.i(TAG,"Closing the connection");
            if(null!=serverSocket && !serverSocket.isClosed()) {
                serverSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG,e.getMessage());
        }
        return null;
    }

    public void cancelAccept() {
        Log.i(TAG, "cancelAccept()");
        isExiting = true;
        if(null!=this.serverSocket&& !serverSocket.isClosed())
        {
            try {
                Log.i(TAG,"Closing Server connection in onCancelled");
                this.serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.gc();
    }
}