QT windows与linux之间sokcet通信中文乱码问题解决方法
linux发送与接收都转码utf-8:
tcpClient ->write( send_msg.toUtf8());//解决乱码,发送转码
接收:
QByteArray buffer = tcpClient->readAll();
if(!buffer.isEmpty())
{
// ui->plainTextEdit_Recv_Msg->appendPlainText(str_time+" "+QString::fromLocal8Bit(buffer));
ui->plainTextEdit_Recv_Msg->appendPlainText(str_time+" "+QString::fromUtf8(buffer)); //linux 读取数据,接收转码
}
windows发送与接收都转码utf-8:
linux接收:
void Dialog_debug::slot_readData()
{
QDateTime current_time=QDateTime::currentDateTime();
// QString str_time=current_time.toString("yyyy-MM-dd hh:mm:ss.zzz ddd");//ddd--周五,zzz毫秒
QString str_time=current_time.toString("yyyy-MM-dd hh:mm:ss");//ddd--周五,zzz毫秒
QByteArray buffer = tcpClient->readAll();
if(!buffer.isEmpty())
{
// ui->plainTextEdit_Recv_Msg->appendPlainText(str_time+" "+QString::fromLocal8Bit(buffer));
ui->plainTextEdit_Recv_Msg->appendPlainText(str_time+" "+QString::fromUtf8(buffer)); //linux 读取数据
}
}
windows发送:
void Server::on_pushButton_Send_clicked()
{
if(ui->listWidget_IP->selectedItems().length()>0)
{
foreach(QListWidgetItem* item, ui->listWidget_IP->selectedItems())
{
QString clientIP=item->text().split(":")[0];
int clientPort=item->text().split(":")[1].toInt();
for(int i=0;i<tcpClients.length();i++)
{
if(tcpClients[i]->peerAddress().toString()==clientIP &&tcpClients[i]->peerPort()==clientPort)
{
tcpClients[i]->write(ui->plainTextEdit_Send_Msg->toPlainText().toUtf8());//windows与linux之间数据传送
// tcpClients[i]->write(ui->plainTextEdit_Send_Msg->toPlainText().toLocal8Bit());fromLocal8Bit显示中文,解决乱码,windows之间
ui->plainTextEdit_Recv_Msg->appendPlainText(GetCurrentTimeStr()+" 发送:"+ui->plainTextEdit_Send_Msg->toPlainText());
}
}
}
}
else
{
QMessageBox::information(this,"提示","IP列表未选择");
}
}