关闭一个旧后新创建套接字连接导致拒绝 [英] Creating new socket after closing an old one causes connection refused

查看:603
本文介绍了关闭一个旧后新创建套接字连接导致拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新到Android编程,我试图从端口标准的Java程序到Android。这个Java应用程序包含插槽。当我移植程序我得到一个连接被拒绝,如果我preSS按钮太快。我做了一个简单的程序进行测试。

I'm new to android programming and I'm trying to port a program from standard java to android. This java application contains sockets. When I ported the program I get a connection refused if I press a button too fast. I made a simple program for testing.

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Socket mySocket = new Socket();
            InetSocketAddress address = new InetSocketAddress("10.94.35.24", 5000);
            mySocket.connect(address, 5000);
            mySocket.shutdownOutput();
            mySocket.shutdownInput();
            mySocket.close();
            Log.w("Socket", "Socket closed");
            Socket mySocket2 = new Socket();
            mySocket2.connect(address, 5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
thread.start();

我有一台服务器和这个IP和端口运行,我的程序正常工作在我的计算机上的Java应用程序。但在Android的程序获取尝试连接到mySocket2时以下异常

I have a server up and running on this IP and port and my program works fine on a java application on my computer. But on android the program gets the following exception when trying to connect to mySocket2

java.net.ConnectException:无法连接到/10.94.35.24(端口
  5000)后,5000毫秒:isConnected失败:ECONNREFUSED(连接
  拒绝)

java.net.ConnectException: failed to connect to /10.94.35.24 (port 5000) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)

我的猜测是,这事做与Android操作系统的限制,但我希望不会。有谁知道,如果我做错事,或可以确认插座,Android的这些限制?

My guess is that this has something to do with a limitation in the android OS but I hope not. Does anyone know if I'm doing something wrong or can confirm that sockets have these limitations in Android?

推荐答案

公共类的ConnectException
扩展SocketException

public class ConnectException extends SocketException

说:,同时试图将套接字连接到远程地址和端口时发生了错误的信号。通常情况下,连接被远程拒绝(例如,没有任何进程正在监听的远程地址/端口)。

连接被拒绝错误意味着在服务器计算机上的插座堆栈收到连接请求,并故意拒绝接受。出现这种情况的两个可能的原因:

"Connection Refused" error means the socket stack on the server machine received your connection request and intentionally refused to accept it. That happens for one of two possible reasons:

1)是您试图连接的端口上没有运行监听套接字。

1) There is no listening socket running on the port you are trying to connect to.

2)有一个监听套接字,但它挂起连接的积压是满的,所以没有空间来排队你在那一刻的请求。

2) There is a listening socket, but its backlog of pending connections is full, so there is no room to queue your request at that moment.

要区分这两种,尝试在每次尝试之间的延迟重新连接几次。如果你得到同样的错误持续,那么#1可能是罪魁祸首。确保端口号是正确的。如果#2是罪魁祸首,您重新连接了最终成功的机会。

To differentiate between the two, try reconnecting a few times with a delay in between each attempt. If you get the same error consistently, then #1 is likely the culprit. Make sure the port number is correct. If #2 is the culprit, your reconnect has a chance of succeeding eventually.

这篇关于关闭一个旧后新创建套接字连接导致拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆