客户端如何从服务器检索矢量列表?

时间:2013-06-13 10:03:36

标签: c++ visual-studio tcp client-server

我有一个客户端应用程序需要从任何地方连接到服务器所以要做到这一点我正在尝试创建一个主服务器。此主服务器将其他服务器的IP地址保存在向量中。然后,客户端将获取此服务器列表,并选择一个要连接的服务器。所以基本上主服务器只需要将服务器地址传递给客户端

我需要我的客户端应用程序从主服务器访问此向量列表并在列表框中显示地址

那么客户端如何从主服务器访问向量列表?

这是来自主服务器的代码:

WSADATA wsaData; 
SOCKET ListeningSocket, NewConnection; 
SOCKADDR_IN ServerAddr, SenderInfo;  quantity
int Port = 7171;
char recvbuff[1024];
int ByteReceived, i, nlen;

ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

ServerAddr.sin_family = AF_INET; 
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

std::vector<std::string> vClientIPs; // client ip string vector
while(1)
{
    NewConnection = SOCKET_ERROR;
    while(NewConnection == SOCKET_ERROR)
    {
        NewConnection = accept(ListeningSocket, NULL, NULL);
        printf("Server: New client got connected, ready to receive and send data...\n\n");
        ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);

        if ( ByteReceived > 0 )
        {
            getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
            printf("Server: IP(s) used by Server: %s\n", inet_ntoa(ServerAddr.sin_addr)); 
            vClientIPs.push_back(std::string(inet_ntoa(ServerAddr.sin_addr))); //insert client ip
            printf("Server: port used by Server: %d\n\n", htons(ServerAddr.sin_port));
            memset(&SenderInfo, 0, sizeof(SenderInfo));
            nlen = sizeof(SenderInfo);
            getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
            printf("Server: IP used by Client: %s\n", inet_ntoa(SenderInfo.sin_addr));
            printf("Server: Port used by Client: %d\n", htons(SenderInfo.sin_port));
            printf("Server: Bytes received: %d\n", ByteReceived);
            printf("Server: Message from client: \"");

            for(i=0;i < ByteReceived;i++)
            {
                printf("%c", recvbuff[i]);
            }
            printf("\"");
            }
            else if ( ByteReceived == 0 )
            {
                printf("Server: Connection closed!\n");
            }
            else
            {
                printf("Server: recv failed with error code: %d\n", WSAGetLastError());
            }
        }
    }
}

这是客户端代码:

WSADATA wsaData;
SOCKET SendingSocket;
SOCKADDR_IN ServerAddr, ThisSenderInfo;
unsigned int Port = 7171;
int RetCode;

char sendbuf[1024] = "This is a test string from client";
int BytesSent, nlen;

WSAStartup(MAKEWORD(2,2), &wsaData);
SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 

ClientInfo info;
info.addr = inet_ntoa(ServerAddr.sin_addr); //push address onto listbox

RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
getsockname(SendingSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));

BytesSent = send(SendingSocket, sendbuf, strlen(sendbuf), 0);

if(BytesSent == SOCKET_ERROR)
{
    AfxMessageBox("Send error %ld.\n", WSAGetLastError());
}
else
{
    memset(&ThisSenderInfo, 0, sizeof(ThisSenderInfo));
    nlen = sizeof(ThisSenderInfo);

    getsockname(SendingSocket, (SOCKADDR *)&ThisSenderInfo, &nlen);         

}

Mutex<CriticalSection>::Lock lock(client_cs);
clients.push_back(info);

现在客户端只连接到本地计算机上的服务器,它只显示一个地址(我发送给它的那个)。如何才能将地址​​保存在矢量中并显示呢?

1 个答案:

答案 0 :(得分:0)

遍历服务器端的向量并发送数据(每个字符串的原始字节)。使用双“0”终止它,以便知道您在客户端接收时已到达终点。 我真的怀疑你有太多的数据,但如果你有,请在发送之前压缩它,然后在另一端解压缩。

相关问题