-1

我正在尝试建立一个服务器,但 accept() 函数运行错误。问题是,它接受空连接(我不知道它们来自哪里)并且它还从它们那里接收空的东西。

这是我的 Server.cpp 类

#include "Server.h"
#include <thread>

Server::Server()
{
    usercount = 0;
    int RetVal=0;
    WSAData wsaData;
    WORD DllVersion=MAKEWORD(2,1);
    RetVal=WSAStartup(DllVersion,&wsaData);
    if( RetVal != 0 )
    {
        MessageBoxA(NULL,"WinSocket startup failed","Error",MB_OK | MB_ICONERROR );
        exit(1);
    }
}

bool Server::ServerStartUp()
{
sListen  = socket(AF_INET, SOCK_STREAM, NULL);
sConnect = socket(AF_INET, SOCK_STREAM, NULL);

addr.sin_addr.s_addr = inet_addr("192.168.0.100");
addr.sin_port        = htons(69);
addr.sin_family      = AF_INET;

bind(sListen, (SOCKADDR*)&addr,sizeof(addr));
listen(sListen,64);

//  CreateThread(0,0,&Server::ServerThread,0,0,0);
std::thread t1(&Server::ServerThread,this);
t1.join();

return true;
}

void Server::ServerThread()
{
int addrlen = sizeof(addr); 
for(;;Sleep(10))
{
    if( sConnect = accept( sListen, (SOCKADDR*)&addr, &addrlen )) 
    {
        cout << " FUTU ";
        char* UserName = new char[256];
        ZeroMemory(UserName,256);
        recv(sConnect,UserName,256,0);

        char* Password = new char[256];
        ZeroMemory(Password,256);
        recv(sConnect,Password,256,0);


        users[ ++usercount ] = new Client(UserName,Password,sConnect);

        if( users[usercount] ->GetLogInSuccesful() )
            send(sConnect,"0x0001",6,0);
        else
            send(sConnect,"0x0002",6,0);

    }
}

}

我也有一个结构几乎完全相同的工作示例,所以我不知道我在哪里或做错了什么......

工作示例:

int ReturnValue = 0;
ReturnValue = InitWinSocket();

if(ReturnValue != 0)
{
    MessageBoxA(NULL,"WinSocket startup failed","Error",MB_OK | MB_ICONERROR );
    exit(1);
}

users = new User[64];

sListen  = socket(AF_INET, SOCK_STREAM, NULL);
sConnect = socket(AF_INET, SOCK_STREAM, NULL);

addr.sin_addr.s_addr = inet_addr("192.168.0.100");
addr.sin_port        = htons(1234);
addr.sin_family      = AF_INET;

bind(sListen, (SOCKADDR*)&addr,sizeof(addr));
listen(sListen,64);


int addrlen = sizeof(addr);


for(;;)
{
    if(sConnect = accept(sListen , (SOCKADDR*)&addr,&addrlen))
    {
        int SuccesfulLogIn = 0;
        SuccesfulLogIn = CheckLogInInformation();

        if(SuccesfulLogIn == 0)
        {
            send(sConnect,"0x0002",6,0);
        }
        else
        {

        users[++ConnectionsCounter].UserConnection = sConnect;
        users[ConnectionsCounter].ID = ConnectionsCounter;
        users[ConnectionsCounter].IsUserInGame = false;
        cout<<"Smb Connected";
        send(sConnect,"0x0001",6,0);

        char* Id = new char[20];
        _itoa ( users[ConnectionsCounter].ID,Id,10 );

        send(sConnect,Id,sizeof(Id),0);

        UserThreadHandler[ConnectionsCounter] = CreateThread(0,0,(LPTHREAD_START_ROUTINE)UserThread,(LPVOID)ConnectionsCounter,0,0);

        }
    }
}

在第二个示例中,它只接受正确的连接,并且实际上在它遇到 accept() 时等待,直到找到连接。

我想知道我做错了什么。

由于评论而编辑:

发现它实际上是我的绑定函数不起作用,我该如何解决这个问题?

4

1 回答 1

1

我的猜测是,您正在尝试在不允许您运行程序的用户执行此操作的操作系统上侦听特权端口(低于 1024 的端口)。bind()、listen()、accept() 等返回指示成功或失败的值——你不应该在没有检查这些返回值的情况下调用它们。

于 2013-10-03T17:42:32.053 回答