Jump to content
  • Advertisement
Sign in to follow this  
hsdjjssjsdf

Solved...winsock error with string variable

This topic is 4228 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

My winsock server works without any string variables. If I add a string variable, the server compiles correctly but can't link or bind and the server fails and crashes. Without the string variable, everything runs fine. Yes, I have included this at the beginning: #include <winsock.h> #include <winsock2.h> #include <windows.h> #include <cstring> #include <string> #include <string.h> #include <iostream> #include <sstream> using namespace std; I am using dev-C++ compiler I would really like an answer!!! I have already spent 3 hours searching for it! [Edited by - hsdjjssjsdf on March 24, 2007 2:08:44 PM]

Share this post


Link to post
Share on other sites
Advertisement
For some reason I think you don't need to include both winsock.h and winsock2.h, just the second one should do.

Other than that, what is "string variable" and where do you add it?

Share this post


Link to post
Share on other sites
cut it down a bit :)


you can definitly remove #include <string.h>
in c++ you use
#include <cstring>
to get access to the old C string functions (strlen, strcat etc)
and #include <string> for C++ strings.

#include <string.h> is used in C not C++ (it works in C++ but since you are including cstring its redundant and likely to cause problems)

Share this post


Link to post
Share on other sites
If you need more information, nothing is wrong with including both winsock files. nothing should be wrong with anything else because the server runs just fine with the exact inclusions, just without any string variables.

If I add only one line of a String variable, anyware, the server won't run. Without the string variable (Either initialied or not, whether at the begining or the end) the client and the server work just fine, can connect and send messages to each-other.

With the string variable, the server just doesn't start.
The client works just fine, with or without a string.
Also, they both use the windows API WinaMain function.

The client includes:

#include <sstream>
#include <iostream>
#include <string>
#include <cstring>

#include <sstream>
#include <iostream>
#include <string>
#include <cstring>

whoops pasted twice...;)
Anways, I would really like an answer!

Share this post


Link to post
Share on other sites
Quote:
Original post by hsdjjssjsdf
If you need more information, nothing is wrong with including both winsock files. nothing should be wrong with anything else because the server runs just fine with the exact inclusions, just without any string variables.

whoops pasted twice...;)
Anways, I would really like an answer!


Posting source might help.

What kind of string, std::string, char *, CString? Is it used as a parameter, defined as global variable, class member, dynamically allocated? Is it referenced? Used as buffer? Declared in a class that gets assigned or copy constructed?

Does the server crash? Creates a socket but doesn't reply? Does the thread not start? Does it even compile? What error messages do you get?

Or does it mean if you simply put "std::string s" into your source that everything compiles fine, you start the executable, just the server doesn't start.

If so, what are the error codes you get in socket calls?

Or does including the string cause compilation error?

Share this post


Link to post
Share on other sites
Does this even have something to do with sockets?

If nobody connects, does the program stay up? Does it crash only when you're trying to send a string? If so, then you're probably not sending the characters of the string, instead trying to send the string object directly. That won't work; you have to use marshalling!

If it crashes when you start it, before any connection, then this is a linkage issue, and you'd be better helped in a tool-specific forum.

And, when posting these problems, you must post the following:

1) What is the exact error you're getting?
2) If this is at runtime, where does the debugger point, and what does the stack look like?
3) If you have inserted logging, or single-step through your program, what is the result?
4) If you say you're using a string, what is the code where that string is declared, and where it is used?

Despite multiple requests for this information, you have not posted it, which shows the forum that you're not really willing to actually read the help people are trying to offer. This means that less people will try to help you in the future, which is a bad thing. Please attempt to read the replies fully, think about what they mean, and then post a considered response.

Share this post


Link to post
Share on other sites
Whether I use the string or not, the server still crashes. Here is more specific: Everything runs fine at first. I then add a string to test it, and I test it in several places like the beginning or end. I try styles stuch as "string variableString;" and "string stringVariable="";"and "string stringVariable="dd";" and "string stringVariable=string("Elephants")" and "string stringVariable="Him" in any place possible. EVery time it failed, even if I only included on string usage statement without even using it. The program crashes at the very bginning of runtime and returns a "SOCKET_ERROR" when both bind and link processses run. I get an error with the network linking process and the network process


And the program still runs, but wont use winsock becuse of these winsock errors thus making it hard to debug.

Example:

// Includes as before

API WinMain(Blah blah...)
{
string favouriteBook="Halo 2";
WSADATA WsaDat;
SOCKET Socket0;
SOCKADDR_IN SockAddr;


/// Blah blah...


//Error code

if(bind(Socket0, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
*closeServer=TRUE;
MessageBox(NULL, "The networking linking process has failed\n\n\t", "Error", MB_OK|MB_ICONERROR);
} // I get this error




//Error code

if(listen(Socket0, 1) == SOCKET_ERROR)
{
*closeServer=TRUE;
MessageBox(NULL, "The networking listening process has failed\n\n\t", "Error", MB_OK|MB_ICONERROR);
} //tHIS TOO

// Extra message anylizing code
return 0;
}

Remember that this works exactly as it should but crashes for some dang reason when a simple string variable is declared, no matter how it is declared or initialize or used the positioned

Share this post


Link to post
Share on other sites
There's nothing wrong in this code by itself.

But the reason you get crashes has nothing to do with string itself. Declaring a string changes the layout of variables in memory, and exposes a different issue.

Somewhere else in your code you have a buffer overrun, write to an unitialized pointer or a dangling pointer or some similar problem.

Also, if bind fails, the code doesn't seem to exit but simply proceeds, you also don't close the socket, so you get a lot of undefined behaviour after that.

Why does bind fail? Try putting WSAGetLastError() in there to find out the reason.

Also, what is *closeServer pointing to, or does it need to be a pointer? Are all of your references initialized?

Did you succesfully create the socket in the first place, and called WSAStartup?

Share this post


Link to post
Share on other sites
Yes, the socket creation does work. It normally works and does not work ONLY when I declare a string variable

And, thank you very much for the support!! I appreciate it!!!

And and, how silly of me!!!!!!! I forgot the winsock code: 10049 so hold tight while I go see if I can see what that means unless you find it first

Share this post


Link to post
Share on other sites
Looking up, I found WSAEADDRNOTAVAIL.

Here, my address and port code (I declare port before address)




SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons((unsigned short)2765);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = INADDR_ANY;

if(bind(Socket0, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR)
{
//Blah blah get last error blah blah quit program blah blah show the error...uh....etc
}


The problem comes when the server uses the BIND function

Edit: OH and I use winsock version 2.0 in the server

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!