CString char2CString(char* ch)
{
int charLen = strlen(ch);
//计算多字节字符的大小,按字符计算
int len = MultiByteToWideChar(CP_ACP, 0, ch, charLen, NULL, 0);
//为宽字节字符数组申请空间,数组大小为按字节计算的多字节字符大小
TCHAR* buf = new TCHAR(len + 1);
//多字节编码转换成宽字节编码
MultiByteToWideChar(CP_ACP, 0, ch, charLen, buf, len);
buf[len] = '\0'; //添加字符串结尾,注意不是len+1
//将TCHAR数组转换为CString
CString strTemp;
strTemp.Append(buf);
//删除缓冲区
delete buf;
return strTemp;
}
从Socket接收到的字符串,先经过这个函数转为CString再进行处理。
//宽字节转多字节
void CStringToChar(CString strSrc, char* pDest, int size)
{
memset(pDest, 0, size);
int nLength = strSrc.GetLength();
int nBytes = WideCharToMultiByte(CP_ACP, 0, strSrc, nLength, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_OEMCP, 0, strSrc, nLength, pDest, nBytes, NULL, NULL);
pDest[nBytes] = 0;
}
CString先经过这个函数,转为Char *,然后再转由Socket去传输。
void CMFCTestCPPDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
//启动Windows Socket 2.x环境
WORD ver = MAKEWORD(2,2);
WSADATA dat;
WSAStartup(ver,&dat);
//1.建立Socket套接字
SOCKET _sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//2.Bind 绑定用于接受客户端连接的网络端口号
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567); //host to net unsigned short
_sin.sin_addr.S_un.S_addr = INADDR_ANY; //inet_addr("127.0.0.1")
if (SOCKET_ERROR == bind(_sock, (sockaddr*)&_sin, sizeof(_sin)))
{
MessageBox(_T("绑定用于接受客户端连接的网络端口失败。"));
}
else
{
MessageBox(_T("绑定网络端口成功。"));
}
//3 listen 监听网络端口
if (SOCKET_ERROR == listen(_sock, 5)) //默认最多接收5个客户端的连接
{
MessageBox(_T("监听失败"));
}
else
{
MessageBox(_T("监听成功"));
}
//4 Accept 等待接收客户端连接
sockaddr_in clientAddr = {};
int nAddrLen = sizeof(sockaddr_in);
SOCKET _cSock = INVALID_SOCKET;
_cSock = accept(_sock,(sockaddr *)&clientAddr, &nAddrLen);
char msgBuf[] = "Hello,I'm Server.";
CString msgBufStr = _T("Hello,I'm Server.");
char _recvBuf[128] = {};
if (INVALID_SOCKET == _cSock)
{
MessageBox(_T("错误,接收到无效客户端SOCKET."));
}
//5 接收客户端数据
int nLen = recv(_sock, _recvBuf,sizeof(_recvBuf),0);
if (nLen <= 0)
{
MessageBox(_T("客户端已退出。"));
}
//6 处理来自客户端的请求
if (0== strcmp(_recvBuf,"getName"))
{
msgBufStr= _T("Xiao Qiang");
CStringToChar(msgBufStr,msgBuf,msgBufStr.GetLength());
send(_cSock, msgBuf, strlen(msgBuf)+1,0);
}
//7 send 向客户端发送一条数据
//void CStringToChar(CString strSrc, char* pDest, int size)
CStringToChar(msgBufStr,msgBuf,msgBufStr.GetLength());
MessageBox(msgBufStr);
send(_cSock, msgBuf, strlen(msgBuf) + 1, 0);
//CDialogEx::OnOK();
}
以上是服务器端部分代码
void CMFCServerCPPDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
// TODO: 在此添加控件通知处理程序代码
//启动Windows socket 2.x环境
WORD ver = MAKEWORD(2, 2);
WSADATA dat;
WSAStartup(ver, &dat);
//1 建立一个socket
SOCKET _sock = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == _sock)
{
MessageBox(_T("错误:建立连接套接字失败。"));
}
else
{
MessageBox(_T("建立连接套接字成功。"));
}
//2 连接服务器 connect
sockaddr_in _sin = {};
_sin.sin_family = AF_INET;
_sin.sin_port = htons(4567); //服务器端的端口号为:4567
_sin.sin_addr.S_un.S_addr = inet_addr("192.168.0.111"); //服务器的IP地址:192.168.0.111
int ret = connect(_sock, (sockaddr*)&_sin, sizeof(sockaddr_in));
if (SOCKET_ERROR == ret)
{
MessageBox(_T("错误:连接服务器失败。"));
}
else
{
MessageBox(_T("连接服务器成功。"));
}
//MessageBox(_T("可能的故障点1"));
// 3接收服务器信息recv
char recvBuf[256] = {};
int nlen = recv(_sock, recvBuf, sizeof(recvBuf), 0);
if (nlen > 0)
{
//有数据从服务器传来;
CString cStr2;
CharToCString(recvBuf, cStr2);
//MessageBox(cStr2);
CString cStr = _T("接收到数据:")+ cStr2 ;
MessageBox(cStr);
//MessageBox(_T("可能的故障点2"));
}
//4关闭套接字 closesocket
closesocket(_sock);
WSACleanup();
MessageBox(_T("结束"));
//CDialogEx::OnOK();
}
以上是客户端的部分代码