C ++套接字表现得很奇怪

时间:2013-10-03 17:18:21

标签: c++ windows sockets

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

这是我的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()时等待直到找到连接。

我想知道我做错了什么。

因评论而修改:

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

1 个答案:

答案 0 :(得分:1)

我的猜测是你试图在一个不允许用户运行程序的操作系统上监听特权端口(一个低于1024的端口)。 bind(),listen(),accept()等返回表示成功或失败的值 - 你不应该在不检查这些返回值的情况下调用它们。