Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with winsock recv() and accept()
    text
    copied!<p>I'm having trouble with a socket application I'm programming in C++. I'm doing my programming with Bloodshed Dev-Cpp on Windows XP. I made a class for handling all the message transfers and have a client and server program that both use that class for handling their services. The application itself is very basic, the only intent I have for it is to get all this to work.</p> <p>The client, which is for sending messages, works like I expect it to. If my server is running, it doesn't have any errors when sending a message. If it's not running it'll pass an error. But my server continuously accepts weird gibberish. It's always the same data. When it receives the message there is no effect. If I have my client try to identify the server, it gets back gibberish.</p> <p>I have included my source code here. The linker also brings in two extra parameters: -lwsock32 and an inclusion of the library libws2_32.a, which came with Dev-Cpp.</p> <p>Here's the header for my Messager class:</p> <pre><code>#ifndef MESSAGER #define MESSAGER #include &lt;string&gt; class Messager{ private: int sendSocket; int listenSocket; public: void init(void); bool connect(std::string ip, std::string port); bool bind(std::string port); void listen(void); void send(std::string message); std::string receive(void); }; #endif </code></pre> <p>These are my definitions for the Messager class:</p> <pre><code>#include "Messager.h" #include &lt;winsock2.h&gt; #include &lt;sys/types.h&gt; #include &lt;ws2tcpip.h&gt; #include &lt;windows.h&gt; void Messager::init(void){ WSADATA wsaData; WSAStartup(MAKEWORD(1,1), &amp;wsaData); } bool Messager::connect(std::string ip, std::string port){ struct addrinfo hints; struct addrinfo *res; bool success = false; memset(&amp;hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; getaddrinfo(ip.c_str(), port.c_str(), &amp;hints, &amp;res); sendSocket = socket(res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol); success = ::connect(sendSocket, res-&gt;ai_addr, res-&gt;ai_addrlen) != -1; freeaddrinfo(res); return success; } bool Messager::bind(std::string port){ struct addrinfo hints, *res; memset(&amp;hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; getaddrinfo(NULL, port.c_str(), &amp;hints, &amp;res); listenSocket = socket(res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol); if(listenSocket == INVALID_SOCKET){ return false; } if(::bind(listenSocket, res-&gt;ai_addr, res-&gt;ai_addrlen) == -1){ return false; } return true; } void Messager::listen(void){ ::listen(listenSocket, 10); } int Messager::send(std::string message){ const std::string terminator = "\r\n"; std::string realMessage; int size = 0; int totalSent = 0; realMessage = message; realMessage += terminator; size = realMessage.size(); totalSent = ::send(sendSocket, realMessage.c_str(), size, 0); if(totalSent == 0 || totalSent == -1){ return 0; // There must be an error, 0 means it is an error } // This statement keeps adding the results of ::send to totalSent until it's the size of the full message for(totalSent = 0; totalSent &lt; size; totalSent += ::send(sendSocket, realMessage.c_str(), size, 0)); return totalSent; } // This function has been updated a lot thanks to @Luke std::string Messager::receive(void){ const int bufferSize = 256; const std::string terminator = "\r\n"; char buffer[bufferSize]; int i = 0; int received = 0; std::string tempString; size_t term = 0; for(i = 0; i &lt; bufferSize; i++){ buffer[i] = 0; } received = ::recv(listenSocket, buffer, bufferSize, 0); tempString = buffer; term = tempString.find(terminator); if(term != -1){ // Already have line line = tempString; } while(received != -1 &amp;&amp; received != 0){ // While it is receiving information... // Flush the buffer for(i = 0; i &lt; bufferSize; i++){ buffer[i] = 0; } ::recv(listenSocket, buffer, bufferSize, 0); tempString += buffer; term = tempString.find(terminator); if(term != -1){ // Found terminator! return tempString; } } throw 0; // Didn't receive any information. Throw an error } </code></pre> <p>Any ideas about what might be going on would be really appreciated. If necessary I can post the code the server and client use, but I can give a general outline:</p> <p>Server:</p> <ul> <li>messager.init()</li> <li>messager.bind()</li> <li>messager.listen()</li> <li>messager.receive() &lt;-- includes accept()</li> </ul> <p>Client:</p> <ul> <li>messager.init()</li> <li>messager.connect()</li> <li>messager.send()</li> </ul> <p>Thanks in advance.</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