void CCHATDlg::OnBnClickedSendButton()
{
CString str,sIP;
GetDlgItemText(IDC_SEND_EDIT, str);
GetDlgItemText(IDC_IP_EDIT, sIP);
m_sock.SendTo(str, str.GetLength(),PORT, sIP);
}
void CCHATDlg::Onreceive(void)
{
CString sIP;
char s[256];
UINT nPort;
int nRetun;
nRetun = m_sock.ReceiveFrom(s, sizeof(s), sIP, nPort);
s[nRetun] = 0;
COleDateTime time = COleDateTime::GetCurrentTime();
CString str = sIP + "(" + time.Format(_T("%H:%M:%S")) + "):\r\n" + s + "\r\n";
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_RECV_EDIT);
pEdit->SetSel(pEdit->GetWindowTextLength(), -1);
pEdit->ReplaceSel(str);
//SetDlgItemText(IDC_RECV_EDIT, str);
}
找到原因了,我用的VS2010默認字符集是Unicode,但接收時用的char類型,默認是MBCS字符集。
只要在發送時將str通過WideCharToMultiByte函數轉化為MBCS編碼方式就行了,感興趣的,可以上網查查