简体   繁体   English

C ++中的TCP / Ip网络通讯

[英]TCP/Ip network communication in c++

I am trying to write a threaded function that sends system information via Tcp/ip over the local network to another computer. 我正在尝试编写一个线程函数,该函数通过本地网络上的Tcp / ip将系统信息发送到另一台计算机。 I have been using sockets to achieve this and this has worked out quite allright thus far. 我一直在使用套接字来实现这一点,到目前为止,这还算不错。 But I am now at a point where this usually works but around 30% of the time I get error messages telling me that the socket can not be opened. 但是我现在可以正常工作,但是大约有30%的时间我收到错误消息,告诉我无法打开套接字。 I use the activeSocket library for the sockets. 我将activeSocket库用于套接字。

#include "tbb/tick_count.h"
#include "ActiveSocket.h"

using namespace std;

CActiveSocket socket;
extern int hardwareStatus;



int establishTCP() {
 char time[11];
 int communicationFailed = 0;
 memset(&time, 0, 11);
 socket.Initialize();
 socket.SetConnectTimeout(0, 20);
 socket.SetSendTimeout(0, 20);
 return communicationFailed;
}


int monitor() {
 cout << "Monitor: init continious monitoring" << endl;
 int communicationFailed;
 tbb::tick_count monitorCounter = tbb::tick_count::now();
 while (!closeProgram) {
  tbb::tick_count currentTick = tbb::tick_count::now();
  tbb::tick_count::interval_t interval;
  interval = currentTick - monitorCounter;
  if (interval.seconds() > 2) {
   monitorCounter = tbb::tick_count::now();
   communicationFailed = 1;
   char buffer[256];
   sprintf(buffer, "%d;", hardwareStatus);

   establishTCP();

   char *charip = new char[monitoringIP.size() + 1];
   charip[monitoringIP.size()] = 0;
   memcpy(charip, monitoringIP.c_str(), monitoringIP.size());
   const uint8* realip = (const uint8 *) charip;
   int monitorCount = 0;
   cout << "Monitor: " << buffer << endl;
   while (communicationFailed == 1 && monitorCount < 2) {
    monitorCount++;
    if (socket.Open(realip, 2417)) {
     if (socket.Send((const uint8 *) buffer, strlen(buffer))) {
      cout << "Monitor: Succeeded sending data" << endl;
      communicationFailed = 0;
      socket.Close();
      } else {
       socket.Close();
       communicationFailed = 1;
       cout << "Monitor: FAILED TO SEND DATA" << endl;
      }
     } else {
       socket.Close();
       communicationFailed = 1;
       cout << "Monitor: FAILED TO OPEN SOCKET FOR DATA" << endl;
      }
     }
    if (monitorCount == 2) cout << "Monitor: UNABLE TO SEND DATA" << endl;
   }
  }
  return communicationFailed;
 }

I think I am doing something wrong with these functions and that the problem is not on the other side of the line where this data is received. 我想我在这些功能上做错了,问题不在接收此数据的那一边。 Can anyone see any obvious mistakes in this code that could cause the failure? 谁能在此代码中看到任何明显的错误,从而导致失败? I keep getting my own cout message "Monitor: FAILED TO OPEN SOCKET FOR DATA" 我不断收到自己的提示信息: "Monitor: FAILED TO OPEN SOCKET FOR DATA"

EDIT: With telnet everything works fine, 100% of the time 编辑:使用telnet,一切正常,时间100%

You can use netstat to check that the server is listening on the port and connections are being established. 您可以使用netstat来检查服务器是否正在侦听端口并建立连接。 Snoop is another good application in your Armour for finding out what is going wrong. Snoop是Armor中另一个用于发现问题出在哪里的好应用程序。 Another possibility is to use telnet to see if the client can connect to that IP address and port. 另一种可能性是使用telnet查看客户端是否可以连接到该IP地址和端口。 As to the code I will take a look at it later to see if something has gone awry. 至于代码,我稍后会看一下,看是否有问题。

socket is a global variable. socket是一个全局变量。 It might be re-used concurrently between two threads or sequentially inside one thread. 它可以在两个线程之间并发使用,也可以在一个线程内按顺序使用。 In fact, the while(~closeProgram) loop indicates that you intend to use it sequentially. 实际上, while(~closeProgram)循环表明您打算顺序使用它。

Some documentation for CActiveSocket::Open reads: " Connection-based protocol sockets (CSocket::SocketTypeTcp) may successfully call Open() only once... " CActiveSocket::Open某些文档显示:“ 基于连接的协议套接字(CSocket :: SocketTypeTcp)只能成功调用Open()一次...

Perhaps your program fails when you call .Open() twice on the same object. 当您在同一对象上两次调用.Open()时,程序可能会失败。

I eventually found out the problem with my code. 我最终发现我的代码有问题。 As the connection was unstable and working for 70% of the time it seemed to be a timeout issue. 由于连接不稳定并且在70%的时间内都正常工作,因此似乎是超时问题。 I removed the two timeout settings 我删除了两个超时设置

socket.SetConnectTimeout(0, 20);
socket.SetSendTimeout(0, 20);

Now it works perfectly fine, thanks for the troubleshooting tips though! 现在,它工作得非常好,不过感谢您提供的故障排除提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM