0

我正在尝试编写一个线程函数,通过 Tcp/ip 通过本地网络将系统信息发送到另一台计算机。我一直在使用套接字来实现这一点,到目前为止,这一切都很好。但我现在处于通常可以正常工作的地步,但大约 30% 的时间我收到错误消息,告诉我无法打开套接字。我将 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;
 }

我认为我对这些功能做错了,问题不在接收数据的另一端。任何人都可以在此代码中看到任何可能导致失败的明显错误吗?我不断收到自己的 cout 消息"Monitor: FAILED TO OPEN SOCKET FOR DATA"

编辑:使用 telnet 一切正常,100% 的时间

4

3 回答 3

1

You can use netstat to check that the server is listening on the port and connections are being established. Snoop is another good application in your Armour for finding out what is going wrong. Another possibility is to use telnet to see if the client can connect to that IP address and port. As to the code I will take a look at it later to see if something has gone awry.

于 2012-07-13T14:23:09.227 回答
0

socket is a global variable. 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.

Some documentation for CActiveSocket::Open reads: "Connection-based protocol sockets (CSocket::SocketTypeTcp) may successfully call Open() only once..."

Perhaps your program fails when you call .Open() twice on the same object.

于 2012-07-13T15:30:35.433 回答
0

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. 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!

于 2012-07-16T08:09:29.697 回答