gpt4 book ai didi

与Qt的Java套接字连接

转载 作者:搜寻专家 更新时间:2023-10-31 02:15:26 24 4
gpt4 key购买 nike

我正在尝试将一个非常简单的 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我都试过了,好像作用不大)

最佳答案

您似乎在 Java 中使用了基于行的处理,但在 C++ 中却没有:

in.readLine()socket->readAll()

out.println(fromUser)writeBack("connected");

C++ 只是从不发送 CR/LF,当另一端不希望发送 CR/LF 时这不是问题 -> readAll()。然而,Java 端确实期望(并发送)CR/LF,因为您使用了 BufferedReader.readLine()BufferedWriter.writeln()

决定是否需要基于线路的通信,然后相应地调整 C++ 或 Java 端。

关于与Qt的Java套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com