Sign in to follow this  
MirekMalysz

server freeze

Recommended Posts

MirekMalysz    122
im very new to c++ network programming, i have a problem that wen i start my server...my game seems to crash, very basic, ive looked and tried many different tutorials on the internet, but all of them seem to make my game crash. im very familiar with vb winsock but this is wayy different then vb.. anyways heres my code, if anyone could plz point out an error, it would be great thx in advance :)
// IN MY WINMAIN
	WORD sockVersion;
	WSADATA wsaData;
	int nret;
	sockVersion = MAKEWORD(1, 1);
	// Initialize Winsock as before
	WSAStartup(sockVersion, &wsaData);
		LPHOSTENT hostEntry;
		in_addr iaHost;
		SOCKADDR_IN serverInfo;
if (keys[VK_HOME]){
		SOCKET listeningSocket;
		listeningSocket = socket(AF_INET,		// Go over TCP/IP
						SOCK_STREAM,   	// This is a stream-oriented socket
						IPPROTO_TCP);		// Use TCP rather than UDP
			if (listeningSocket == INVALID_SOCKET) {
					nret = WSAGetLastError();		// Get a more detailed error
					ReportError(nret, "socket()");		// Report the error with our custom function

					WSACleanup();				// Shutdown Winsock
					return NETWORK_ERROR;			// Return an error value
			}	
				// Use a SOCKADDR_IN struct to fill in address information
				SOCKADDR_IN serverInfo;

				serverInfo.sin_family = AF_INET;
				serverInfo.sin_addr.s_addr = INADDR_ANY;	// Since this socket is listening for connections,
				// any local address will do
				serverInfo.sin_port = htons(5001);		// Convert integer 8888 to network-byte order
				// and insert into the port field
				nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
				if (nret == SOCKET_ERROR) {
				nret = WSAGetLastError();
				ReportError(nret, "bind()");

				WSACleanup();
				return NETWORK_ERROR;
				}
				nret = listen(listeningSocket, 10);
				if (nret == SOCKET_ERROR) {
				nret = WSAGetLastError();
				ReportError(nret, "listen()");

				WSACleanup();
				return NETWORK_ERROR;
				}
					// Wait for a client
				SOCKET theClient;

				theClient = accept(listeningSocket,
							NULL,			// Address of a sockaddr structure (see explanation below)
							NULL);			// Address of a variable containing size of sockaddr struct

				if (theClient == INVALID_SOCKET) {
						nret = WSAGetLastError();
						ReportError(nret, "accept()");

						WSACleanup();
						return NETWORK_ERROR;
					}


					// Send and receive from the client, and finally,
					closesocket(theClient);
					closesocket(listeningSocket);


					// Shutdown Winsock
					WSACleanup();
					return NETWORK_OK;

				}

Share this post


Link to post
Share on other sites
hplus0603    11347
When you post questions about programs crashing, it helps if you say where it crashes, and what the debugger says the various variables are at that point. Even better is if you step through the program line by line in the debugger, and view the various variable values.

Btw: in your code, you initialize WinSock even if the home key isn't held. You also define a second instance of "serverInfo" that shadows the first version.

Share this post


Link to post
Share on other sites
JY    289
Do you mean _crash_ or _hang_? It seems to me that you are not making the listening socket asynchronous. This means that when you call accept it will block the thread until a connection comes in on that socket.

Try making the socket asynchronous, using setsockopt - or use the select function to determine first whether a connection is waiting before you attempt to accept it. As an alternative you can also use WSAAsyncSelect - which will post a message to your specified window when a connection is present.

Hope this helps.

Share this post


Link to post
Share on other sites
RuneSword2    122
You're ussing non assyncronous sockets, in this case the server blocs every time it runs accept or recv. If you don't want to use asyncronous sockets the better way is to use threads. I suggest you to set up one thread to receive the incomming clients and one more for every client.

Share this post


Link to post
Share on other sites
MirekMalysz    122
hey thx for all ur reply's. i have fixed the problem , it was something to do with the code :


WSAAsyncSelect (listeningSocket,hWnd,WM_ONSOCKET,(FD_CLOSE | FD_CONNECT | FD_READ));

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this