安卓socket客户端接收PC服务器发送来的汉字变问号
这是接收的代码try{//连接服务器mSocketClient=newSocket(sIP,port);//portnum//取得输入、输出流mBufferedReade...
这是接收的代码
try
{
//连接服务器
mSocketClient = new Socket(sIP, port); //portnum
//取得输入、输出流
mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);
recvMessageClient = "已经连接server!\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
//break;
}
catch (Exception e)
{
recvMessageClient = "连接IP异常:" + e.toString() + e.getMessage() + "\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
return;
}
char[] buffer = new char[256];
int count = 0;
while (isConnecting)
{
try
{
//if ( (recvMessageClient = mBufferedReaderClient.readLine()) != null )
if((count = mBufferedReaderClient.read(buffer))>0)
{
recvMessageClient = getInfoBuff(buffer, count) + "\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
catch (Exception e)
{
recvMessageClient = "接收异常:" + e.getMessage() + "\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
这是getInfoBuff函数
private String getInfoBuff(char[] buff, int count)
{
char[] temp = new char[count];
for(int i=0; i<count; i++)
{
temp[i] = buff[i];
}
return new String(temp);
} 展开
try
{
//连接服务器
mSocketClient = new Socket(sIP, port); //portnum
//取得输入、输出流
mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);
recvMessageClient = "已经连接server!\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
//break;
}
catch (Exception e)
{
recvMessageClient = "连接IP异常:" + e.toString() + e.getMessage() + "\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
return;
}
char[] buffer = new char[256];
int count = 0;
while (isConnecting)
{
try
{
//if ( (recvMessageClient = mBufferedReaderClient.readLine()) != null )
if((count = mBufferedReaderClient.read(buffer))>0)
{
recvMessageClient = getInfoBuff(buffer, count) + "\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
catch (Exception e)
{
recvMessageClient = "接收异常:" + e.getMessage() + "\n";//消息换行
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
这是getInfoBuff函数
private String getInfoBuff(char[] buff, int count)
{
char[] temp = new char[count];
for(int i=0; i<count; i++)
{
temp[i] = buff[i];
}
return new String(temp);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询