40

C++客户端和java服务端TCP报错10061怎么回事?

这是我的C++客户端的代码,但连接不上JAVA的服务端,报错10061,是还需要写些什么吗?

    WSADATA WsaData;
    char buff[BUFFER_SIZE];

    memset(buff, 0, sizeof(buff));
    if (WSAStartup(MAKEWORD(1, 1), &WsaData) != 0)
    {
        printf("%d \n", GetLastError());
        return;
    }
    if (LOBYTE(WsaData.wVersion) != 1 || HIBYTE(WsaData.wVersion) != 1)
    {
        printf("%d \n", GetLastError());
        WSACleanup();
        system("pause");
        return;
    }

SOCKET SocketClient = socket(AF_INET, SOCK_STREAM, 0);

    SOCKADDR_IN root;
    root.sin_addr.S_un.S_addr = inet_addr("192.168.0.171");
    root.sin_family = AF_INET;
    root.sin_port = htons(14870);

    if (connect(SocketClient, (struct  sockaddr*)&root, sizeof(root)) == INVALID_SOCKET)
    {
        printf("连接服务器失败: %d \n", WSAGetLastError());
        return ;
    }
    else
    {
        //接收数据
        recv(SocketClient, buff, sizeof(buff), 0);
    }

    char Buffer[MAX_BUF_LEN];
    strcpy(Buffer, Log.c_str());
    send(SocketClient, Buffer, sizeof(Buffer), 0);

    closesocket(SocketClient);
    WSACleanup();

本人不懂JAVA语言,不清楚JAVA是这么写的,像我这样可以正常通信吗?

这是JAVA服务器的代码

public class Receive {
    private static final int LISTEN_PORT = 14870;
    private ServerSocket serverSocket;

    @Test
    public void receive(){

        System.out.println("日志代理接收器开始启动,监听端口:" + LISTEN_PORT + " 。。。");
        try {
            serverSocket = new ServerSocket(LISTEN_PORT);
        } catch (IOException e) {
            System.out.println("启动日志代理接收器失败");
            System.out.println(e);
            return;
        }

        while (true){
            try(Socket socket = serverSocket.accept()) {
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String msg = br.readLine();
                System.out.println("接收到一条日志:"+msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

更新下:

图片说明

现在是我在占用服务器的接受端口14870,但服务器接受不到数据,我的程序也无法往下走

查看全部
a513155803
a513155803
2019/03/28 14:54
  • c++
  • java
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复