gpt4 book ai didi

客户端/服务器交互

转载 作者:行者123 更新时间:2023-11-30 18:02:50 26 4
gpt4 key购买 nike

当我的程序来自 my previous question正在响应客户端,它应该发送加密消息。
当客户端连接时,它会发送一条类似“YourMessage”的消息。现在我想做的是,当客户端连接时,应该以某种方式对这条消息进行编码,并且其他内容会出现在客户端的屏幕上。我该怎么办?

服务器代码:

#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();
}

最佳答案

它应该像现在一样工作,但不是发送明文字符串,而是发送加密字符串。如果加密使得二进制数据 blob 也只需发送它,但客户端必须在将其显示给用户之前解密。

关于客户端/服务器交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8911306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com