2017-01-09 12:48:23

Hello!
Not working server in BGT!
Please help me!
This is code server:
network host;
network_event event;
void main()
{
show_game_window("Server");
string port=input_box("Port", "What port should the server be on?","4321");
host.setup_server(string_to_number(port),10,100);
while(true)
{
event=host.request();
if(event.type==event_receive)
{
string[] msg=string_split(event.message,"|",true);
if(msg[0]=="connect")
host.send_reliable(0,event.message,0);
if(msg[0]=="who")
{
string whoms;
uint[] peers=host.get_peer_list();
if(peers.length>1) whoms=peers.length+" players online";
if(peers.length==1) whoms="No players online";
host.send_reliable(event.peer_id,"who|"+whoms,0);
}
if(msg[0]=="dc")
{
host.send_reliable(0,"dc|"+msg[1],0);
host.disconnect_peer(event.peer_id);
}
}
if(key_pressed(KEY_ESCAPE)) {
host.destroy();
exit();
wait(5);
}
wait(5);
}
}
and this is code client:
#include "form.bgt"
#include "includes/logo.bgt"
#include "includes/m.bgt"
#include "includes/menu.bgt"
#include "includes/dlg.bgt"
#include "sound_pool.bgt"
string[] chat_history;
int chat_position;
string name="No name";
bool connecting;
network net;
network_event event;
void main()
{
show_game_window("Endless war");
set_sound_decryption_key("kids********", true);
set_sound_storage("sounds.dat");
wait(1000);
install_keyhook();
net.setup_client(10, 100);
mainmenu();
}
void test()
{
dlgplay("test.wav");
}
void set_name()
{
name=input_box("Enter name","Please enter youre name: ","");
mainmenu();
}
void game()
{
net.connect("localhost", 4321);
connecting=false;
while(true)
{
event=net.request();
if(connecting==false)
{
net.send_reliable(0,"connect|"+name,0);
connecting=true;
}
if(key_pressed(KEY_F1))
{
double time=net.get_peer_average_round_trip_time(0);
speak("The ping took "+time+" milliseconds.");
}
if(key_down(KEY_LSHIFT) or key_down(KEY_RSHIFT) and key_pressed(KEY_SLASH))
net.send_reliable(0,"/who",0);
if(key_pressed(KEY_LBRACKET))
{
chat_position--;
if(chat_position<0)
chat_position=0;
speak(chat_history[chat_position]);
}
if(key_pressed(KEY_RBRACKET))
{
chat_position++;
if(chat_position>=chat_history.length)
chat_position=(chat_history.length-1);
speak(chat_history[chat_position]);
}
if(key_pressed(KEY_ESCAPE))
{
speak("Disconnecting");
net.send_reliable(0,"dc|"+name,0);
net.destroy();
mainmenu();
}
if(event.type==event_disconnect)
{
net.destroy();
mainmenu();
}
if(event.type==event_receive)
{
string[] msg=string_split(event.message,"|",true);
if(msg[0]=="chat")
{
string chmessage=msg[1]+" say: "+msg[2];
speak(chmessage);
chat_history.insert_last(chmessage);
}
if(msg[1]=="info")
speak(msg[1]);
if(msg[0]=="dc")
speak(msg[1]+" offline");
if(msg[0]=="connect")
speak(msg[1]+" online");
}
}
}
void chatbox()
{
audio_form form;
form.set_output_mode(active_sr());
form.create_window("Chat",false);
int chat=form.create_input_box("&chat");
int ok=form.create_button("&ok",true);
int cancel=form.create_button("&cancel",false,true);
form.focus(chat);
while(true)
{
wait(1);
form.monitor();
if(form.is_pressed(cancel))
{
speak("Canceled");
return;
}
if(form.is_pressed(ok))
{
if(form.get_text(chat)!="")
{
net.send_reliable(0,form.get_text(chat),0);
return;
}
else
{
speak("Canceled");
return;
}
}
}
}
Please help me!

2017-01-10 10:23:50

Please help me!

2017-01-10 23:18:41 (edited by Rastislav Kish 2017-01-10 23:26:39)

Hello,
I guess the problem is, that you didn't use event_connect. For that reason, what happens is:
You start server, server is running. You start game, game is running. you enter multiplayer section, so connect method of network object is called. This object is in other thread, so computer don't must to wait until connection is done, he continuing in the main code. In time about 0 milliseconds computer starts cycle while(true), and see, that he must to set connecting to true, because it is false. So, computer set this variable and send message to the server, that connection is made. Hovever, this message will never arrive to the server, because connection is not established yet. Time needed to go from one command to an other is much smaller than time needed to establish connection, especially if there is need to send data like connected peers, ports, ip addresses etc. I don't know what bgt is sending when establishing connection, but that is not important, time is still larger, so message will not go out.
I will send a part of connection algorithm from my game Audioslenderman in next post, you can inspire from it.
Hope this helps.
Best regards

Rastislav

2017-01-10 23:24:06

Hello again,
I am sending the code, it is only connection, things like chat or other game elements are in next cycle, which I haven't included here, because it is not needed.

void connect() {
connected=false;
host.connect(address,port);
timer connecttimer;
inttemp=1500;
while (connecttimer.elapsed<inttemp) {
event=host.request();
if (event.type==event_connect) {
connected=true;
break;
}
}
if (connected) {
host.send_reliable(1,"name;"+name,1);
} else {
alert("connection fail","");
start_menu();
}
//***other code***
}

Happy coding

Rastislav