大家好,又见面了,我是你们的朋友全栈君。
MFC Java 使用socket 中文乱码解决方法(一)
1、在使用MFC做服务端,Java做客户端时
(1)客户端向服务端发送数据
Java发送代码:
PrintWriter printWriter =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
client.getOutputStream(), "gbk")),true);
//Windows一般默认为GBK编码,故转换为gbk编码,也可以使用gb18030等编码
//Unicode会出现乱码!!!!!!!!!!!!!
printWriter.print(msg);
printWriter.flush();
//printWriter.close();
MFC接收代码
char strRecive[512];//数组大小可以是发送数据的类型自由改变
ZeroMemory(strRecive, sizeof(strRecive));//清空字符串数组,避免产生乱码
int num = Receive(strRecive, sizeof(strRecive));//接收客户端发送的数据
CString tem = (CString)strRecive;//转换为CString
(2)服务端向客户端发送数据
MFC发送代码
//将数据转换为utf-8形式
int strSize = WideCharToMultiByte(CP_UTF8, 0, SendMsg, -1, NULL, 0, NULL, FALSE);//获取待发送数据的长度
char *str = new char[strSize + 1];//数组长度要比数据长度大1,用于存放'//将数据转换为utf-8形式
int strSize = WideCharToMultiByte(CP_UTF8, 0, SendMsg, -1, NULL, 0, NULL, FALSE);//获取待发送数据的长度
char *str = new char[strSize + 1];//数组长度要比数据长度大1,用于存放'\0'
memset(str, 0, strSize + 1);
WideCharToMultiByte(CP_UTF8, 0, SendMsg, -1, str, strSize, NULL, FALSE);
str[strSize] = '\0';
//发送数据
if (SOCKET_ERROR == sendSocket->Send(str, strlen(str), 0))
{
CString str;
str.Format(L"发送失败,错误代号:%d", GetLastError());
MessageBox(str);
return;
}
else
{
sendSocket->Close();
}
int len = SendMsg
'
memset(str, 0, strSize + 1);
WideCharToMultiByte(CP_UTF8, 0, SendMsg, -1, str, strSize, NULL, FALSE);
str[strSize] = '//将数据转换为utf-8形式
int strSize = WideCharToMultiByte(CP_UTF8, 0, SendMsg, -1, NULL, 0, NULL, FALSE);//获取待发送数据的长度
char *str = new char[strSize + 1];//数组长度要比数据长度大1,用于存放'\0'
memset(str, 0, strSize + 1);
WideCharToMultiByte(CP_UTF8, 0, SendMsg, -1, str, strSize, NULL, FALSE);
str[strSize] = '\0';
//发送数据
if (SOCKET_ERROR == sendSocket->Send(str, strlen(str), 0))
{
CString str;
str.Format(L"发送失败,错误代号:%d", GetLastError());
MessageBox(str);
return;
}
else
{
sendSocket->Close();
}
int len = SendMsg
';
//发送数据
if (SOCKET_ERROR == sendSocket->Send(str, strlen(str), 0))
{
CString str;
str.Format(L"发送失败,错误代号:%d", GetLastError());
MessageBox(str);
return;
}
else
{
sendSocket->Close();
}
int len = SendMsg
Java接收代码
//有多种方法可用于接收数据
reader=new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
/* BufferedInputStream in = new BufferedInputStream(client.getInputStream()); byte[] bytes = new byte[1024]; int len; String sb=null; while ((len = in.read(bytes)) != -1) { //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8 sb=new String(bytes,"UTF-8"); } System.out.println("get message from client: " + sb); in.close();*/
/* Reader reader =new InputStreamReader(client.getInputStream()); char chars[]=new char[121231]; int len; StringBuilder sb =new StringBuilder(); while((len=reader.read(chars)) != -1) { sb.append(new String(chars,0, len)); } System.out.println("from client: "+ sb);*/
总结
解决好编码转换问题就ok了
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/159149.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...