Tony
Tony

Reputation: 111

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

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)

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.

Upvotes: 3

Views: 28281

Answers (4)

user207421
user207421

Reputation: 310913

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.

Your basic problem is that you are creating a new ServerSocket every time inside the loop, instead of using the same one for the life of the application.

Upvotes: 3

Silas
Silas

Reputation: 37

I had the same error. I simply used ServerSocket and it worked well.

ServerSocket socket = new ServerSocket(8888);

Upvotes: -2

nerd
nerd

Reputation: 369

Try killing the adb service before you begin the connection. I had a similar problem and killing the adb service before the connection resolved the issue.

Upvotes: 0

nKn
nKn

Reputation: 13761

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
  • 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.

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).

Upvotes: 3

Related Questions