Showing posts with label tcplisten. Show all posts
Showing posts with label tcplisten. Show all posts

Friday, January 30, 2009

tcpaccept()

This function accepts any incoming connections on a certain listening socket. You must use the tcplisten() script before this one so there is an open listening socket. This script responds to the tcpconnect() script. (The client would use the tcpconnect script and when it got to the server it would be accepted with this script)

Argumento:
This is the socket to listen on. The best way to set this would be to use the tcplisten() script and then set it to a variable and then use that for this argument. Like this:
socket = tcplisten(1337,8,1) //creates the socket
tcpaccept(socket,1) //tells it to accept connections on this socket

Argument1:
Blocking-0/Non Blocking-1 mode.

This function returns the id of the socket. So it is good to set this to a variable for use later. If it doesn't get a connection it will return a negative number. This script should most likely be used
in the step event in a server object so it will constantly be checking if it needs to accept a connection from the client.

Remember in order for this script to work you must already have created a socket using tcplisten().

In actual code the script works like this:

clientSocket=tcpaccept(socket,1) // accept the connections
if clientSocket<=0{ //if it errored
exit //exit this script
}

//if it didn't you can continue scripting here.

Thursday, January 29, 2009

tcpconnect/tcplisten

We have just learned the tcpconnect() and tcplisten() functions. These functions are used in conjunction. On the server you would use the tcplisten() function to create a listening socket and with the client you would use the tcpconnect() function to connect to the socket that the server has opened. Next we will learn to accept the tcp connection using the tcpaccept() script. I just wanted to throw that little tidbit of information in there so you can see where I am going with this.

tcplisten()

This function creates a listening socket to monitor a port for incoming connections. You use this to open up a 'door' for the messages to come to. This function has 3 arguments.

Argument 0:
(A number)The port to listen on. As stated before a port number can be anything from 1024 to 49151

Argument 1:
(A number) This is the max number of people allowed to be connected but not accepted.

Argument 2:
(true or false)Blocking or Non Blocking mode. (0=Blocking, 1=Non-Blocking) This affects the tcpaccept() script. Remember that false=0 and true=1

This function returns the id of the socket or a negative number if it had an error. So you may want to attach a variable to it to check its status easier. This script in action would be used to start a listening socket to accept connections from players. In actual scripting it would be used like this:

listen=tcplisten(1337,8,true) //opens a listening socket and gives it the id 'listen'
if listen<=0{ //if it errors(returns a negative number)
show_message("Could not create listening socket") //tell them
exit //exit the script
}

//the script can continue on here because if it errored it would have been stopped by the above code(exit)