在我的代码中,我能够生成一个向量uint8_t,其中包含十六进制值,因为我可以通过argv将它们传递出去。我将代码封装起来,如何将“命令”向量的组件作为主函数的参数传递?我希望我自己解释一下。
我想以这种方式传递这些值:
./ main 192.168.1.60 0x26 0x00 0x00 0x00 0x26
或也
./ main 192.168.1.60 2600000026
谢谢
#include <stdio.h>
#include <errno.h>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <unistd.h>
#include <sstream>
using namespace std;
// per compilare gcc test.cpp -lstdc++ -o main
int main(int argc, char *argv[])
{
int sockfd, n;
int connected = 0;
struct sockaddr_in servaddr;
char sendline[5];
char recvline[5];
std::string serveraddr = argv[1];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(serveraddr.c_str());
servaddr.sin_port = htons(9761);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
uint8_t command[] = {0x26, 0x00, 0x00, 0x00, 0x26};
int bytes_to_send = sizeof(command);
//cout << command;
send(sockfd, command, bytes_to_send, 0);
recv(sockfd, command, bytes_to_send, 0);
}
我尝试了您的建议(@Remy Lebeau),但它们不起作用,我重新编写了与您相关的代码。
我还将出口附加到cout
#include <stdio.h>
#include <errno.h>
#include <string>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include <iostream>
#include <unistd.h>
#include <sstream>
using namespace std;
// to compile gcc test.cpp -lstdc++ -o main
int main(int argc, char *argv[])
{
int sockfd, n;
int connected = 0;
struct sockaddr_in servaddr;
std::string serveraddr = argv[1];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(serveraddr.c_str());
servaddr.sin_port = htons(9761);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
std::string pref_hex;
std::string hex("0x");
std::string test = argv[2];
size_t numbytes = test.size() / 2;
uint8_t command[numbytes];
for (size_t w = 0, x = 0; w < numbytes; ++w, x += 2)
{
pref_hex = hex + test.substr(x, 2);
cout << pref_hex;
command[w - 1] = stoi(pref_hex, nullptr, 16);
//cout << test.substr(x, 2);
cout << "\n";
cout << command[w - 1];
cout << "\n";
}
//uint8_t command[] = {0x26, 0x00, 0x00, 0x00, 0x26};
int bytes_to_send = sizeof(command);
send(sockfd, command, bytes_to_send, 0);
uint8_t output_command[numbytes];
recv(sockfd, output_command, bytes_to_send, 0);
}
出口退出:
0x26
0x00
0x00
0x00
0x26&
另外,我还将发送的代码的嗅探与我发布的第一个代码附加在一起,并且可以正常工作,对第二个代码的嗅探进行了修改,以将十六进制值从函数中传递出去,并且不起作用。实际上,这些包是用不同的方式编写的……我在做什么错?
嗅探器工作代码:
无效的代码