能力值:
( LV3,RANK:20 )
|
-
-
2 楼
c++ 客户端
//根据DataLen接收指定长度在返回
BOOL mRecv(SOCKET Socket, BYTE *RecvBuf, int DataLen)
{
int nRetVal = 0, TotalBytesRecv = 0;
while (TotalBytesRecv < DataLen)
{
nRetVal = recv(Socket, (char*)(RecvBuf + TotalBytesRecv), DataLen - TotalBytesRecv, 0);
if (nRetVal <= 0)
return TRUE;
else
TotalBytesRecv += nRetVal;
}
return FALSE;
}
//解密服务端发送的内容
VOID jiemi(PBYTE p, int len, string key)
{
for (int i = 0; i < len; i++)
{
for (string::iterator ite = key.begin(); ite != key.end(); ite++)
{
p[i]=p[i]^(*ite)^'1';
}
}
}
参数 sret:返回服务端发送的解密完成的数据
user:用户
rjbh:验证
jb:向服务器请求的文本
key:秘钥,发送到服务器,服务器根据秘钥加密数据
int get_js(string &sret, string user="000", string rjbh="000", string jb = "1.txt",string key="555")
{
const int BUF_SIZE = 1024;
WSADATA wsd; //WSADATA变量
SOCKET sHost; //服务器套接字
SOCKADDR_IN servAddr; //服务器地址
char buf[BUF_SIZE]; //接收数据缓冲区
char bufRecv[BUF_SIZE];
int retVal; //返回值
//初始化套结字动态库
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
cout << "WSAStartup failed!" << endl;
return -1;
}
//创建套接字
sHost = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == sHost)
{
cout << "socket failed!" << endl;
WSACleanup();//释放套接字资源
return -1;
}
//设置服务器地址
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servAddr.sin_port = htons((short)9090);
int nServAddlen = sizeof(servAddr);
//连接服务器
retVal = connect(sHost, (LPSOCKADDR)&servAddr, sizeof(servAddr));
if (SOCKET_ERROR == retVal)
{
cout << "connect failed!" << endl;
closesocket(sHost); //关闭套接字
WSACleanup(); //释放套接字资源
return -1;
}
//向服务器发送数据
ZeroMemory(buf, BUF_SIZE);
ZeroMemory(bufRecv, BUF_SIZE);
sprintf(buf, "{\"user\":\"%s\",\"rjbh\":\"%s\",\"jb\":\"%s\",\"key\":\"%s\"}", user.c_str(), rjbh.c_str(), jb.c_str(),key.c_str());
BASE64_Encode((PBYTE)buf, strlen(buf), bufRecv);
retVal = send(sHost, bufRecv, strlen(bufRecv), 0);
if (SOCKET_ERROR == retVal)
{
cout << "send failed!" << endl;
closesocket(sHost); //关闭套接字
WSACleanup(); //释放套接字资源
return -1;
}
ZeroMemory(bufRecv, BUF_SIZE);
recv(sHost, bufRecv, BUF_SIZE, 0); // 接收服务器端的数据
int str_size = atoi(bufRecv);
retVal = send(sHost, bufRecv, strlen(bufRecv), 0);//发送文件长度
if (SOCKET_ERROR == retVal)
{
cout << "send failed!" << endl;
closesocket(sHost); //关闭套接字
WSACleanup(); //释放套接字资源
return -1;
}
PBYTE data_buf_base64 = new BYTE[str_size + 1];
RtlZeroMemory(data_buf_base64, str_size + 1);
mRecv(sHost, data_buf_base64, str_size);
int ilen = strlen((char*)data_buf_base64);
if (ilen == str_size)
{
jiemi(data_buf_base64, ilen, key);
}
sret =(char*)data_buf_base64;
delete[]data_buf_base64;
//退出
closesocket(sHost); //关闭套接字
WSACleanup(); //释放套接字资源
return 1;
}
最后于 2018-5-5 12:10
被进口坚果编辑
,原因:
|