2012-01-18 202 views
0

當我的程序my previous question正在響應客戶端時,它應該發送一條加密的消息。
當客戶端連接時,它發送一條消息,如「YourMessage」。現在我想要這樣做,當客戶端連接這個消息時應該以某種方式編碼,並在客戶端的屏幕上顯示其他內容。我該如何解決它?客戶端/服務器交互

Server代碼:

#include "wind.h" 

using namespace std; 

//begi 
void main() 
{ 

    long answer ; 
    WSAData wsaData; 
    WORD DLLVERSION; 
    DLLVERSION = MAKEWORD(2,1); 

    answer = WSAStartup(DLLVERSION, &wsaData); 

    SOCKADDR_IN addr; 
    int addrlen = sizeof(addr); 

SOCKET slisten; 
SOCKET sConnect; 

sConnect = socket(AF_INET,SOCK_STREAM,NULL); 

addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
addr.sin_family = AF_INET; 

addr.sin_port = htons(1234); 

slisten = socket(AF_INET,SOCK_STREAM,NULL); 

bind(slisten, (SOCKADDR*)&addr, sizeof(addr)); 
// unlimited clients 
listen(slisten, SOMAXCONN); 

//conditions 
for(;;) 



     { 
cout <<"waiting for client" <<endl; 
if (sConnect = accept(slisten,(SOCKADDR*)&addr, &addrlen)) 
     { 
      cout<<" A connection was found" <<endl; 

      answer = send(sConnect,  "YourMessage", 12 ,NULL); 
     char cMyCharacter = 'A'; 
     int iMyAsciiValue = static_cast<int>(cMyCharacter); 
     } 

} 
} 

客戶端代碼:

#include "clnt.h" 

using namespace std; 

void main() 
{ 
string confirm; 
char message[200]; 
    string strmessage; 

long answer; 
WSAData wsaData; 
WORD DLLVersion; 
DLLVersion = MAKEWORD(2,1); 
answer = WSAStartup(DLLVersion, &wsaData); 



SOCKADDR_IN addr; 
int addrlen = sizeof(addr); 

    SOCKET sconnect; 

    sconnect = socket(AF_INET,  SOCK_STREAM,NULL); 

    addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 

    addr.sin_family = AF_INET; 

    addr.sin_port = htons(1234); 


cout <<" Do you want to connect to your server?[Y/N]" <<endl; 
cin>> confirm; 
if(confirm == "N") 
{ 
     exit(1); 
    } 
     else 
    { 
     if(confirm == "Y") 
     { 
      connect(sconnect,  (SOCKADDR*)&addr, sizeof(addr)); 
      answer = recv(sconnect, message, sizeof(message), NULL); 
      strmessage = message; 
      cout << strmessage <<endl; 
      getchar(); 
     } 

    } 

getchar(); 
} 

回答

0

它應該工作一樣,但現在的它,而不是發送您發送的加密字符串的明文字符串。如果加密使得二進制數據blob也發送它,但是客戶端在向用戶顯示之前必須解密。