Legacy GM Networking issue

G

Groove

Guest
I want to create a simple server. But it's function never works on the first try, no idea why..

Code:
var networkType = network_socket_tcp;
var port = 50000;
var maxClients = 32;
var bufferSize = 1024;
var bufferType = buffer_fixed;
var bufferAlign = 1;

server = network_create_server(networkType, port, maxClients);

global.buffer = buffer_create(bufferSize,bufferType,bufferAlign);
sockets = ds_list_create();

if server s_msg("Server is up! Port: " + string(port));
else s_msg("Server is down!");
The message will be "Server down". No matter the port neither the net type.
When I try the same code with a loop, it works on the second try.
Like this:

Code:
var networkType = network_socket_tcp;
var port = 50000;
var maxClients = 32;
var bufferSize = 1024;
var bufferType = buffer_fixed;
var bufferAlign = 1;

server = network_create_server(networkType, port, maxClients);

//////////////////////////////////////////////////////

while (!server && port < 65535){
   port++;
   server = network_create_server(networkType, port, maxClients);
}

//////////////////////////////////////////////////////

global.buffer = buffer_create(bufferSize,bufferType,bufferAlign);
sockets = ds_list_create();

if server s_msg("Server is up! Port: " + string(port));
else s_msg("Server is down!");
Here the message will result on "Server up! Port: 50001".
As I said, no matter the port.. it could be set to 50001 at the beginning resulting on create the server to 50002.
Could someone point my mistake?
 

Humayun

Member
network_create_server return index of the server and mostly indexing starts with 0, so your server variable value is 0 and actually your server is running but you check it by "if server" it returns false because your server value us 0 you should check using "if server < 0" it should fix your problem BTW about loop it works on second try because then you also create a new server at 50001 port and now network_create_server returns 1 change !server to server < 0 it will work too
 
G

Groove

Guest
It works fine now, but that must to be " if server >= 0 ". As you said the first index would be 0.
THANK YOU!
 
Top