18,356
社区成员
发帖
与我相关
我的任务
分享
CString str, str1;
GetDlgItem( IDC_SENDTEXT ) -> GetWindowText(str);
char* pBuff = W2A(str);
if( ::send( s, (const char *)((LPCTSTR)str), (str.GetLength() + 1) * sizeof(TCHAR), 0 ) != SOCKET_ERROR )
[/quote]
你服务器接收的时候不要再用A2W转码啊。保持编码,你的转换是多此一举。void HexDump(char *buf,int len,int addr) {
int i,j,k;
char binstr[80];
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%08x -",i+addr);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
}
}
void CopyCStringTToChar( char* pchDest, const CString& strSrc, int nDestLen )
{
if ( pchDest == NULL )
{
return;
}
#ifdef _UNICODE // 使用宽字节
{
WCHAR* pWStrSRc = const_cast<CString&>(strSrc).GetBuffer(0);
int nTmplen = WideCharToMultiByte( CP_ACP, 0, pWStrSRc, -1, NULL, 0, NULL, NULL );
char* pTemp = new char[nTmplen+1];
memset( pTemp, 0, nTmplen+1 );
WideCharToMultiByte( CP_ACP, 0, pWStrSRc, -1, pTemp, nTmplen+1, NULL, NULL );
int nLen = strlen(pTemp);
if( nLen + 1 > nDestLen )
{
strncpy( pchDest, pTemp, nDestLen-1 );
pchDest[nDestLen-1] = 0;
}
else
{
strcpy( pchDest, pTemp );
}
delete []pTemp;
}
#else // 使用窄字节
{
int nSrcLen = strSrc.GetLength();
if( nSrcLen + 1 > nDestLen )
{
strncpy( pchDest, strSrc, nDestLen-1 );
pchDest[nDestLen-1] = 0;
}
else
{
strcpy( pchDest, strSrc );
}
}
#endif
}
size_t len = str.GetLength();
if( ::send( s, pBuff, len, 0 ) != SOCKET_ERROR )
然后在服务器端这样接收:
LRESULT CTCPClientDlg::OnSocket(WPARAM wParam, LPARAM lParam)
{
USES_CONVERSION;
// TODO: Add your control notification handler code here
char cs[100] = {0};
if( lParam == FD_READ )
{
CString num = TEXT("");
recv( s, cs, 100, NULL );
CString recvText = A2W(cs);
GetDlgItem( IDC_TEXT ) -> GetWindowText( num );
num += TEXT("服务器说:\r\n");
num += recvText;
GetDlgItem( IDC_TEXT ) -> SetWindowText( num );
}
return 0;
}
英文字符是可以的,汉字就显示问号.
CString str, str1;
GetDlgItem( IDC_SENDTEXT ) -> GetWindowText(str);
//char* pBuff = W2A(str); //这个多余的不用转换
size_t len =( str.GetLength()+1)*sizeof(TCHAR);
if( ::send( s, pBuff, len, 0 ) != SOCKET_ERROR )
LRESULT CTCPClientDlg::OnSocket(WPARAM wParam, LPARAM lParam)
{
USES_CONVERSION;
// TODO: Add your control notification handler code here
WCHAR cs[100] = {0};
if( lParam == FD_READ )
{
CString num = TEXT("");
recv( s, (char*)cs, 100, NULL );
//CString recvText = A2W(cs); 也不用转换
CString recvText = cs;
GetDlgItem( IDC_TEXT ) -> GetWindowText( num );
num += TEXT("服务器说:\r\n");
num += recvText;
GetDlgItem( IDC_TEXT ) -> SetWindowText( num );
}
return 0;
}
if( ::send( s, (const char *)((LPCTSTR)str), (str.GetLength() + 1) * sizeof(TCHAR), 0 ) != SOCKET_ERROR )
size_t len = str.GetLength()*sizeof(TCHAR);
if( ::send( s, pBuff, len, 0 ) != SOCKET_ERROR )