java 套接字 - 地址已在使用中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11352650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:44:56  来源:igfitidea点击:

Socket - Address already in use

javaandroidsockets

提问by 113408

I'm new to Socketand I try to code an Server and Client on the same application just to see how it work.

我是新手Socket,我尝试在同一个应用程序上编写服务器和客户端,只是为了看看它是如何工作的。

Code:

代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((Button)this.findViewById(R.id.bouton1)).setOnClickListener(this);

}


public void onClick(View v) {
    TCPServer server = new TCPServer();
    TCPClient client = new TCPClient();
    server.start();
    client.start();     
}

public class TCPServer extends Thread {
    @Override public void run() {

        try {

            ServerSocket s = new ServerSocket(8080,0,InetAddress.getLocalHost());
            Socket cli = s.accept();

            byte[] b = new byte[512];
            int n;

            InputStream is = cli.getInputStream();
            while((n=is.read(b))>0){
                Log.d("TCPServer",new String(b));
                if(new String(b).contains("\r\n\r\n"))break;
                b = new byte[512];
            }

            OutputStream os = cli.getOutputStream();
            os.write("Hello".getBytes());

        } catch (Exception e) { 
            e.printStackTrace();
        } 



    }
}
public class TCPClient extends Thread {     
    @Override public void run() {

        try {
            Socket s = new Socket(InetAddress.getLocalHost().getHostAddress(),8080);
            //Socket s = new Socket("www.google.com",80);                               
            //Log.i("",s.getLocalAddress().getHostAddress());

            byte[] b = new byte[512];
            int n;

            if (s.isConnected()) {

                OutputStream os = s.getOutputStream();
                os.write("Hi How are you \r\n\r\n".getBytes());

                InputStream is = s.getInputStream();
                while((n=is.read(b))>0){
                    Log.d("TCPClient",new String(b));
                    b = new byte[512];
                }

            }

            s.close();

        } catch (Exception e) { 
            e.printStackTrace();
        } 


    }
}

The code work fine but just for the first time I click my button. the error is java.net.BindException: Address already in use.

代码工作正常,但只是我第一次单击我的按钮。错误是java.net.BindException: Address already in use

回答by Frank Sposaro

If it works the first time, but not after that it sounds like you aren't closing your socket correctly before your program exits.

如果它第一次工作,但之后没有,听起来你在程序退出之前没有正确关闭你的套接字。

You can check to see if it's still open by running

您可以通过运行来检查它是否仍然打开

netstat

pending your not on a windows machine. I'm sure they have something similar.

等待您不在 Windows 机器上。我确定他们有类似的东西。

回答by 113408

Sorry i just forgot to close the ServerSocketafter open it

对不起,我只是忘记关闭ServerSocket后打开它