- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试打开一个 TCP 套接字并将数据转储到一个文件中。我试图获得与我的 python3
版本相同的结果。逐行输出到控制台和文件,如下所示:
python3 logsvrd.py
Server started on port: 8901
Client (172.16.1.1, 4937) connected
b'<166>%ASA-6-414007: TCP syslog server connection restored. New connections allowed.\n'
b'<166>%ASA-6-302016: Teardown UDP connection 778663 for OUTSIDE:172.31.252.15/514 to INSIDE:172.16.1.12/514 duration 0:02:10 bytes 258\n'
b'<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-305011: Built dynamic TCP translation from INSIDE:172.16.1.117/63215 to OUTSIDE:68.202.229.140/63215\n'
b'<166>%ASA-6-302013: Built outbound TCP connection 778712 for OUTSIDE:172.25.5.10/8443 (172.25.5.10/8443) to INSIDE:172.16.1.117/63215 (68.202.229.140/63215)\n'
b'<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-302014: Teardown TCP connection 776408 for OUTSIDE:17.154.66.156/443 to INSIDE:172.16.1.112/58335 duration 1:06:50 bytes 3651 Connection timeout\n'
b'<166>%ASA-6-305012: Teardown dynamic TCP translation from INSIDE:172.16.1.112/58335 to OUTSIDE:68.202.229.140/13531 duration 1:06:50\n'
b'<166>%ASA-6-302014: Teardown TCP connection 778706 for OUTSIDE:172.25.5.10/8443 to INSIDE:172.16.1.117/63193 duration 0:00:30 bytes 0 SYN Timeout\n'
b'<166>%ASA-6-305012: Teardown dynamic TCP translation from INSIDE:172.16.1.117/63193 to OUTSIDE:68.202.229.140/63193 duration 0:00:30\n'
b'<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0\n'
b'<166>%ASA-6-305012: Teardown dynamic UDP translation from INSIDE:172.16.1.117/58331 to OUTSIDE:68.202.229.140/58331 duration 0:02:33\n'
b'<166>%ASA-6-302014: Teardown TCP connection 776406 for OUTSIDE:17.154.66.156/443 to INSIDE:172.16.1.112/58333 duration 1:06:55 bytes 11658 Connection timeout\n'
b'<166>%ASA-6-305012: Teardown dynamic TCP translation from INSIDE:172.16.1.112/58333 to OUTSIDE:68.202.229.140/58333 duration 1:06:55\n'
当我在 c++
中执行此操作时,它会产生断断续续的结果:
<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
E:172.16.1.117/62572 (68.202.229.140/62572)
<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
.1/0
E:172.16.1.117/62572 (68.202.229.140/62572)
<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
E:172.16.1.117/62572 (68.202.229.140/62572)
<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
.1/0
E:172.16.1.117/62572 (68.202.229.140/62572)
<166>%ASA-6-302014: Teardown TCP connection 778219 for OUTSIDE:172.25.5.10/8443 to INSIDE:172.16.1.117/62550 duration 0:00:30 bytes 0 SYN Timeout
140/62572)
<166>%ASA-6-305012: Teardown dynamic TCP translation from INSIDE:172.16.1.117/62550 to OUTSIDE:68.202.229.140/62550 duration 0:00:30
SYN Timeout
140/62572)
<166>%ASA-6-302016: Teardown UDP connection 778171 for INSIDE:172.16.1.117/68 to identity:172.16.1.1/67 duration 0:02:02 bytes 620
0
SYN Timeout
140/62572)
<166>%ASA-6-302016: Teardown UDP connection 778172 for OUTSIDE:172.31.253.8/53 to INSIDE:172.16.1.117/58307 duration 0:02:02 bytes 163
YN Timeout
140/62572)
<166>%ASA-6-302016: Teardown UDP connection 778173 for OUTSIDE:172.31.253.8/53 to INSIDE:172.16.1.117/56906 duration 0:02:01 bytes 147
YN Timeout
140/62572)
<166>%ASA-6-604103: DHCP daemon interface INSIDE: address granted 0110.4a7d.af14.fa (172.16.1.122)
7/56906 duration 0:02:01 bytes 147
YN Timeout
140/62572)
<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
ion 0:02:01 bytes 147
YN Timeout
140/62572)
<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
.1/0
ion 0:02:01 bytes 147
YN Timeout
140/62572)
<166>%ASA-6-305011: Built dynamic TCP translation from INSIDE:172.16.1.122/61908 to OUTSIDE:68.202.229.140/61908
<166>%ASA-6-302013: Built outbound TCP connection 778266 for OUTSIDE:172.16.82.10/389 (172.16.82.10/389) to INSIDE:172.16.1.122/61908 (68.202.229.140/61908)
<166>%ASA-6-302014: Teardown TCP connection 778221 for OUTSIDE:10.201.218.61/443 to INSIDE:172.16.1.122/61901 duration 0:00:30 bytes 0 SYN Timeout
TCP connection 778266 for OUTSIDE:172.16.82.10/389 (172.16.82.10/389) to INSIDE:172.16.1.122/61908 (68.202.229.140/61908)
<166>%ASA-6-305012: Teardown dynamic TCP translation from INSIDE:172.16.1.122/61901 to OUTSIDE:68.202.229.140/61901 duration 0:00:30
0 SYN Timeout
TCP connection 778266 for OUTSIDE:172.16.82.10/389 (172.16.82.10/389) to INSIDE:172.16.1.122/61908 (68.202.229.140/61908)
<166>%ASA-6-302015: Built outbound UDP connection 778267 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.122/49834 (172.16.1.122/49834)
onnection 778266 for OUTSIDE:172.16.82.10/389 (172.16.82.10/389) to INSIDE:172.16.1.122/61908 (68.202.229.140/61908)
<166>%ASA-6-302015: Built outbound UDP connection 778268 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.117/22843 (172.16.1.117/22843)
onnection 778266 for OUTSIDE:172.16.82.10/389 (172.16.82.10/389) to INSIDE:172.16.1.122/61908 (68.202.229.140/61908)
<166>%ASA-6-305011: Built dynamic UDP translation from INSIDE:172.16.1.117/49549 to OUTSIDE:68.202.229.140/49549
<166>%ASA-6-302015: Built outbound UDP connection 778269 for OUTSIDE:216.58.216.78/443 (216.58.216.78/443) to INSIDE:172.16.1.117/49549 (68.202.229.140/49549)
<166>%ASA-6-305011: Built dynamic TCP translation from INSIDE:172.16.1.117/62591 to OUTSIDE:68.202.229.140/62591
<166>%ASA-6-302013: Built outbound TCP connection 778270 for OUTSIDE:216.58.216.78/443 (216.58.216.78/443) to INSIDE:172.16.1.117/62591 (68.202.229.140/62591)
<166>%ASA-6-302015: Built outbound UDP connection 778271 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.117/63224 (172.16.1.117/63224)
<166>%ASA-6-305011: Built dynamic UDP translation from INSIDE:172.16.1.117/57906 to OUTSIDE:68.202.229.140/57906
<166>%ASA-6-302015: Built outbound UDP connection 778272 for OUTSIDE:192.168.10.2/53 (192.168.10.2/53) to INSIDE:172.16.1.117/57906 (68.202.229.140/57906)
91)
<166>%ASA-6-302015: Built outbound UDP connection 778271 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.117/63224 (172.16.1.117/63224)
<166>%ASA-6-302020: Built inbound ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
<166>%ASA-6-302015: Built outbound UDP connection 778272 for OUTSIDE:192.168.10.2/53 (192.168.10.2/53) to INSIDE:172.16.1.117/57906 (68.202.229.140/57906)
91)
<166>%ASA-6-302015: Built outbound UDP connection 778271 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.117/63224 (172.16.1.117/63224)
<166>%ASA-6-302021: Teardown ICMP connection for faddr 172.16.1.123/0 gaddr 172.16.1.1/0 laddr 172.16.1.1/0
.1/0
<166>%ASA-6-302015: Built outbound UDP connection 778272 for OUTSIDE:192.168.10.2/53 (192.168.10.2/53) to INSIDE:172.16.1.117/57906 (68.202.229.140/57906)
91)
<166>%ASA-6-302015: Built outbound UDP connection 778271 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.117/63224 (172.16.1.117/63224)
<166>%ASA-6-305011: Built dynamic TCP translation from INSIDE:172.16.1.117/62594 to OUTSIDE:68.202.229.140/62594
<166>%ASA-6-302015: Built outbound UDP connection 778272 for OUTSIDE:192.168.10.2/53 (192.168.10.2/53) to INSIDE:172.16.1.117/57906 (68.202.229.140/57906)
91)
<166>%ASA-6-302015: Built outbound UDP connection 778271 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.117/63224 (172.16.1.117/63224)
<166>%ASA-6-302013: Built outbound TCP connection 778274 for OUTSIDE:172.25.5.10/8443 (172.25.5.10/8443) to INSIDE:172.16.1.117/62594 (68.202.229.140/62594)
ction 778272 for OUTSIDE:192.168.10.2/53 (192.168.10.2/53) to INSIDE:172.16.1.117/57906 (68.202.229.140/57906)
91)
<166>%ASA-6-302015: Built outbound UDP connection 778271 for OUTSIDE:172.31.253.8/53 (172.31.253.8/53) to INSIDE:172.16.1.117/63224 (172.16.1.117/63224)
代码如下:
// Handle errors with messages
void error(const char *msg){
perror(msg);
exit(1);
}
// Handle TCP client
void HandleClient(int clntSession, std::string logmsg_path){
char buffer[BUFSIZE];
// build the bin file time stamp
std::time_t epoch = std::time(nullptr);
std::string ts = std::to_string(epoch);
std::string logmsg = logmsg_path + "log_msg.bin." + ts;
std::ofstream bin_outfile(logmsg, std::ios::out | std::ios::binary | std::ios::app);
// receive syslog message
ssize_t numBytesRcved = recv(clntSession, buffer, BUFSIZE, 0);
if (numBytesRcved <0) {
error("revived failed");
}
// Send received string and receive again until end of stream
while(numBytesRcved > 0){
// Echo message back to client
ssize_t numBytesSent = send(clntSession, buffer, numBytesRcved, 0);
if (numBytesSent < 0) {
error("send failed");
}
else if (numBytesSent != numBytesRcved) {
error("unexpected number of bytes sent");
numBytesRcved = recv(clntSession, buffer, BUFSIZE, 0);
}
// See if there is more data to receive
numBytesRcved = recv(clntSession, buffer, BUFSIZE, 0 );
if (numBytesRcved < 0) {
error("recive faild");
}
// write buffer to binary file and std::cout
//bin_outfile << buffer;
std::cout << buffer;
}
close(clntSession); // close the session when done
}
最佳答案
您应该做但没有做的一件事是确保您使用适当的函数来输出完整数据。
您当前的代码使用的函数将在遇到 NULL 字节时停止处理返回的缓冲区。 printf
等函数使用 %s
格式说明符,和 std::cout
当给出一个字符指针作为 operator <<
的参数时将停止在 NULL 上。如果您的数据包含嵌入的 NULL,您将无法处理(或查看)完整数据,从而得到截断的结果。
numBytesRcved
您声明的变量应该以某种方式用于通知后续代码确切如何处理返回的缓冲区。因此,例如,如果您想将缓冲区输出到 std::cout
,而不是这个:
std::cout << buffer;
应该是这样的:
std::cout.write(buffer, numBytesRcvd);
对于 strcpy
等函数也可以这样说, strlen
等,在 NULL 字节上停止——不要使用它们来处理或操作可能嵌入 NULL 的缓冲区。
补充一下(因为我在使用 std::string
存储缓冲区时多次看到这个错误):
对于诸如 std::string
之类的事情.而不是这样的:
std::string s = buffer;
应该是这样的:
std::string s = std::string(buffer, numBytesRcvd);
或者如果附加到现有数据:
std::string s;
//... assume s has data already, now append
s.append(buffer, numBytesRcvd);
关于python - 为什么我的缓冲区输出被切碎了? C++套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32571932/
这个问题在这里已经有了答案: Possible to make an event handler wait until async / Promise-based code is done? (2
我经常有多个运行的进程(R,Python,eshell/shell),对于每个进程,我经常都有一个相关的脚本,可以从中发送摘要。为此,我通常将每个框架垂直地分成两个窗口,以便脚本文件(例如.py)位于
如何修改 emacs 在关闭缓冲区后选择要显示的缓冲区的方式? 当我有多个列显示相同的缓冲区,然后在其中一个缓冲区中打开另一个文件,然后关闭新打开的缓冲区时,它不会切换回前一个缓冲区,而是切换到另一个
如何将 ex 命令复制到剪贴板或粘贴到缓冲区? 在 Windows 上使用 gvim。 最佳答案 windows剪贴板可以通过the buffer + 访问.因此,可以使用 + 将剪贴板粘贴为前命令。
在 javascript 中如何以比以下更简单的方式获取 b 缓冲区? var num=6553599 var a = new Buffer(4); a.writeInt32LE(num)
每次我在 Google 上搜索有关 OpenGL 编程的文章时,我都会找到一些文章,但似乎所有文章都提到了着色器和缓冲区。那些是什么?你能解释其中的一些吗: 深度缓冲区 模板缓冲区 像素着色器 帧缓冲
我有java考试,当我学习时,我看到了这个练习,我尝试解决它,但我发现一些困难,所以请帮助我考虑实用程序中方法的以下注释、 header 和部分代码名为 Atbash 的加密类。 /**
每次我在 Google 上搜索有关 OpenGL 编程的文章时,我都会找到一些文章,但似乎所有文章都提到了着色器和缓冲区。那些是什么?你能解释其中的一些吗: 深度缓冲区 模板缓冲区 像素着色器 帧缓冲
对于每个属性使用跨步顶点缓冲区与紧密打包缓冲区有何优缺点?我的意思是例如: 步幅:xyzrgb xyzrgb xyzrgb 紧:xyzxyzxyz rgbrgbrgb 乍一看,使用步幅时您似乎可以轻松
我正在尝试将文本文件中每行的数字读取到 ArrayList 中。当我执行以下函数时,它总是跳过最后一个元素。有人可以帮我吗?因为我在这里没有遇到问题,因为它读取直到缓冲区为空,所以他应该在到达 Fil
#include #include int main () { time_t time_raw_format; struct tm * ptr_time; char *buff
基本上我有一个包含不同类型数据的自定义结构。例如: typedef struct example_structure{ uint8_t* example_1[4]; int example_2[4];
我之前的列表实现是一个简单的 LinearLayout,位于一个装满我的项目的 ScrollView 中。 我切换到 ListView 的 Android 实现以简单地使用 CursorAdapter
我想创建一个可变长度的输入事件窗口/缓冲区,当它接收到额外的事件时会变长。 这是为了实现“键入时搜索”功能。我想捕获点击,但为了不给服务器造成压力,我想明智地进行服务调用。 我想到的逻辑是缓冲击键,从
我想将 yuv420P 像素写入缓冲区而不是二进制文件。假设我在指针中存储了 luma 、 Cb 和 Cr。 luma = output_pixel.luma; cb = output_pixel.c
我想在 Go 中构建一个支持多个并发读取器和一个写入器的缓冲区。所有写入缓冲区的内容都应由所有读者读取。允许新读者随时加入,这意味着已经写入的数据必须能够为迟到的读者回放。 缓冲区应满足以下接口(in
本文转载自微信公众号「小明菜市场」,作者小明菜市场。转载本文请联系小明菜市场公众号。 前言 Java NIO 需要理解的主要有缓冲区,通道,选择器,这三个主要的部分。 基础
一 点睛 NIO,可以称为 New IO 或 Non Blocking IO,是在 JDK 1.4 后提供的新 API。传统的I/O 是阻塞式的 I/O、面向流的操作;而 NIO 是非阻塞 I/O 、
我正在寻找一种切换到包含搜索文本的缓冲区的方法。 例如。如果我打开了 100 个缓冲区,我想切换到一个包含 'fooBar = 1' 的缓冲区 最佳答案 我写了一个 Vim 插件来做到这一点:buff
我正在尝试将提取的视频帧(我使用 ffmpeg)推送到 FFMPEG 缓冲区中。我已经查看了 ffmpeg 的缓冲区源文件,例如 buffersrc.c 和 fifo.c,以确定我是否可以这样做,但我
我是一名优秀的程序员,十分优秀!