// JP opened flex table

Click to See Complete Forum and Search --> : server program auto shut down


hanhao
March 11th, 2004, 02:02 AM
#include <windows.h>
#pragma comment(lib, "wsock32.lib")

#include <winsock.h>
#include <stdio.h>






#define NETWORK_ERROR -1
#define NETWORK_OK 0

void ReportError(int, const char *);


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
WORD sockVersion;
WSADATA wsaData;
int nret;
SOCKET listeningSocket;
SOCKADDR_IN serverInfo;
SOCKET theClient;

sockVersion = MAKEWORD(1, 1); // We'd like Winsock version 1.1



// We begin by initializing Winsock
WSAStartup(sockVersion, &wsaData);


// Next, create the listening socket


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


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(8888); // Convert integer 8888 to network-byte order
// and insert into the port field


// Bind the socket to our local server address
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

if (nret == SOCKET_ERROR) {
nret = WSAGetLastError();
ReportError(nret, "bind()");

WSACleanup();
return NETWORK_ERROR;
}


// Make the socket listen
nret = listen(listeningSocket, 10); // Up to 10 connections may wait at any
// one time to be accept()'ed

if (nret == SOCKET_ERROR) {
nret = WSAGetLastError();
ReportError(nret, "listen()");

WSACleanup();
return NETWORK_ERROR;
}


// Wait for a client




while(1)
{

theClient = accept(listeningSocket,
NULL, // Address of a sockaddr structure (see explanation below)
NULL); // Address of a variable containing size of sockaddr struct
MessageBox(hInst, "CONNECTED", "This program is:", MB_OK | MB_ICONINFORMATION);

if (theClient == INVALID_SOCKET) {
nret = WSAGetLastError();
ReportError(nret, "accept()");
WSACleanup();
return NETWORK_ERROR;
}
else
{

char buffer[256]; // On the stack
// char *buffer = new char[256]; // or on the heap
MessageBox(hInst, "CONNECTED", "This program is:", MB_OK | MB_ICONINFORMATION);
nret = recv(theClient,
buffer,
256, // Complete size of buffer
0);

if (nret == SOCKET_ERROR) {// Get a specific code// Handle accordingly
return NETWORK_ERROR; }

else if (buffer == "check_network")
{
MessageBox(hInst, "checkNW", "This program is:", MB_OK | MB_ICONINFORMATION);
;}
else if (buffer == "shutdown")
{
MessageBox(hInst, "shutdown", "This program is:", MB_OK | MB_ICONINFORMATION);
;}
else if (buffer == "restart")
{
MessageBox(hInst, "restartt", "This program is:", MB_OK | MB_ICONINFORMATION);
;}
else if (buffer == "lower_volume")
{
MessageBox(hInst, "lowervol", "This program is:", MB_OK | MB_ICONINFORMATION);
;}
// listeningSocket = theClient;
}
;
}



//delete [] buffer; // Manipulate buffer, then delete if and only if
// buffer was allocated on heap





// nret contains the number of bytes received


// listeningSocket = theClient;

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


// Shutdown Winsock
WSACleanup();
return NETWORK_OK;
}





void ReportError(int errorCode, const char *whichFunc) {
char errorMsg[92]; // Declare a buffer to hold
// the generated error message

ZeroMemory(errorMsg, 92); // Automatically NULL-terminate the string

// The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);

MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
}




here's a prototype of my code
basically it's a server allowing a custom client(that i am going to create soon) to connect to it.
the problem is that when a client connects, and if the client shuts down instead of properly closing socket and disconnecting, the whole server program shuts down

is there anyway to prevent the server program from shutting down? I suspect it's due to the error checking codes but i cant put my finger on it

copied most of the codes from
johnie's winsock tutorial btw

Mathew Joy
March 11th, 2004, 05:05 AM
I assume that you are building a server and more than one client is connecting. Each client interacting with the server. Your code is a result of trying to pull out a server code without even a rudimentary knowledge. You have a loop that accept()s that is fine. The rest of the code is problematic. After one recv what happens to the socket? The code is written to exit from the main thread (Winmain) in case of an error or socket closure. The how do you expect the server to run after the client is disconnected?

A simple design is to use a loop, in the main thread, just to accept the clients. For each client, you should spawn a thread and that thread handles the client 'from the cradle to the grave'. Once the client is disconnected you must close the socket before you exit the thread. Again I assume that you are building a simple, low load server

shivakumarthota
March 11th, 2004, 09:00 AM
The server socket should remain active.
Try after commenting this line from the code :closesocket(listeningSocket);

And then try to connect the client and try.

defiler_z
March 11th, 2004, 04:21 PM
I haven't looked at your code but i am writing something like that and i'll tell you what i am doing: i use my own class for connection(that i've write, wrapping the standard functions send(), connect(), etc.) and if there is an error i throw an exeption. I put every user that connects to the system in separate thread and if this user close the connection(no matter whether this is normaly or because of error-this is in try catch block) i clear all reserved memory for this user(and exit from the user thread, not from the main one). So use multithreading.
I hope that i am not wrong with the subject:) If i am :rolleyes: sorry

Mathew Joy
March 11th, 2004, 11:29 PM
Wrong points shiva,
Originally posted by shivakumarthota
The server socket should remain active.
Try after commenting this line from the code :closesocket(listeningSocket); Server socket should remain active only as long as the server itself is active. He is closing the server socket just before exiting the main thread which is correct.
And then try to connect the client and try. The problem is, he is exiting winmain as soon as an error occurs. Next is he is accepting on the same socket without closing other sockets, which is a resource leak. Thirdly, to handle multiple clients you need multiple threads (of-course in the most simple design). He'll need to rewrite the whole code after studing about winsock. Well, I think that is the solution.

//JP added flex table