Reputation: 11
I want to create a simple server using GameMaker 1.4. But it's function never works on the first try, no idea why..
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:
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?
Upvotes: 0
Views: 193
Reputation: 11
SOLUTION (From user Humayun in GM Forums): network_create_server returns an index from 0, so it can't be checked as false/true since the first index would be 0 which returns false. Explains that on the second try i was creating a second instance to the sever variable indexed as 1, and so returning true.
Upvotes: 0