Note that there are some explanatory texts on larger screens.

plurals
  1. POTCP/Ip network communication in c++
    text
    copied!<p>I am trying to write a threaded function that sends system information via Tcp/ip over the local network to another computer. 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. I use the activeSocket library for the sockets.</p> <pre><code>#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(&amp;time, 0, 11); socket.Initialize(); socket.SetConnectTimeout(0, 20); socket.SetSendTimeout(0, 20); return communicationFailed; } int monitor() { cout &lt;&lt; "Monitor: init continious monitoring" &lt;&lt; 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() &gt; 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 &lt;&lt; "Monitor: " &lt;&lt; buffer &lt;&lt; endl; while (communicationFailed == 1 &amp;&amp; monitorCount &lt; 2) { monitorCount++; if (socket.Open(realip, 2417)) { if (socket.Send((const uint8 *) buffer, strlen(buffer))) { cout &lt;&lt; "Monitor: Succeeded sending data" &lt;&lt; endl; communicationFailed = 0; socket.Close(); } else { socket.Close(); communicationFailed = 1; cout &lt;&lt; "Monitor: FAILED TO SEND DATA" &lt;&lt; endl; } } else { socket.Close(); communicationFailed = 1; cout &lt;&lt; "Monitor: FAILED TO OPEN SOCKET FOR DATA" &lt;&lt; endl; } } if (monitorCount == 2) cout &lt;&lt; "Monitor: UNABLE TO SEND DATA" &lt;&lt; endl; } } return communicationFailed; } </code></pre> <p>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 <code>"Monitor: FAILED TO OPEN SOCKET FOR DATA"</code></p> <p>EDIT: With telnet everything works fine, 100% of the time</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload