java.net.ConnectException拒绝连接

时间:2017-12-30 10:51:24

标签: android sockets server client connection-refused

我在尝试为Android实现服务器/客户端应用程序时遇到了问题。

我实现了一个服务器类,我首先通过构造函数初始化,然后启动一个异步任务来运行它。

这是服务器类:

public class Server {
    RestaurantTables activity;
    ServerSocket serverSocket;
    String message = "";

    Handler updateConversationHandler;

    //static final int socketServerPORT = 8080;
    static final int socketServerPORT = 0; // 0 = take any free port

    public Server(RestaurantTables activity) {
        this.activity = activity;
        //Thread socketServerThread = new Thread(new SocketServerThread(this.activity.getHandler()));
        updateConversationHandler = new Handler();
        map = new HashMap();
        try {
            // create ServerSocket using specified port
            serverSocket = new ServerSocket(socketServerPORT);
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SocketConnectAsyncTask atSockConn = new SocketConnectAsyncTask();
        atSockConn.execute();

    }

    public int getPort() {
        return serverSocket.getLocalPort();
    }

    public void closeSocket() {
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private class SocketConnectAsyncTask extends AsyncTask<Void, Void, Void>
    {

        int count = 0;
        Handler mHandler;
        Socket socket;

        @Override
        //public void run() {
        protected Void doInBackground(Void... params) {
            try {
                /*// create ServerSocket using specified port
                serverSocket = new ServerSocket(socketServerPORT);
*/
                while (true) {
                    // block the call until connection is created and return
                    // Socket object
                    socket = serverSocket.accept();
                    count++;
                    message += "#" + count + " from "
                            + socket.getInetAddress() + ":"
                            + socket.getPort() + "\n";
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            ReceiveMessage atRecMsg = new ReceiveMessage(socket, this.mHandler);
            atRecMsg.execute();
        }
    }

    class ReceiveMessage extends AsyncTask<Void, Void, Void> {

        private Socket clientSocket;
        private Handler mHandler;
        private BufferedReader input;


        public ReceiveMessage(Socket clientSocket, Handler mHandler)
        {
            this.clientSocket = clientSocket;
            this.mHandler = mHandler;
            try {
                this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected Void doInBackground(Void... params) {
            while(true) {
                try {
                    String read = input.readLine();// blocking
                    Message msg = Message.obtain();
                    msg.obj = read; // Put the string into Message, into "obj" field.
                    msg.setTarget(mHandler); // Set the Handle
                    System.out.println("Here is what I read: " + read);
                    //updateConversationHandler.post(new updateUIThread(read));
                    //mHandler.sendMessage();
                    msg.sendToTarget(); //Send the message

                    try {
                        OutputStream outputStream = clientSocket.getOutputStream();
                        PrintStream printStream = new PrintStream(outputStream);
                        printStream.print("OK");
                        printStream.close();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                }
            }
            //return null;
        }

    }

    public String getIpAddress() {
        String ip = "";
        try {
            Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (enumNetworkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = enumNetworkInterfaces
                        .nextElement();
                Enumeration<InetAddress> enumInetAddress = networkInterface
                        .getInetAddresses();
                while (enumInetAddress.hasMoreElements()) {
                    InetAddress inetAddress = enumInetAddress
                            .nextElement();

                    if (inetAddress.isSiteLocalAddress()) {
                        ip = inetAddress.getHostAddress();
                    }
                }
            }

        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ip += "Something Wrong! " + e.toString() + "\n";
        }
        return ip;
    }


}

服务器类的调用如下:

myServer = new Server (this); // this = activity

所以我认为服务器正在运行。尝试通过以下呼叫从客户端连接...

Client myClient = new Client(sServerIpAddress, sServerPort, Commands.this);

...我收到以下异常:

W/System.err: java.net.ConnectException: failed to connect to /192.168.200.2 (port 47803) from /:: (port 48982): connect failed: ECONNREFUSED (Connection refused)
W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:138)
W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
W/System.err:     at java.net.Socket.connect(Socket.java:616)
W/System.err:     at java.net.Socket.connect(Socket.java:565)
W/System.err:     at java.net.Socket.<init>(Socket.java:445)
W/System.err:     at java.net.Socket.<init>(Socket.java:248)
W/System.err:     at emu.apps.com.jollybell.Client.doInBackground(Client.java:45)
W/System.err:     at emu.apps.com.jollybell.Client.doInBackground(Client.java:20)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err:     at java.lang.Thread.run(Thread.java:764)
W/System.err: Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
W/System.err:     at libcore.io.Linux.connect(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:126)
W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:152)
W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:130)
W/System.err:   ... 17 more

老实说,即使在所有可能的论坛中搜索了几个小时之后,我也不知道出了什么问题。

如果有帮助,这是客户端类:

class Client extends AsyncTask<String, Void, String> {

    public interface ServerResponse {
        void serverResultReceived(String output);
    }

    String sIpAddress, sPort, response;
    private Socket socket;
    private PrintWriter out;

    public ServerResponse delegate = null;

    public Client(String sIpAddress, String sPort, ServerResponse delegate)
    {
        this.sIpAddress = sIpAddress;
        this.sPort = sPort;
        this.delegate = delegate;
    }

    //@Override
    //public void run() {
    @Override
    protected String doInBackground(String... params) {
        try {
            InetAddress serverAddr = InetAddress.getByName(this.sIpAddress);
            socket = new Socket(serverAddr, Integer.parseInt(this.sPort));

            // First send command
            out = new PrintWriter(new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream())),
            true);
            out.print(params[0]);

            // ... then wait for answer
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                    1024);
            byte[] buffer = new byte[1024];

            int bytesRead;
            InputStream inputStream = socket.getInputStream();

            /*
             * notice: inputStream.read() will block if no data return
             */
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
                response += byteArrayOutputStream.toString("UTF-8");
            }

        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        delegate.serverResultReceived(result);
    }
}

1 个答案:

答案 0 :(得分:0)

所以大家。前段时间,但我发现了如何解决我的问题。

首先,在客户端有一个小问题。我正在使用“out.print”函数编写,然后在服务器中使用“readLine()”函数进行等待。

readLine期望一行的结束,因此字符串应该以最后的“/ n”发送,或者必须使用函数“println”而不是print。

其次,当我启动2个Android设备时,他们无法相互通信。我仍然没有找到原因,但使用相同的设备来运行服务器和客户端应用程序为我工作。