Re: Looking for someone to give a quick one on one tutorial
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_h
andle = 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()<
br />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 s
erver_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()
wind
ow_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 mess
age
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.
_______________________________________________ Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector