2015-08-15 22:04:04

Hello, so I'm currently working on several projects. I work on one till I get sick of it, then pick up another or start one up, then go back. It's not a good work ethic, but at least I'm learning as I go. LOL. I've been trying to learn to use BGT's networking object, but I'm having a bit of an issue wrapping my head around it. I just have questions that I can't seem to be able to find answers to on my own. I'd very much appreciate it if someone would take the time and sit down one on one with me on Skype and walk me through the process and help answer some questions. I'm not asking for help in coding my project, just to help get a general idea of how to have the client send packets to the server and have the server react in kind. Thanks in advance.

The universe is a rain storm. We are droplets sent to quench the problems of the world, yet we are blown sideways by the winds of distractions.

2015-08-15 23:07:31

hi sneak
I'm sorry I haven't manage packets in bgt, So I can't help you.

And I have a question. If I'm in server mode, Can I send a function as a packet in bgt?
It means like this
In stw, you will walk around, step by step
If you press up arrow, your client will send a packet to the server, telling it to increase a value that stored in int variable.
and the client will play a step sounds.
I think it is like this
send_reliable "lol", int corrdinates - 1
And if you died, The server will send a packet that tells your client to play the dying sounds.
Is it like this?
Send_reliable "rofl", play die.ogg
If I was wrong, please let me know about it.
And please note, I'm a newbie in the bgt.
Thanks

2015-08-15 23:16:16

I'm pretty sure the best way to go about what you're asking is to have the function written up in the server portion, then have the client accept the keystrokes, so you press up arrow, then the client sends that information to the server and the server is like,
if(key_pressed(KEY_UP)
{
moveForward();
}
or something. I don't know why you're asking me if I said I don't know how the networking object actually works though lol. It's all theory and speculation in my case.

The universe is a rain storm. We are droplets sent to quench the problems of the world, yet we are blown sideways by the winds of distractions.

2015-08-15 23:33:13

I thought You are better than me about bgt.
And try this idea.
I don't remember the actual code , so I will put my idea only
If you pressed up arrow, The client sends a value, and the value will be assigned in the server's variable. You can use the if statement to call moveforward function.
For example, if the variable lol is 0, the moveforward function will stay there. If the lol variable is 1, the moveforward function will be called.
But if you making an actual game, Don't do it. Maybe you should make up to 1000 variables
or much more.

2015-08-16 08:25:25

It looks like what you're asking is for an introduction to networking.

2015-08-16 12:16:04

Hello,
I want to instantly say sorry for my bad English, but I feel that I can to help you phigure this out:
I don't use BGT, but I can tell the general idea of networking.
TCP / UDP sends the packets to the client or to the server. It can send for instance a simple text, like "Hello", or something else. By splitting that text your client or the server might decide what to do.
Imagine a situation, that you need to create an application that allows you to chat with someone else. So basic idea is this one:
Someone needs to connect to the other one, so that one is connecting are called a client. Other one that accepts the connections is called a server. Server should handle with that client (it should decide whether to accept the client or just disconnect it if its isn't suitable (for example your chatting program requires a correct password to be entered if you want to start to communicate).
When server completes to check if password that was entered is right one, it accepts the client. Otherwise, it must to close the clients socket and continue to scan if someone else wants to connect to it. If client entered the right password, it should allow to start chatting. When the client types a message, server should catch it and print it on the screen for the user that client is chatting with. Also client should scan for the server's messages to catch them as well.
Lets take a look more deeply into this:
I will not write any valid code for that, but I just try to show you the basic idea:

// global constants
constant SERVER_IP_ADDR = "127.0.0.1" // just for example
constant SERVER_PORT = 12232
constant MAX_CLIENTS_AT_ONCE = 1
// create a variable for determine weather its a server or client
connection_state = "" // it is declared as a blank string for now
server_socket = -1 // variable that holds info about the server socket
client_socket = -1 // the same, but it holds the data bout the client socket
login_name = ""
login_password = ""
my_name = ""
Now imagine that we need to create a GUI for the selection, so we will make a form within the function:

function main_screen()
window_handle = create_GUI("Chatting GUI")
button1 = create_button("Create a server")
button2 = create_button("Join the server")
while 1 = 1 // Forever loop
form_messages_catcher = catch_form_messages()
if form_messages_catcher == button1 // create a server button was clicked
login("server")
else if form_messages_catcher == button2 // join server button was clicked
login("client")
else if form_messages_catcher == GUI_CLOSED // if user wants to close the GUI
exit
end if
end while // end forever loop
end function

function login(state)
delete_previous_gui()
window_handle = create_GUI("Chatting GUI")
label1 = create_label("Please type your nickname: ")
edit1 = create_edit("")
label2 = create_label("Please enter your password for connection: ")
edit2 = create_edit("")
button1 = create_button("Submit")
button2 = create_button("Cancel")
while 1 = 1
form_messages_catcher = catch_form_messages()
if form_messages_catcher == button1 // submit was clicked
my_name = read_gui_element(edit1)
login_password = read_gui_element(edit2)
delete_previous_gui()
if state == "server"
start_server()
else
join_server()
end if
else if form_messages_catcher == button2 // cancel was clicked
delete_previous_gui()
main_screen()
else if form_messages_catcher == GUI_CLOSED // if user wants to close the GUI
exit
end if
end while
end function

function start_server()
TCP_startup() // some function to start the tcp services
server_socket = TCP_listen(SERVER_IP_ADDR, SERVER_PORT, MAX_CLIENTS_AT_ONCE) // an example of function that creates a server socket
message_from_client = ""
repeat // starting a repeat loop to wait for the client
if client_socket == -1
client_socket = TCP_scan_for_clients(server_socket) // server_socket goes as a argument / parameter
else if client_socket != -1 // if someone connected. We determine that, because our client_socket ID has been changed
message_from_client = TCP_receive_message(client_socket) // we give a client_socket as an argument to determine, from which client we are getting this message (at this time we use only one client, but in the future, we will may need more)
if message_from_client != "" // if message isn't blank, then
splitted_message_from_client = string_split_to_array(message_from_client, ",") // we determine, how to split the message, at this time by commas
if splitted_message_from_client[2] == login_password // if password is correct, then
login_name = splitted_message_from_client[1]
else // if password is wrong
TCP_send_message(client_socket, "wrong password") // we send a message to the client, that entered password was wrong
TCP_close_socket(client_socket)
client_socket = -1 // reset the variable
end if
end if
end if
until client_socket != -1 // we set to do this lloop until someone is connected successfully
TCP_send_message(client_socket, my_name) // server sends it's user's nickname
chat_window() // someone gets connected, so we can show the main chatting window
end function

function join_server() // now for client
TCP_startup()
server_socket = TCP_connect(SERVER_IP_ADDR, SERVER_PORT) // we connect to the server and if it will be done successfully, we will have a server_socket
if server_socket != -1 // if we connected successfully
TCP_send_message(server_socket, my_name&","&login_password) // this way we send a data to the server to check.
message_from_server = ""
repeat
message_from_server = TCP_receive_message(server_socket)
if message_from_server != "wrong password" // if password is correct
message_from_server = ""
repeat // another loop, just to get the server's user name
message_from_server = TCP_receive_message(server_socket)
until message_from_server != ""
login_name = message_from_server
main_chat_window()
end if
until message_from_server != "" // waits till server gives an answer
else // if our connection wasn't successful
show_error_message("Error occurred when program tryed to connect to the server.")
main_screen()
end if
end function

function chat_window()
window_handle = create_gui("Chat window")
label1 = create_label("Message: ")
edit1 = create_edit("")
button1 = create_button("Send")
label2 = create_label("History: ")
read_only_text1 = create_read_only("")
while 1 = 1
form_messages_catcher = catch_form_messages()
if form_messages_catcher == button1 // send button was clicked
message = read_gui_element(edit1)
if connection_state = "server"
send_message(client_socket, message, read_only_text1)
else
send_message(server_socket, message, read_only_text1)
end if
else if form_messages_catcher == GUI_CLOSED
exit
end if
if connection_state == "server"
message_from_other_user = TCP_receive_message(client_socket)
else
message_from_other_user = TCP_receive_message(server_socket)
end if
if message_from_other_user != "" // if someone got a new message
history = read_gui_element(read_only_text1)
update_gui_element(read_only, history&"You got the new message "&message&@new_line)
end if
end while
end function

function send_message(to, message, read_only)
TCP_send_message(to, message)
history = read_gui_element(read_only)
update_gui_element(read_only, history&"You sent the message "&message&@new_line)
end func

So the basic idea is this one. If you want to have more that one client, you need to save all them within an array (it can be a dimentional one). Then you need to loop between them all to check if someone sends something or not.
I hope this helps a bit.

2015-08-16 16:46:15

hi
But my question is, can I send a function as a packet like this?
tcpsend $socket1, soundplay"lol.ogg"

If the lol.ogg is exist in the client, the client plays the lol.ogg file? or just print an error message
And if I've send a user function to the client, the client can call this function?
I saw an autoit example, I think I can send a function as a packet like I said, because in autoit example, the client just sent a function, fileopen
And the server recived the file

2015-08-16 17:42:27

No, you can't just send a function to the client or to the server. As you can see in my previously given example, you can only send some text, which can be interpreted as some sort of command. Your program should handle what the text that server sent to client means. For instance, you have a server, that sends a command to the client and then client program understands that command and does whatever it takes.
As for AutoIt it looks something like this:

;Server
TCPSend($client_socket, "play_sound|mysound1.wav")

;Client
local $message_from_server = ""
do
$message_from_server = TCPRecv($server_socket, 1024) ; we accept max 1 kilobyte of data
until $message_from_server <> ""
$message_from_server = stringSplit($message_from_server, "|") ;in this case we just split our given string into an array, that $message_from_server[0] = number, which represents how many items this new array has, $message_from_server[1] = our command, in this particular variant its "play_sound", and $message_from_server[2] = sound that should be played, so further it looks like this
switch $message_from_server[1] ; we use switch to determine which command to perform
case "play_sound"
if fileExists($message_from_server[2]) then
soundPlay($message_from_server[2])
endif
case "something_else"
do something...
endSwitch

I hope this helps.