我正在尝试将一个非常简单的Java套接字客户端连接到QTcpServer。但是,当它连接时,我无法发送和发送消息。我也尝试将java客户端连接到java服务器(运行良好),将Qt服务器连接到qt客户端(也很好用)。但是以某种方式混合它们不会......
这是Java客户端:
public class SocketClient {
public static void main(String[] args) throws IOException {
String hostName = "10.57.140.129";
int portNumber = 2010;
try (
Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));
) {
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Test recieved. KKTHXBYE"))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
使用相关的QTserver代码位:
#include "socketserver.h"
SocketServer::SocketServer(QObject *parent) :
QTcpServer(parent) {
}
void SocketServer::StartServer(){
if(!this->listen(QHostAddress::Any, 2010)){
qDebug() << "Could not start server";
}else{
qDebug() << "Listening...";
}
}
void SocketServer::incomingConnection(qintptr socketDescriptor){
qDebug() << socketDescriptor << " Connecting...";
SocketThread *thread = new SocketThread(socketDescriptor,this);
connect(thread, SIGNAL(finished()),thread, SLOT(deleteLater()));
connect(thread, SIGNAL(newData(QByteArray)), this, SLOT(incomeData(QByteArray)));
thread->start();
}
void SocketServer::incomeData(QByteArray newData){
emit sendBack(newData);
}
主题:
#include "socketthread.h"
SocketThread::SocketThread(qintptr ID, QObject *parent) :
QThread(parent) {
this->socketDescriptor = ID;
}
void SocketThread::run(){
//thread starts here
qDebug() << socketDescriptor << " Starting thread";
socket = new QTcpSocket();
if(!socket->setSocketDescriptor(this->socketDescriptor)){
emit error(socket->error());
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
qDebug() << socketDescriptor << " Client Connected";
writeBack("connected");
exec();
}
void SocketThread::readyRead(){
QByteArray incomeData = socket->readAll();
qDebug() << socketDescriptor << " Data in: " << incomeData;
emit newData(incomeData);
}
void SocketThread::writeBack(QByteArray Data){
socket->write(Data);
}
void SocketThread::disconnected(){
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
如上所述,客户端连接,并编写这些调试输出: 924连接...... 924启动线程 924客户端已连接
但无论我尝试从客户端编写什么内容,它都不会通过readyReady()函数获取任何内容。有任何想法吗?我在写作的路线上缺少具体的结局吗? (我试过\ r \ n和\ 0,它们似乎做得不多)
答案 0 :(得分:2)
您似乎在Java中使用基于行的处理,但在C ++中却没有:
in.readLine()
与socket->readAll()
和
out.println(fromUser)
与writeBack("connected");
C ++从不发送CR / LF,当另一端不期望一个时,这不是问题 - &gt; readAll()
。然而,Java端确实期望(并发送)CR / LF,因为您使用BufferedReader.readLine()
和BufferedWriter.writeln()
。
决定是否需要基于行的通信,然后相应地调整C ++或Java端。