Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The server has connected, but the client hasn't. #1350

Open
yangman92 opened this issue Aug 9, 2023 · 1 comment
Open

The server has connected, but the client hasn't. #1350

yangman92 opened this issue Aug 9, 2023 · 1 comment

Comments

@yangman92
Copy link

I used a tool as a server. and i used my phone to connect with.
The problem now is a the server was connected when i used my phone to connect.

Client coding blow:

public class ClientSocket extends WebSocketClient {

    private final static String TAG = "Socket - ClientSocket";

    private OnClientSocketListener onClientSocketListener;

    public void setOnClientSocketListener(OnClientSocketListener onClientSocketListener) {
        this.onClientSocketListener = onClientSocketListener;
    }

    public ClientSocket(URI serverUri) {
        super(serverUri);
        LogUtils.e(TAG, "ClientSocket " + serverUri.toString());
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        LogUtils.e(TAG, "onOpen 连接成功");

        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("连接到服务端成功");
        }
    }

    @Override
    public void onMessage(String message) {
        LogUtils.e(TAG, "onMessage 收到消息: " + message);

        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("收到服务端发来的消息" + message);
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        LogUtils.e(TAG, "onClose 连接被" + (remote ? "remote peer" : "us") + "关闭 " + " Code: " + code + " Reason: " + reason);

        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("断开了  连接被" + (remote ? "remote peer" : "us") + "关闭 " + " Code: " + code + " Reason: " + reason);
        }
    }

    @Override
    public void onError(Exception ex) {
        ex.printStackTrace();
        LogUtils.e(TAG, "onError " + ex.getMessage());
        if (onClientSocketListener != null) {
            onClientSocketListener.onLogging("发生异常" + ex.getMessage());
        }
    }

use location

         URI uri = new URI("ws://" + NetworkUtils.getServerAddressByWifi() + ":3333");
                        clientSocket = new ClientSocket(uri);

                        clientSocket.setSocketFactory(SocketFactory.getDefault());
                        clientSocket.connect();

1691568603978
The result from the picture show that there is a client has successfuly connected, but i didn't see any error from my client.

Can you give me an idea about this problem? Thanks a lot.

@yangman92
Copy link
Author

yangman92 commented Aug 9, 2023

In addition, i also code a server. I used two as a server and a client. it worked , both successded and can sent message to each.

But i need to connect the tool that is our device.

server coding


public class ServerSocket extends WebSocketServer {

    private static final String TAG = "Socket - ServerSocket";

    private OnServerSocketListener onServerSocketListener;

    public void setOnServerSocketListener(OnServerSocketListener onServerSocketListener) {
        this.onServerSocketListener = onServerSocketListener;
    }

    public ServerSocket(int port) throws UnknownHostException {
        super(new InetSocketAddress(port));
    }

    public ServerSocket(InetSocketAddress address) {
        super(address);

        LogUtils.i(TAG, "ServerSocket" + address.getHostString());
    }

    public ServerSocket(int port, Draft_6455 draft) {
        super(new InetSocketAddress(port), Collections.<Draft>singletonList(draft));
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        LogUtils.i(TAG, "onOpen 加入连接" + conn.getRemoteSocketAddress().getAddress().getHostAddress());

        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 连接到服务端");
        }
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        LogUtils.i(TAG, "onClose 断开连接" + conn.getRemoteSocketAddress().getAddress().getHostAddress());
        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 断开链接");
        }
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        LogUtils.i(TAG, "onMessage 收到信息" + conn.getRemoteSocketAddress().getAddress().getHostAddress() + message);
        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " 发送了消息 : " + message);
        }
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        ex.printStackTrace();
        LogUtils.i(TAG, "onError " + ex.getMessage());

        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging("服务端异常" + ex.getMessage());
        }
    }

    @Override
    public void onStart() {
        LogUtils.i(TAG, "onStart " + getAddress().toString());

        if (onServerSocketListener != null) {
            onServerSocketListener.onLogging("服务端启动 " + getAddress().toString());
        }
    }
}

where i used to

    server = new ServerSocket(new InetSocketAddress(3333));
                    server.start();

@yangman92 yangman92 changed the title The server has connected, but the client hadn't. The server has connected, but the client hasn't. Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant