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) );
}
}