#define MAX_NUM_BUF 96
typedef struct _head
{
TCHAR type;//类型
unsigned short len;
}hdr, *phdr;
//数据包中的数据结构
typedef struct _data
{
TCHAR m_Name[MAX_NUM_BUF];
TCHAR m_Password[MAX_NUM_BUF];
}DATABUF, *pDataBuf;
<1>client端
#define Packet_Type_CleintInfo 'C'
#define Packet_Type_EXIT 'E'
#define HEADERLEN sizeof(hdr)
void CClientDlg::OnBnClickedBtnSendMsg() //要么这里出现了问题,要么是在服务端接收数据那里出现了问题
{
CString strName, strPassword;
GetDlgItemText(IDC_EDIT_NAME, strName);
GetDlgItemText(IDC_EDIT_PWD, strPassword);
if (strName.IsEmpty() || strPassword.IsEmpty())
{
AfxMessageBox(_T("is not empty!"));
return;
}
int NameLen = strName.GetLength()*sizeof(TCHAR);
int PasswordLen = strPassword.GetLength()*sizeof(TCHAR);
DATABUF CleintInfo;
ZeroMemory(&CleintInfo, sizeof(DATABUF));
memcpy_s(CleintInfo.m_Name, NameLen, strName.GetBuffer(), NameLen);
memcpy_s(CleintInfo.m_Password, PasswordLen, strPassword.GetBuffer(), PasswordLen);
hdr DataHeader;
//定义包头内容
DataHeader.type = Packet_Type_CleintInfo;
DataHeader.len = sizeof(CleintInfo); //这里只设置了包括了包体,不是设置的整个数据包的长度(即包头+包体)
TCHAR m_SendBuf[1024] = {0};
memcpy_s(m_SendBuf, HEADERLEN, &DataHeader, HEADERLEN);
memcpy_s(m_SendBuf + HEADERLEN, sizeof(CleintInfo), &CleintInfo, sizeof(CleintInfo));
m_ClientSocket->Send((char*)m_SendBuf, HEADERLEN + sizeof(CleintInfo), 0);
}
<2>server端
#define MAX_SIZE_BUF 1024
#define HEADERLEN sizeof(hdr)
void CSocketServerDlg::OnReceive() //要么这里出现了问题,要么是在客户端发送按钮函数里出现了问题
{
TCHAR szRecvBuf[MAX_SIZE_BUF] = {0};
int iRecvNum = m_ClientSocket->Receive((char*)szRecvBuf, MAX_SIZE_BUF, 0);
hdr DataHeader;
DATABUF CleintInfo;
if (iRecvNum > HEADERLEN)
{
ZeroMemory(&DataHeader, sizeof(DataHeader));
memcpy_s(&DataHeader, HEADERLEN, szRecvBuf, HEADERLEN);
if (DataHeader.type == 'C')
{
memcpy_s(&CleintInfo, sizeof(CleintInfo), szRecvBuf + HEADERLEN, sizeof(CleintInfo));
//ShowMsg(CleintInfo.m_Name);
//ShowMsg(CleintInfo.m_Password);
MessageBox(CleintInfo.m_Name);
MessageBox(CleintInfo.m_Password);
}
}
}
我是想把客户端的用户名和密码发送到服务端,但是服务端弹出的对话框都是空,就意味着收到的数据为空。。不知道哪里写错了
,求高人指点。谢谢!!!
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课