netcat command in c++
netcat command in c++
当谈到C++编码时,我是个新手。我目前正在使用qt制作一个简单的GUI,在那里我想通过TCP/IP向设备发送命令。
当我将电脑连接到设备并通过终端发送命令时:
echo '3b00010000001b010001000000120000013000002713000300030101' | xxd -r -p | nc 192.168.1.101 30013
该设备相应地工作。
我需要能够将这个命令作为函数在qt中发送。有人能帮我吗?这是我到目前为止(不工作(
标题:
#ifndef SOCKET_H
#define SOCKET_H
#include <QObject>
#include <QTcpSocket>
#include <QtDebug>
#include <string>
using namespace std;
class Socket : public QObject
{
Q_OBJECT
public:
explicit Socket(QObject *parent = nullptr);
void Connect(const QString &host, const string &cmd);
private:
QTcpSocket *socket;
};
#endif // SOCKET_H
Cpp:
#include "socket.h"
Socket::Socket(QObject *parent) : QObject(parent)
{
}
void Socket::Connect(const QString &host, const string &cmd)
{
//connect
socket = new QTcpSocket(this);
socket->connectToHost(host,30013);
if(socket->waitForConnected(1500))
{
qDebug() << "Connected";
//send
socket->write(cmd.c_str(), cmd.size());
socket->waitForBytesWritten(1000);
//close
socket->close();
}
else
qDebug() << "Not Connected";
}
然后我想通过以下方式发送命令:
Socket.Test
Test.Connect("192.168.1.101","3b00010000001b010001000000120000013000002713000300030101")
任何帮助都将不胜感激。谢谢
由于您的命令是一个固定字符串,您可以直接输入字符:
const char data[] = "x3bx00x01x00x00x00x1bx01x00x01x00x00x00x12x00x00x01x30x00x00x27x13x00x03x00x03x01x01";
Test.Connect("192.168.1.101",string(data, sizeof(data)-1));
请注意,由于数据中嵌入了null字符,因此不能简单地将字符串文本传递给std::string
,因为它会截断第一个null字符之前的字符串。
相关文章:
- netcat command in c++
- Difference in displaying cv2 Mat
- C++ MFC Libraries in Travis CI
- 如何在OpenSSL中从configuration.h.in获取configuration.h
- 创建具有 new in 函数和"this is nullptr"异常的对象
- IN, OUT, INOUT Parameters
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 有人安装"IITB Simplecpp in mac"吗?
- 从 C 样式字符串中删除子字符串 "in place" 在C++代码中
- 如何修复"error: ‘_1’ was not declared in this scope"?
- Softmax Implementation in C++
- 将 out/in out 参数与 if/switch 的 init 语句一起使用
- IF-nesting in c++
- Gurobi GRBModel and GRBmodel in C++
- Tensorflow Hub in C++
- Centos7 g++ "to_string is not in a member of std"
- InitializeCriticalSectionEx Not Located In KERNEL32.Dll
- 将 lambda 表达式传递给 std::function in C++
- @CPPFLAGS@在 Makefile.in 中意味着什么?
- std::async from std::async in windows xp