如何基于TCP/IP协议进行MFC Socket网络通讯编程( 二 )


////////////接收数据
while (1)
{
if ((res = recv(sock, msg, 1024, 0)) == -1)
{
dlg->update(_T("失去客户端的连接"));
break;
}
else
{
msg[res] = '';
dlg->update(_T("client:") + CString(msg));
}
}
return 0;
}
6、添加按钮发送函数(在对话框中右键点击刚添加的按钮弹出菜单,然后选择添加事件响应函数这一栏,将函数命名为OnSending)
事件响应代码如下:
void CsFileDlg::OnSending()
{
// TODO: Add your control notification handler code here
CString s;
char msg[1024];
send_edit->GetWindowTextW(s);
CString2Char(s, msg); // 注意!这里调用了字符格式转换函数,此函数功能:CString类型转换为Char类型,实现代码后面添加
if (send(sock, msg, strlen(msg), 0) == SOCKET_ERROR)
{
show_edit->ReplaceSel(_T("发送失败"));
m_listwords.SetWindowTextW(_T("发送失败"));
}
else if (s == "")
{
MessageBox(_T("请输入信息"));
}
else
{
s = msg;
//update(s);//消息上屏,清空输入,并重获焦点
//show_edit->ReplaceSel(_T("server:") + s);//消息上屏,清空输入,并重获焦点
m_listwords.AddString(_T("server:") + s);
send_edit->SetWindowText(_T(""));
m_listwords.SetFocus();
}
}
void CString2Char(CString str, char ch[])//此函数就是字符转换函数的实现代码
{
int i;
char *tmpch;
int wLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);//得到Char的长度
tmpch = new char[wLen + 1]; //分配变量的地址大小
WideCharToMultiByte(CP_ACP, 0, str, -1, tmpch, wLen, NULL, NULL); //将CString转换成char*
for (i = 0; tmpch[i] != ''; i++) ch[i] = tmpch[i];
ch[i] = '';
}
客户端具体步骤如下:
1、 在cFileDlg.h中添加 public:void update(CString s);
private: CEdit* send_edit;
void CString2Char(CString str, char ch[]);
2、 新建一个socket套接字: SOCKET sock;
声明线程函数 UINT server_thd(LPVOID p);
3、在OnInitDialog()函数中添加:
send_edit = (CEdit *)GetDlgItem(IDC_EDIT1);
4、添加函数update():
void CcFileDlg::update(CString s)
{
m_listwords.AddString(s);
}
5、添加线程函数server_thd():
UINT recv_thd(LPVOID p)
{
int res;
char msg[1024];
//CString s;
CcFileDlg * dlg = (CcFileDlg *)AfxGetApp()->GetMainWnd();
////////////接收数据
while (1)
{
if ((res = recv(sock, msg, 1024, 0)) == -1)//接收服务器的数据
{
dlg->update(_T("失去连接"));
break;
}
else
{
msg[res] = '';
dlg->update(_T("server:") + CString(msg));
}
}
//closesocket(sock);
return 0;
}
6、添加连接按钮事件响应函数:在连接按钮上右键选择添加事件响应这一栏,将函数取名OnConnecting,如下
void CcFileDlg::OnConnecting()
{
// TODO: Add your control notification handler code here
WSADATA wsaData;
SOCKADDR_IN server_addr;
memset(&server_addr, 0, sizeof(server_addr));
WORD wVersion;
wVersion = MAKEWORD(2, 2);
WSAStartup(wVersion, &wsaData);
// WSAStartup(0x0202, &wsaData);
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
update(_T("create socket error !!!"));
}
//CString ip;
//ip_edit->GetWindowTextW(ip);//取得服务器的IP地址
//server_addr.sin_addr.s_addr = inet_addr((LPSTR)(LPCSTR)ip.GetBuffer());
BYTE nArrIP[4];
m_ip.GetAddress(nArrIP[0], nArrIP[1], nArrIP[2], nArrIP[3]);
CString str;
str.Format(_T("%d.%d.%d.%d"), nArrIP[0], nArrIP[1], nArrIP[2], nArrIP[3]);
ip_edit->SetWindowTextW(str);
char cp[50];
CString2Char(str, cp);
server_addr.sin_addr.s_addr = inet_addr(cp);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8888);
bind(sock, (SOCKADDR*)&server_addr, sizeof(SOCKADDR));
if (connect(sock, (struct sockaddr *) &server_addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
update(_T("连接失败"));
}
else
{
//show_edit->SetWindowText(_T(""));
update(_T("连接成功"));
btnconn->EnableWindow(FALSE);//按钮变灰
AfxBeginThread(recv_thd, NULL);
}
}
7、添加发送按钮事件响应函数:在发送按钮上右键选择添加事件响应这一栏,将函数取名OnSending,如下


推荐阅读