(WinSocket)断开连接时的垃圾邮件

时间:2013-02-21 19:32:31

标签: c++ sockets chat

我正在尝试为客户端/服务器聊天程序编写服务器。 唯一的问题是,如果客户端断开连接,服务器就会开始向服务器发送垃圾邮件。即使他们重新连接,它仍然发送垃圾邮件..我试过谷歌搜索,但我找不到任何东西。 这是服务器代码:

#include "MasterServer.h"
using namespace std;
SOCKADDR_IN Server;
int addrlen = sizeof(Server);
SOCKET sListen;
SOCKET Connect; 
SOCKET* Connections;
int port = 444;
int ConCounter = 0;
char* Recv = new char[256];
int ServerThread(int ID)
{

ZeroMemory(Recv, sizeof(Recv));
for(;;)
{
    if(recv(Connections[ID],Recv ,256 ,NULL))
    {
        cout << Recv << endl;
    }
}
}

int InitWinSock()
{
int RetVal = 0;
      WSAData wsa;
      WORD Version = MAKEWORD(2,1);
      RetVal = WSAStartup(Version, &wsa);

      return RetVal;
}


int main()
{
    int RetVal = 0;
    RetVal = InitWinSock();
    if(RetVal != 0)
    {
        cout << "WinSock Start Up Failure";
    }
      Connections = (SOCKET*)calloc(64, sizeof(SOCKET));

       sListen = socket(AF_INET, SOCK_STREAM, NULL);
       Connect = socket(AF_INET, SOCK_STREAM, NULL);
       cout << "\t\t----====ModernBacon Server====----"<< endl;

        cout << "What Port Would You Like To Run On [Default 444]: ";
        cin >> port;

        system("cls");
      Server.sin_addr.s_addr = inet_addr("127.0.0.1");
      Server.sin_family = AF_INET;
      Server.sin_port = htons(port);

      bind(sListen, (SOCKADDR*)&Server, sizeof(Server));
      //Listening
      listen(sListen, 64);
cout << "Listening For Connections IP:"<<inet_ntoa(Server.sin_addr);"Port:"<<port<< 
endl;


      for(;;)
          {
            if(Connect = accept(sListen, (SOCKADDR*)&Server, &addrlen))
            {
                Connections[ConCounter] = Connect;
                char* ID = new char[64];
                ZeroMemory(ID, sizeof(ID));
                itoa(ConCounter, ID, 10);
                send(Connections[ConCounter], ID, sizeof(ID),NULL);

                ConCounter = ConCounter + 1;
                CreateThread(NULL,NULL, (LPTHREAD_START_ROUTINE) 
ServerThread, (LPVOID)(ConCounter -1), NULL, NULL);

                cout << "\t\tClient Connected!\tID: " <<
ConCounter << endl;



            }

      }
      return(0);
}

我不知道最近会发生什么......

1 个答案:

答案 0 :(得分:0)

您的代码存在很多问题。尝试更像这样的东西:

#include "MasterServer.h"
#include <string>

using namespace std;

static const int MAX_CLIENTS = 64;
static const int MAX_RECV = 256;

struct ClientInfo
{
    int ID;
    SOCKET Connection;
    char Recv[MAX_RECV];
};

ClientInfo Clients[MAX_CLIENTS];

DWORD WINAPI ServerThread(LPVOID lpParam)
{
    ClientInfo *Client = (ClientInfo*) lpParam;
    char szID[64] = {0};

    itoa(Client->ID, szID, 10);

    int RetVal = send(Client->Connection, szID, sizeof(szID), NULL);
    if (RetVal != sizeof(szID))
    {
        cout << "Client " << Client->ID << " Send Failure. Error " << WSAGetLastError() << endl;
    }
    else
    {
        do
        {
            RetVal = recv(Client->Connection, Client->Recv, MAX_RECV, NULL);
            if (RetVal == SOCKET_ERROR)
            {
                cout << "Client " << Client->ID << " Recv Failure. Error " << WSAGetLastError() << endl;
                break;
            }

            if (RetVal == 0)
            {
                cout << "Client " << Client->ID << " disconnected" << endl;
                break;
            }

            cout << "Client " << Client->ID << ": " << string(Client->Recv, RetVal) << endl;
        }
        while (true);
    }

    closesocket(Client->Connection);
    Client->Connection = INVALID_SOCKET;

    return 0;
}

int InitWinSock()
{
    WSAData wsa;
    return WSAStartup(MAKEWORD(2,1), &wsa);
}

int main()
{
    SOCKADDR_IN addr;
    int addrlen;
    SOCKET sListen;
    SOCKET sClient;
    int port = 444;
    int index;

    cout << "\t\t----====ModernBacon Server====----"<< endl;

    for (int i = 0; i < MAX_CLIENTS; ++i)
        Connections[i] = INVALID_SOCKET;

    int RetVal = InitWinSock();
    if (RetVal != 0)
    {
        cout << "WinSock Start Up Failure. Error " << RetVal << endl;
        return 0;
    }

    sListen = socket(AF_INET, SOCK_STREAM, NULL);
    if (sListen == INVALID_SOCKET)
    {
        cout << "WinSock Socket Failure. Error " << WSAGetLastError() << endl;
        return 0;
    }

    cout << "What Port Would You Like To Run On [Default 444]: ";
    cin >> port;
    system("cls");

    ZeroMemory(&addr, sizeof(addr));
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);

    RetVal = bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
    if (RetVal != 0)
    {
        cout << "WinSock Bind Failure. Error " << WSAGetLastError() << endl;
        return 0;
    }

    //Listening
    RetVal = listen(sListen, MAX_CLIENTS);
    if (RetVal != 0)
    {
        cout << "WinSock Listen Failure. Error " << WSAGetLastError() << endl;
        return 0;
    }

    cout << "Listening For Connections. IP: " << inet_ntoa(addr.sin_addr) << ", Port: " << port << endl;

    do
    {
        addrlen = sizeof(addr);
        sClient = accept(sListen, (SOCKADDR*)&addr, &addrlen);
        if (sClient == INVALID_SOCKET)
        {
            cout << "WinSock Accept Failure. Error " << WSAGetLastError() << endl;
            return 0;
        }

        index = -1;
        for (int i = 0; i < MAX_CLIENTS; ++i)
        {
            if (Clients[i].Connection == INVALID_SOCKET)
            {
                 index = i;
                 break;
            }
        }

        if (index == -1)
        {
            closesocket(sClient);
            cout << "\t\tClient Connected. Max Clients Exceeded!" << endl;
            continue;
        }

        Clients[index].ID = index + 1;
        Clients[index].Connection = sClient;

        CreateThread(NULL, NULL, &ServerThread, &Clients[index], NULL, NULL);

        cout << "\t\tClient Connected!\tID: " << Clients[index].ID << endl;
    }
    while (true);

    return 0;
}