2013-06-13 73 views
0

我有一個客戶端應用程序,需要從任何地方連接到服務器,所以要做到這一點我想創建一個主服務器。這個主服務器將其他服務器的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); 

現在客戶只需連接到位於本地計算機和它的服務器只顯示一個地址(我發送的地址)。我如何獲得保存在向量中的地址並顯示它?

回答

0

遍歷服務器端的向量併發送數據(每個字符串的原始字節)。用雙'0'來終止它,以便知道你在客戶端接收時已達到結尾。 我真的懷疑你有太多的數據,但如果你有,在發送之前壓縮它,並在另一側解壓。

+0

我將如何發送/接收數據?只有當客戶端應用程序連接到服務器時才需要發送數據 – user2478375

+0

send()/ recv()?也許與select()結合使用,如果你有非阻塞套接字等等......當新客戶端連接時,向他發送向量內容。而已。 – user1764961

+0

發送代碼是什麼樣的?我試過vClientIPs =發送(ListeningSocket,recvbuff,(int)strlen(recvbuff),0);但它不起作用 – user2478375

相關問題