java - 如何解决 java.net.ConnectException :/ip_address:port - Connection refused exception in android

标签 java android sockets inetaddress

我收到 java.net.ConnectException:/ip_address:port - Connection refused 异常。

代码如下:

public boolean sendMessage(String message, String ip, int port) 
    {
        try {


            Log.i("Log", "Socket Operator IP before split : "+ip);
            String[] str = ip.split("\\.");
            for(int i =0;i<str.length;i++)
            {
                Log.i("Log", "Socket Operator after split : "+str[i]);
            }
            byte[] IP = new byte[str.length];

            for (int i = 0; i < str.length; i++) {

                IP[i] = (byte) Integer.parseInt(str[i]);                
            }               
            Socket socket = getSocket(InetAddress.getByAddress(IP), port);
            if (socket == null) {
                Log.i("Log","Socket Operator SOCKET got null");
                return false;
            }

            PrintWriter out = null;
            out = new PrintWriter(socket.getOutputStream(), true);

            out.println(message);
        } catch (UnknownHostException e) {      
            Log.i("Log","Socket Operator SOCKET got UnknownHostException");
            return false;
            //e.printStackTrace();
        } catch (IOException e) {
            Log.i("Log","Socket Operator SOCKET got IOException");
            e.printStackTrace();
            return false;           
            //e.printStackTrace();
        }
        Log.i("Log","Socket Operator SOCKET got executed");
        return true;        
    }

这是我的 getSocket 方法:

private Socket getSocket(InetAddress addr, int portNo) 
    {
        Log.i("Log","Socket Operator InetAddress : "+addr.toString()+" Port : "+portNo);            
        Socket socket = null;
        if (sockets.containsKey(addr) == true) 
        {
            Log.i("Log","Socket Operator socket is true");
            socket = sockets.get(addr);
            // check the status of the socket
            if  ( socket.isConnected() == false ||
                  socket.isInputShutdown() == true ||
                  socket.isOutputShutdown() == true ||
                  socket.getPort() != portNo 
                 )  
            {           
                // if socket is not suitable,  then create a new socket
                sockets.remove(addr);               
                try {
                    socket.shutdownInput();
                    socket.shutdownOutput();
                    socket.close();
                    socket = new Socket(addr, portNo);
                    sockets.put(addr, socket);
                } 
                catch (IOException e) {                 
                    Log.e("getSocket: when closing and removing", "");
                }               
            }
        }
        else  
        {
            Log.i("Log","Socket Operator socket is false");
            try {
                Log.i("Log","Socket Operator socket is false in try start");
                socket = new Socket(addr, portNo);
                sockets.put(addr, socket);
            } catch (IOException e) {
                Log.e("getSocket: when creating", "");              
            }                   
        }
        return socket;      
    }

我点击了这些链接,但没有成功:

1 https://stackoverflow.com/a/6876306/1395259

2 java.net.ConnectException - Connection Refused Android Emulator

请帮我解决这个问题。

提前致谢。

最佳答案

我遇到过类似的问题,如果你看到消息

当端口错误或被阻止或没有服务时会出现这种情况:

java.net.ConnectException:在 xxms 之后无法连接到/xx.xx.xx.xx(端口 xx):isConnected 失败:ECONNREFUSED(连接被拒绝)

当无法在网络中找到 IP 或主机名或被阻止并指定超时时,您会看到如下消息:

java.net.SocketTimeoutException:10000 毫秒后无法连接到/xx.xx.xxx.xx(端口 xx)

当设备上没有连接网络时,您会看到如下消息:

java.net.ConnectException:10000 毫秒后无法连接到/xx.xx.xx.xxx(端口 xx):连接失败:ENETUNREACH(网络无法访问)


(备案),我的 API 在内部使用了这种方法:

com.android.okhttp.internal.http.HttpURLConnectionImpl.execute()

因此请检查您的案例的端口号,尝试使用不同的端口号。

关于java - 如何解决 java.net.ConnectException :/ip_address:port - Connection refused exception in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20492151/

相关文章:

java - Jar 与 DLL - 它们运行相似吗?

java - 415 不支持的媒体类型(JAVA + Angular + RestFULL JSON)

java - Spring Data 配置 - 找不到 hibernate.properties

sockets - HaProxy(无法绑定(bind)套接字,选择测试失败)

java - 传输了多少数据?

java - 安卓JAVA : Reading variables that a thread is accessing

java - 如何显示 Activity 中的数据库数据?

android - FCM demo 获取不到token

android - 如何在 Android 版 YouTube 播放器中捕获播放视频 Action

c - 在 C 中轮询 TCP 连接