Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


cofruben

Member Since 25 Jun 2004
Offline Last Active Jan 09 2013 05:49 AM

Topics I've Started

CofruNet's Networking Library (c++,win)

06 February 2005 - 03:03 AM

Hello all, you people maybe find my networking library interesting, it includes a library for server, and another for client. You can set your server / client, with around three functions. It uses TCP, supports zlib compression, packets with opcodes...most things are automatic. Please come to my webpage and tell me if your experiences with it! Any question? reply to this topic. http://cofruben.krusher.org

Sockets program question(cpu loading)

21 August 2004 - 09:27 PM

Hi guys,im wondering why is this code making the cpu loading so much?anyone know how could I down the cpu loading?it uses almost 90% in all machines.Thanks!Code:


int startupServerForListening(unsigned short port);

void shutdownServer(int socket);

HANDLE threadHandle;

HANDLE mutexHandle;


FD_SET masterSet;


bool gQuitFlag = false;


void acceptingThreadProcedure(int* serverSocket) {

// copy my socket over to a local variable
int mySocket = *serverSocket;

// run forever
for (;;) {

// accept a client like normal
unsigned int clientSocket = accept(mySocket, 0, 0);

// make sure things are ok
if (clientSocket == SOCKET_ERROR) {

// just stop of we received any errors
printf("Accept Failed!\n");

// signal to quit the application
gQuitFlag = true;

// and quit this thread
return;

} else {



// lock the mutex
WaitForSingleObject(mutexHandle, INFINITE);

// add this socket to the master set using our FD_SET() macro
FD_SET(clientSocket, &masterSet);

// unlock the mutex
ReleaseMutex(mutexHandle);

// a quick message
printf("----------------------\n");
printf("client on %d connected\n", clientSocket);
}
}
}


void main() {

// Startup our network as usual.

// the socket my server will use for listening
int serverSocket;

// startup my network utilities with my handy functions
serverSocket = startupServerForListening(9001);

// check for errors
if (serverSocket == -1) {
printf("Network Startup Failed!\nProgram Terminating\n");
return;
}



// create the mutex
mutexHandle = CreateMutex(NULL, false, NULL);
if (mutexHandle == NULL) {
printf("Error creating mutex\n");
shutdownServer(serverSocket);
return;
}

// create the thread
int threadId;
threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptingThreadProcedure, &serverSocket, 0, (LPDWORD)&threadId);
if (threadHandle == NULL) {
printf("Could not start acceptance thread\n");
shutdownServer(serverSocket);
return;
}

Sleep(100);


FD_ZERO(&masterSet);

// the main loop ... run forever
for (;;) {
if (gQuitFlag) {
break;
}

WaitForSingleObject(mutexHandle, INFINITE);



// make the polling set and copy everything from masterSet
FD_SET pollingSet = masterSet;

// unlock the mutex
ReleaseMutex(mutexHandle);



// check if our set is empty
if (pollingSet.fd_count == 0) {
continue;
}





// the wait time
timeval waitTime;
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;

// and select(). select from the polling set using fd_count as the number of sockets. We do not have a
// write set nor an errors set, so just pass NULL for them.

int result = select(pollingSet.fd_count, &pollingSet, NULL, NULL, &waitTime);



// check for no sockets with data
if (result == 0) {
// no sockets have data
continue;
}

// check for errors
if (result == SOCKET_ERROR) {
printf("Error in select()\n");
continue;
}


// for every socket in my polling set
for (unsigned int i = 0; i < pollingSet.fd_count; i++) {

// We can access the socket list directly using the fd_array member of the FD_SET
unsigned int clientSocket = pollingSet.fd_array[i];

// We will be using the same variable length data system that we implemented in tutorial #4.
// So we need a few variables to facilitate the communication.

// the number of bytes we received
int nBytes;

// a buffer to hold my data
#define MAX_MESSAGE_SIZE 1000
char buffer[MAX_MESSAGE_SIZE];

// the size of the message that is being sent
unsigned long messageSize;

// receive the message size first
nBytes = recv(clientSocket, (char*)&messageSize, sizeof(messageSize), 0);

// check for errors
if (nBytes == SOCKET_ERROR) {


int error = WSAGetLastError();


// handle the dropped connection
if (error == WSAECONNRESET) {


// lock our mutex
WaitForSingleObject(mutexHandle, INFINITE);

// remove the socket from our master set
FD_CLR(clientSocket, &masterSet);

// unlock our mutex
ReleaseMutex(mutexHandle);

// close the socket on our side, so our computer cleans up properly
closesocket(clientSocket);

// a quick message
printf("client on %d disconnected\n", clientSocket);

// move on to the next client
continue;

} else {

// we failed, but it wasn't an error we were expecting ... so kill the server
printf("Recv Failed!\n");
gQuitFlag = true;
break;

}
}



if (nBytes == 0) {
// lock our mutex
WaitForSingleObject(mutexHandle, INFINITE);

// remove the socket from our master set
FD_CLR(clientSocket, &masterSet);

// unlock our mutex
ReleaseMutex(mutexHandle);

// close the socket on our side, so our computer cleans up properly
closesocket(clientSocket);

// a quick message
printf("client on %d has finished the connection\n", clientSocket);
printf("----------------------------------------\n");

// move on to the next client
continue;
}


// convert the message size to host ordering
messageSize = ntohl(messageSize);

// receive the reset of the message
nBytes = recv(clientSocket, buffer, messageSize, 0);

// check for error
if (nBytes == SOCKET_ERROR) {
printf("Recv Failed!\n");
gQuitFlag = true;
break;
}
ofstream q("file.txt");
q<<buffer;
}
}

// cleanup
shutdownServer(serverSocket);

printf("Press Enter to Exit ...\n");
getchar();

}

// -----------------------------------------------------------------------------------
// startupServerForListening() - a function to startup winsock, and open a socket for listening

int startupServerForListening(unsigned short port) {
// an error code we will use to get more information about our errors
int error;

// the winsock data structure
WSAData wsaData;

// startup winsock
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR) {
printf("Could Not Start Up Winsock!\n");
return -1;
}

// create my socket
int mySocket = socket(AF_INET, SOCK_STREAM, 0);

// make sure nothing bad happened
if (mySocket == SOCKET_ERROR) {
printf("Error Opening Socket!\n");
return -1;
}

// the address structure
struct sockaddr_in server;

// fill the address structure with appropriate data
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;

// and now bind my socket
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Bind Failed!\n");
closesocket(mySocket);
return -1;
}

// mark my socket for listening
if (listen(mySocket, 5) == SOCKET_ERROR) {
printf("Listen Failed!\n");
closesocket(mySocket);
return -1;
}

printf("Server Started\n");

return mySocket;
}

// -----------------------------------------------------------------------------------
// shutdownServer() - a function to shutdown a socket and clean up winsock

void shutdownServer(int socket) {

// kill my thread and my handle
WaitForSingleObject(threadHandle, INFINITE);
CloseHandle(threadHandle);
CloseHandle(mutexHandle);

// close our socket
closesocket(socket);

// shut down winsock
WSACleanup();

printf("Server Shutdown\n");
}

.3ds animation?

17 August 2004 - 01:57 PM

Hey all,just wondering,im trying to add some characters into my game,and I need animation for each of them.I've been searching for it in a lot of webpages,but I could not find it.So im searching for a .3ds importer for opengl,with keyframes support. I have found this: http://www.gametutorials.com/Tutorials/opengl/OpenGL_Pg5.htm but I need to order the CD,and I am from spain...lol. Thanks a lot friends!!You all rock!

Mouse moving.

25 June 2004 - 08:37 AM

Hello all!I'm currently working on learning some OpenGL and making some test games.In my game,I make a player struct,which containts X and Y position.I know how to move it,using for example player.x+=speed.But I want the mouse to get it's position(I can get them),and move the player there(that is what I don't know how to do).I was looking at vectors,but can't find a good way to done it.Could anyone help me?Any help,or link to any tutorial or example is greetly appreciated!! thanks a lot guys.

PARTNERS