为什么我的缓冲输出会被切断? c ++套接字编程

时间:2015-09-14 18:56:46

标签: python c++ sockets c++11 tcp

我正在尝试打开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++中执行此操作时,它会生成chopy结果:

<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
}

1 个答案:

答案 0 :(得分:2)

您应该做的一件事就是确保您使用适当的功能输出完整的数据。

当前代码使用的函数会在遇到NULL字节时停止处理返回的缓冲区。使用printf格式说明符的%sstd::cout作为operator <<的参数时numBytesRcved的函数将停止在NULL上。如果您的数据包含嵌入的NULL,则不会处理(或查看)完整数据,从而为您提供截断的结果。

您声明的std::cout变量应该以某种方式用于通知后续代码如何处理返回的缓冲区。例如,如果您想将缓冲区输出到std::cout << buffer; ,而不是:

std::cout.write(buffer, numBytesRcvd);

它应该是这样的:

strcpy

对于诸如strlenstd::string等停止在NULL字节上的函数,可以说同样的事情 - 不要使用它们来处理或操作可能嵌入NULL的缓冲区。

添加(因为我在使用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);

或附加到现有数据:

theArray.filter() { $0.json?.rangeOfString(searchQuery, options: .CaseInsensitiveSearch) != nil}