Jump to content
  • Advertisement
Sign in to follow this  

Winsock recieve function error

This topic is 4257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The winsock now works just fine MessageBox(NULL, "0", NULL, NULL); string charStringWSS=string(charStringWS); //Error (charStringWS is char*) MessageBox(NULL, "1", NULL, NULL); Now, at runtime after the client connects, this code will execute. The line gets executed, and I get a 0 message. When the string line executes (at runtime), the server process just stops. No error messages, no winsock errors. The server just stops. ? What is causing this and how do I fix this? [Edited by - hsdjjssjsdf on March 24, 2007 1:29:01 PM]

Share this post

Link to post
Share on other sites
Its a char *, but what does it point at? Is it properly NUL terminated?

Also note that if string is of type "std::string", then you can write:
string charStringWSS = charStringWS;
which is a little easier to read than the original line.

Finally, what a horrible variable name. Any string is made of chars. There is no need to document that in the variable name. If I wanted to know the type of a variable I can scroll up to where its declared. Pick a name based on the purpose of the variable, not its type.

And giving 2 variables such similar names is just asking for trouble when reading or debugging that code.

Share this post

Link to post
Share on other sites
I found the error, it's in the recieve loop. For some reason the loop doesn't receive anything and continues on to initialization but charStringWS is null so it stops.

The loop is:

int RetVal = SOCKET_ERROR;

while (RetVal == SOCKET_ERROR&&(charStringWS == ""||charStringWS == NULL))
RetVal = recv(Socket0, charStringWS, 999, 0);
MessageBox(NULL, "Aborted", NULL, NULL);
exitCleanup(1); //(Custom exit function)

What is wrong with this loop?

[Edited by - hsdjjssjsdf on March 24, 2007 1:26:01 PM]

Share this post

Link to post
Share on other sites
The first error is that you haven't use the forum's Code or Source tag to format your post properly.

The second error is that you don't understand C-style strings - if they're "char*", then you cannot use "==" to compare them to empty strings. You also have to ensure they point at valid memory before you write to them, which you may or may not be doing (and since you've not provided the code where you initialise it, it's impossible for us to tell).

Share this post

Link to post
Share on other sites
Original post by hsdjjssjsdf
Okay, I changed
"while (RetVal == SOCKET_ERROR&&(charStringWS == ""||charStringWS == NULL))"
"while (RetVal == SOCKET_ERROR)"
and the while loop never quit

Check out this post: http://www.gamedev.net/community/forums/topic.asp?topic_id=440506 with my error I had before for my other code

The charStringWS is highly misleading. It makes one thing it's a wide string of std::wstring type.

But that's not the problem.

Look at RetVal and its value as returned by recv.

If the call succeeded, the return value will be number of bytes read into charStringWS. So checking for contents of string is redundant (and incorrect in this case). If charStringWS == NULL, the call won't succeed in the first place (socket is reading into invalid address).

Also, if your charStringWS is std::string type, you cannot pass it as buffer for socket to read into.

Typical call would be something along these lines:

// Allocate receive buffer of 1500 bytes
int charStringLen = 1500;
char charStringWS[charStringLen]; // Not a string, memory buffer

int RetVal;
bool value_socket = true;

while (valid_socket) {
// We reserve last byte in buffer so we can make a properly terminated
// string later
RetVal = recv(Socket0, charStringWS, charStringLen-1 , 0);
if (RetVal == SOCKET_ERROR ) {
valid_socket = false;
} else {
// Set last char in our buffer to 0, so it becomes valid char string
charStringWS[RetVal] = 0;
// Now we make proper string object from received bytes.
// From here on, the message
std::string message_text(charStringWS);
// Use the value stored in message_text.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!