Okay, since i'm making a mud client, i expect some people to be asinine and try to connect to a server while they have no internet connection. Here's my problem. In my check to see if the socket returned INVALID_SOCKET, I always get an error from windows and my program exits. I was wondering if anyone could tell me why? (it's the bool open_socket function)
/*
Nova Client
Created: June 22, 2007
Last Updated: June 22, 2007
Description: This file contains all the socket functions this client uses
*/
char buffer_in[512]; //these two are used too much to be private
std::string out;
private:
int port;
int datasize;
int error_ret;
LPHOSTENT LPHostEnt;
WSADATA WsaData;
WORD WVer;
std::string address;
SOCKET Nova;
struct sockaddr_in sin;
struct hostent host;
};
ahoodin
June 25th, 2007, 08:32 AM
Use WSAGetLastError() to check for WSAENOTSOCK and other such errors.
HTH,
MikeAThon
June 25th, 2007, 11:46 AM
What does the "error from windows" say before your program exits?
Try running your program in the debugger, and single-step throught the open_sock() function.
Incidentally, you should call WSAStartup() only once per program. It normally doesn't hurt to call it more often, so long as there is a matching call to WSACleanup(). Your program would apparently call it thousands of times, however, and I am not certain of the affect.
Mike
ahoodin
June 25th, 2007, 04:47 PM
Yep I agree here, he definitely shouldnt be requesting Winsock dll in the same function where he opens a socket. That could be a serious problem especially where you never call WSACleanup().
ccubed
June 25th, 2007, 05:48 PM
Okay, i'll try seperating the WSAStartup and socket calls. Also, i call WSACleanup in my main.cpp file when the program is exiting. But i've never really had a problem with the WSAStartup, it skips the check for invalid socket apparently and tries to go straight to gethostbyname, which of course, causes HUGE problems when there's no internet connection. However, i'll see if seperating the two will work.
ccubed
June 25th, 2007, 10:35 PM
Okay, this gonna be a long post but at least i now know what is wrong. It is definately the socket check and it definately fails inside the open_socket function. I've included updated code.
//if datasize is somehow greater then what we expected, treat it as new data. Should always be lower.
if ( datasize > esize ){
#ifdef DEBUG
std::cout << "Debug:MISC WARN:Datasize is bigger then what we SHOULD have received. DATASIZE " << datasize << ". ESIZE " << esize << ". treating it as new data we should get." << std::endl;
#endif
esize = datasize;
}
}
#ifdef DEBUG
std::cout << "#debug: Received a total of " << datasize << " bytes in " << times << " recv() processes." << std::endl;
#endif
return true;
}
else if( datasize == 0 ){
#ifdef DEBUG
std::cout << "#debug: No data to receive" << std::endl;
#endif
std::cout << "Error Connecting to " << address << " on port " << port << "." << std::endl;
return false;
}
return true;
}
void close_Nsock(){
closesocket(Nova);
}
//these two need to be able to be accessed by non members of novasock
char buffer_in[512];
std::string out;
std::string last_cmd;//last command sent to server
private:
int port;
int datasize;
int error_ret;
LPHOSTENT LPHostEnt;
WSADATA WsaData;
WORD WVer;
std::string address;
SOCKET Nova;
int MXBUFA;
struct sockaddr_in sin;
};
The part in bold is DEFINATELY where the problem is.
MikeAThon
June 26th, 2007, 07:22 AM
Run the program in the debugger, and single-step through the open_socket() function to find the line that is causing the page fault.
Mike
ccubed
June 26th, 2007, 10:31 PM
well, it's a mud client. I can't run the debugger though. for some reason, Dev c++ doesn't stop to get the input from me. Now my program returns an access violation from when i try to get an address from LPHostEnt that contains nothing. it also doesn't say LPHostEnt is invalid when i try to gethostbyname.
ahoodin
June 27th, 2007, 07:08 AM
Is the debugging information in the executible, or are you running a "release mode" executible?
In MSVC, there are different preprocessor defines that you set in the project. You will have to look up the method in dev.
With g++ there is -g to link in gnu debug info.
You *could* also pepper the program with couts to find out which was the last line to run.
MikeAThon
June 27th, 2007, 11:49 AM
See http://www.codeguru.com/forum/showthread.php?t=427239 for a related post
ahoodin
June 27th, 2007, 12:36 PM
The above URL appears to be a circular reference Mike.
Try some of these:
http://csjava.occ.cccd.edu/~gilberts/devcpp5/
Now just derive this class as private class novosock : private UseWinsock { and it will automatically initialise and clean up as you go. Also you no longer have to worry about including the library as the #pragma comment does it all for you.
G.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.