net code fun
Displaying 1-3 of 3 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
lynerd

I am a little bored of working on my game and decided to try making a program that makes use of the netcode functions.

I can get connections to work and such, but if I type in an ip address the program goes to no respond. How long should an error message take? How long does Connect() take to stop trying to connect? The program works if I use localhost.

I want to thank the guy who made the pong game in the file section or I wouldn't of even got this far.

I am using the latest verge from the ftp if that helps any.

**code updated to show that I have fixed problems
**it also checks how long the client has been idle and gives them time to keep sending messages

client code:

#define BOOL int
#define TRUE 1
#define FALSE 0


string server = "localhost";

int connection;


void AutoExec()
{

BOOL done = FALSE;
int time;

string message;

SetAppName("CLIENT"); //if you run multiple clients, shows which is which

ConnectToServer();

while( !done )

{

SocketSendString( connection, str(systemtime / 100) );

ShowPage();

if(b1)
done = TRUE;

RefreashScreen();

}

}


void ConnectToServer()

{

int time;
string message;

connection = Connect(server);

if(!connection)
exit("couldn't connect!");

time = systemtime;

while( !SocketHasData( connection ) )

{
updatecontrols();

if(systemtime - time > 20)
exit("connection timed out");
}

if( SocketHasData( connection ) )

{
message = SocketGetString(connection);

log(message);

if ( !strcmp(message, "full") )
Exit("server is full");

}

}


void RefreashScreen()

{

RectFill( 0, 0, ImageWidth(screen), ImageHeight(screen), rgb( 0, 0, 0), screen);

ShowPage();

}


server code: you just need to run the vserver_RunServer() function in your game loop for it to work

#define VSERVER_CLIENTS_MAX 5 //max amount of connections to server
#define VSERVER_MESSAGES_MAX 100

#define VSERVER_BOOL int
#define VSERVER_TRUE 1
#define VSERVER_FALSE 0

#define VSERVER_DISCONNECT_TIME 2000 //time to wait before disconnecting an inactive client

struct vserver_socket
{

VSERVER_BOOL active; //is it active?

int id; //socket connection to server
int old_time; //last know connection or data recieved
int new_time;
int idle, idle_start;

};


vserver_socket vserver_clients[VSERVER_CLIENTS_MAX];
int vserver_clients_connected = 0; //how many clients are connected

string vserver_client_message[VSERVER_MESSAGES_MAX];


void vserver_RunServer()

{

int i;

vserver_CheckForConnections();

for(i = 0; i < VSERVER_CLIENTS_MAX; i++)

{

if( vserver_clients[i].active )

{

vserver_ParseMessage(i);

if( vserver_ClientIdleCheck(i) )
vserver_ClientDisconnect(i);

}

}

}


//adds a client to the connections list, if server full than it sends a message
void vserver_CheckForConnections()

{

int connection, socket_open;

connection = GetConnection();

if(connection)

{

if(vserver_clients_connected == VSERVER_CLIENTS_MAX)

{
SocketSendString( connection, "full" );
SocketClose(connection);

Log("sent server full message on connection attempt");
}
else

{

socket_open = vserver_GetFreeSocket();

vserver_clients[socket_open].id = connection;
vserver_clients[socket_open].active = VSERVER_TRUE;
vserver_clients[socket_open].old_time = 0;
vserver_clients[socket_open].new_time = 0;
vserver_clients[socket_open].idle = 0;
vserver_clients[socket_open].idle_start = 0;

vserver_clients_connected++;
Log("++ connection made at socket-" + str(socket_open) + " by client-" + str(connection) + " at time-" + str(systemtime) );

SocketSendString( connection, "connected" );
}

}

}


//used to find a free open socket when a client connects
int vserver_GetFreeSocket()
{

int i;

for (i = 0; i < VSERVER_CLIENTS_MAX; i++)
if (!vserver_clients[i].active)
return i;

}


void vserver_ParseMessage(int client)

{

string message = "";

if( vserver_clients[client].active )

{

if( SocketHasData( vserver_clients[client].id ) )

{

message = SocketGetString( vserver_clients[client].id );

vserver_clients[client].old_time = vserver_clients[client].new_time;

vserver_clients[client].new_time = val(message);

}

}

}


VSERVER_BOOL vserver_ClientIdleCheck(int client)

{

if( vserver_clients[client].active )

{

if( vserver_clients[client].old_time == vserver_clients[client].new_time)

{
vserver_clients[client].idle = systemtime - vserver_clients[client].idle_start;
}
else
{
vserver_clients[client].idle = 0;
vserver_clients[client].idle_start = systemtime;
}


if( (vserver_clients[client].idle) > VSERVER_DISCONNECT_TIME)
return VSERVER_TRUE;
else
return VSERVER_FALSE;

}

return VSERVER_FALSE;

}


void vserver_ClientDisconnect(int client)

{

if( vserver_clients[client].active )

{

vserver_clients[client].active = VSERVER_FALSE;
SocketClose( vserver_clients[client].id );
vserver_clients[client].id = 0;
vserver_clients[client].old_time = 0;
vserver_clients[client].new_time = 0;
vserver_clients[client].idle = 0;

vserver_clients_connected--;
Log("-- disconnection at socket-" + str(client) + " by client-" + str(vserver_clients[client].id) + " at time-" + str(systemtime) );

}

}

Posted on 2007-05-26 21:26:02 (last edited on 2007-05-27 01:24:18)

Kildorf

I'm not sure of this offhand, but I believe that the net functions are not very intelligent -- if Connet() can't make a connection you may find yourself in an infinite loop.

It would be great if this were fixed!

Posted on 2007-05-28 08:46:41

lynerd

Yeah, I figured out that if I let it just hang for about 15-20 seconds then it will eventually come out of the loop.
My server program is actually working out pretty well. I have it where clients can connect, send messages, and get disconnected if they stop sending messages for a time set by the admin. Starting on the client side and then I think it would be ready for a release.
It isn't meant for an MMORPG or anything, but I really don't think the netcode was meant to handle that anyways. It would work pretty well for anything single map probably. There is not login or anything, but that could be added in when the game's server code was written.

Posted on 2007-05-29 01:57:57


Displaying 1-3 of 3 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.