java 上的服务器未收到来自 Qt 客户端的消息
Server on java does not get message from Qt client
我有非常简单的 java 服务器:
int port = 2245;
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");
Socket incomingClient = ss.accept();
InputStream i = incomingClient.getInputStream();
OutputStream o = incomingClient.getOutputStream(); // Use it write to the Client Socket
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String str = new String();
while ((str = br.readLine())!=null){
System.out.println("str = " + str);
o.write(123); //("message from server");
}
} catch(Exception x) { x.printStackTrace(); }
我有简单的 Qt 客户端,它使用 QNetworkAccessManager
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl url("http://127.0.0.1:2245");
url.port(6666);
QByteArray postData;
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QString postKey("SomeKey");
postData.append(postKey);
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
我在 mainwindow.h 中声明了槽 replyFinished
public slots:
void replyFinished(QNetworkReply* reply);
在最好的情况下服务器得到一些 headers like(POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded 等)
但是在服务器上我无法读取来自客户端的消息
并且客户端没有收到来自 java 服务器
的任何响应
Qt 客户端的正确代码是这样的:
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl url("http://192.168.0.101:8000");
url.port(8000);
QByteArray postData;
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QString postKey("some data");
postData.append(postKey);
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
manager->post(request, postData);
}
并获得响应声明并实施以下插槽
void MainWindow::replyFinished(QNetworkReply *reply){
qDebug() << "Status" << reply->errorString();
qDebug() << "Status" << reply->error();
QByteArray data = reply->readAll(); //It's works!
qDebug() << "data: " << data;
}
对于服务器端,我使用了 HttpServer,它在 JDK
的最新版本中可用
我有非常简单的 java 服务器:
int port = 2245;
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");
Socket incomingClient = ss.accept();
InputStream i = incomingClient.getInputStream();
OutputStream o = incomingClient.getOutputStream(); // Use it write to the Client Socket
InputStreamReader isr = new InputStreamReader(i);
BufferedReader br = new BufferedReader(isr);
String str = new String();
while ((str = br.readLine())!=null){
System.out.println("str = " + str);
o.write(123); //("message from server");
}
} catch(Exception x) { x.printStackTrace(); }
我有简单的 Qt 客户端,它使用 QNetworkAccessManager
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl url("http://127.0.0.1:2245");
url.port(6666);
QByteArray postData;
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QString postKey("SomeKey");
postData.append(postKey);
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
我在 mainwindow.h 中声明了槽 replyFinished
public slots:
void replyFinished(QNetworkReply* reply);
在最好的情况下服务器得到一些 headers like(POST / HTTP/1.1 Content-Type: application/x-www-form-urlencoded 等)
但是在服务器上我无法读取来自客户端的消息 并且客户端没有收到来自 java 服务器
的任何响应Qt 客户端的正确代码是这样的:
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl url("http://192.168.0.101:8000");
url.port(8000);
QByteArray postData;
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QString postKey("some data");
postData.append(postKey);
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
manager->post(request, postData);
}
并获得响应声明并实施以下插槽
void MainWindow::replyFinished(QNetworkReply *reply){
qDebug() << "Status" << reply->errorString();
qDebug() << "Status" << reply->error();
QByteArray data = reply->readAll(); //It's works!
qDebug() << "data: " << data;
}
对于服务器端,我使用了 HttpServer,它在 JDK
的最新版本中可用