我正在尝试将一条简单消息从作为客户端的我的树莓派传输到作为服务器的我的计算机。我将 cpp 用于 tcpClient,将 java 用于 tcpServer。 这是我的 TCPServer.java:
import java.net.*;
import java.io.*;
class TCPServer extends Thread {
TCPClass() {
}
public void connect(){
try {
ServerSocket welcomeSocket = new ServerSocket();
welcomeSocket.setReuseAddress(true);
welcomeSocket.bind(new InetSocketAddress(8080));
System.out.println("server start listening... ... ...");
Socket connectionSocket = welcomeSocket.accept();
System.out.print("Server has connected!\n");
Connection c = new Connection(connectionSocket);
c.start();
connectionSocket.close();
welcomeSocket.close();
System.out.println("connection terminated");
}
catch(IOException e) {
System.out.println("Listen :"+e.getMessage());
}
}
class Connection extends Thread{
Socket connectionSocket;
Connection(Socket _connectionSocket){
connectionSocket = _connectionSocket;
}
public void run(){
try{
BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
String clientSentence = inFromClient.readLine();
System.out.println("Client sent: "+clientSentence);
String capitalizedSentence = clientSentence.toUpperCase() + '\n';
System.out.println("Client sent: "+capitalizedSentence);
outToClient.writeBytes(capitalizedSentence);
outToClient.flush();
inFromClient.close();
}catch(Exception e){}
}
}
这是我的 TCPClient.cpp:
#include "Client.h"
#include <QHostAddress>
Client::Client(QObject* parent): QObject(parent)
{
connect(&client, SIGNAL(connected()),
this, SLOT(startTransfer()));
}
Client::~Client()
{
client.close();
}
void Client::start(QString address, quint16 port)
{
QHostAddress addr(address);
client.connectToHost(addr, port);
}
void Client::startTransfer()
{
client.write("Hello, world", 13);
}
最后,这是执行我的 TCPClient.cpp 的程序:
#include "Client.h"
#include <QApplication>
#include <iostream>
int main(int argc, char** argv)
{
QApplication app(argc, argv);
Client client;
std::cout << argv[1] << " " << argv[2] << std::endl;
client.start( argv[1], atoi( argv[2] ) );
// client.start("127.0.0.1", 8888);
return app.exec();
}
所以我执行我的TCPServer.java,我有一条消息证明服务器已经连接,所以我执行我的TCPCLient.cpp,但有问题。我的服务器没有显示客户端发送的任何消息,但是当我结束 TCPClient.cpp 的执行时,该消息显示在我的屏幕上,这意味着服务器已收到该消息。奇怪不!! 感谢您的帮助。
编辑: 这是我的Client.h,抱歉耽误了
// client.h
#include <QtNetwork>
#include <QObject>
#include <QString>
#include <QTcpSocket>
class Client: public QObject
{
Q_OBJECT
public:
Client(QObject* parent = 0);
~Client();
void start(QString address, quint16 port);
public slots:
void startTransfer();
private:
QTcpSocket client;
};
最佳答案
完全没有。您正在使用以下方式关闭服务器中的套接字:
connectionSocket.close();
就在线程启动之后。因此,当线程开始执行时,它将使用关闭的套接字。请记住,start()
不会等待线程终止(它会在线程创建后立即返回)。您必须让 TcpServer
在关闭所有内容之前等待 Connection
线程终止。像这样更改代码:
Connection c = new Connection(connectionSocket);
c.start();
c.join(); //add this line. It means: wait for the termination of c then proceed to closing the socket
connectionSocket.close();
[更新]
另外,写完后需要flush client(cpp部分):
client.write("Hello, world", 13);
client.flush();
关于java - 如何在java中实现TCP server,在cpp中实现TCP Client来传输字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38331204/