简体   繁体   English

Android套接字连接拒绝错误

[英]Android socket connection refused error

I want to implement socket connection between 2 deceives , client keep sending GPS data to the server and I need both of it run in new thread , the client send first one data then keep show error like this 我想实现2个欺骗之间的套接字连接,客户端继续向服务器发送GPS数据,我需要它在新线程中运行,客户端发送第一个数据然后保持显示错误这样

03-18 16:35:11.805: E/Client run:(8163): java.net.ConnectException: failed to connect to /192.168.2.103 (port 5678): connect failed: ECONNREFUSED (Connection refused) 03-18 16:35:11.805:E / Client运行:(8163):java.net.ConnectException:无法连接到/192.168.2.103(端口5678):连接失败:ECONNREFUSED(连接被拒绝)

here is the client code 这是客户端代码

    public class Send implements Runnable{

    private boolean Connect = true;

    public void Connect(){
        Connect = true;
    }
    public void Disconnect(){
        Connect = false;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(Connect){
            try {
                SocketClient = new Socket("192.168.2.103", 5678);
                ObjectOutputStream oos = new ObjectOutputStream(SocketClient.getOutputStream());
                oos.writeDouble(GPSinfo[2]);
                //ObjectInputStream ois = new ObjectInputStream(SocketClient.getInputStream());
                //ois.readInt();
                oos.close();
                //ois.close();
            } catch (Exception e) {
                Log.e("Client run: ", e.toString());
            }
        }

    }

}

here is server code 这是服务器代码

    public class Receive implements Runnable{
    private boolean CanReceive = true;
    private double Data;

    public void Connect(){
        CanReceive = true;
    }
    public void Disconnect(){
        CanReceive = false;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        while(CanReceive){
            try {
                SocketServer = new ServerSocket(5678);
                Socket connectedSocket = SocketServer.accept();
                ObjectInputStream ois = new ObjectInputStream(connectedSocket.getInputStream());
                Data = ois.readDouble();
                DataText.setText("" + Data);
                //ObjectOutputStream oos = new ObjectOutputStream(connectedSocket.getOutputStream());
                //oos.writeInt(1);
                //ois.close();
                //oos.close();
            } catch (Exception e) {
                Log.e("Server run: ", e.toString());
            }

        }
    }

}

by the way , the both code is inner class , and INTERNET permission is added. 顺便说一下,这两个代码都是内部类,并添加了INTERNET权限。

It's obvious it's not a router-firewall related problem as you are under the same net, so there are only a few possibilities: 很明显,这不是路由器防火墙相关的问题,因为你在同一个网络下,所以只有几个可能性:

  • There's nothing listening on that port on that IP on the server-side 在服务器端的那个IP上没有监听该端口
  • There's a local firewall on the server-side that is blocking that connection attempt 服务器端有一个本地防火墙阻止了这种连接尝试
  • You are not using WIFI so you're not under the same net. 您没有使用WIFI,因此您不在同一个网络中。

You should make sure you can open that service some ther way, that would help you debugging where the culprit is. 您应该确保可以以某种方式打开该服务,这将有助于您调试罪魁祸首的位置。 If you've already done this, I'd suggest using some debugging tool to trace TCP packets (I don't know either what kind of operating system you use on the destination machine; if it's some linux distribution, tcpdump might help, in Win environments WireShark works just good). 如果你已经这样做了,我建议使用一些调试工具来跟踪TCP数据包(我不知道你在目标机器上使用什么样的操作系统;如果它是一些linux发行版, tcpdump可能有帮助,在赢得环境WireShark工作得很好)。

This isn't a 'data transfer error'. 这不是'数据传输错误'。 This is a 'connection refused' error. 这是“拒绝连接”错误。 It means the server you want to transfer the data to or from isn't running at the IP:port you specified. 这意味着要将数据传入或传出的服务器未在您指定的IP:端口上运行。

Try killing the adb service before you begin the connection. 尝试在开始连接之前终止adb服务。 I had a similar problem and killing the adb service before the connection resolved the issue. 我遇到了类似的问题并在连接解决问题之前终止了adb服务。

I had the same error. 我有同样的错误。 I simply used ServerSocket and it worked well. 我只是使用ServerSocket而且效果很好。

ServerSocket socket = new ServerSocket(8888);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM