ftp服务器的问题c++语言

计网课程设计要做一个ftp服务器并用命令行进行连接和访问,
现在在连接的时候不知道为什么连接不上,服务器过一会儿就自动断开
图片说明
下面是代码

#include<io.h>
#include<string>
#include<iostream>
#include<vector>
#include<thread>
#include<WS2tcpip.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable : 4996)
int main()
{
    WORD sockVersion = MAKEWORD(2, 2);
    WSADATA data;
    if (WSAStartup(sockVersion, &data) != 0)
    {
        std::cout << "WSAStartup error" << std::endl;
        return 0;
    }

    SOCKET serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (INVALID_SOCKET == serverSock)
    {
        std::cout << "socket erroro" << std::endl;
        closesocket(serverSock);
        WSACleanup();
        return 0;
    }

    sockaddr_in cmd_port;
    cmd_port.sin_family = AF_INET;
    cmd_port.sin_port = htons(21);
    inet_pton(AF_INET, "127.0.0.1", &cmd_port.sin_addr);

    if (bind(serverSock, (SOCKADDR*)& cmd_port, sizeof(SOCKADDR)))
    {
        std::cout << "bind error" << std::endl;
        closesocket(serverSock);
        WSACleanup();
        return 0;
    }

    if (listen(serverSock, 5) == SOCKET_ERROR)
    {
        std::cout << "listen error" << std::endl;
        closesocket(serverSock);
        WSACleanup();
        return 0;
    }

    SOCKET userSock;//用户端socket信息
    sockaddr_in user_sockAddr;//用于accept函数,存放客户端的信息
    int addrlen = sizeof(user_sockAddr);

    while (true)
    {
        std::cout << "waiting to be connected..." << std::endl;
        userSock = accept(serverSock, (SOCKADDR*)& user_sockAddr, &addrlen);//接受用户端请求,返回一个套接字
        if (INVALID_SOCKET == userSock)
        {
            std::cout << "accept error." << std::endl;
            continue;
        }
        char user_ip[16];
        inet_ntop(AF_INET, &user_sockAddr.sin_addr, user_ip, sizeof(user_ip));
        std::cout << "接收到一个链接:" << user_ip << std::endl;

    }
}

望各位大触指教

c++

1个回答

因为客户端在**等待服务端响应**!请先搞清楚ftp的工作流程!比如这个这个

另外客户端主动断开连接也会显示“远程主机断开连接”,但这是个本地化问题,原文是"remote host has disconected"

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问