Showing posts with label socket. Show all posts
Showing posts with label socket. Show all posts

Wednesday, January 28, 2009

tcpconnect()

tcpconnect()

This function creates a TCP socket. A socket is a combination of an IP and a Port. It is like the house address(IP) and the door(port).

Argument 0:
Argument0 is the IP as a string. To connect to your self remember that you can use 127.0.0.1(This is what you will probably be doing until you get some more stuff programmed.) Don’t forget quotes around it!

Argument 1:
Argument 1 is the port. A port number is can be anything from 1024 to 49151

Argument 2:
Argument 2 is for blocking mode. It has 3 options
0 is for blocking mode
1 is for non-blocking mode
2 is for non blocking and non freezing
(according to 39ster)

After it creates the socket it will return the id if it fails it will return a negative number. So when using this particular function it is good to set it to a variable so you can check if it is negative and so you can use it again later.

The tcpconnect() script in action looks like this:

socket=tcpconnect(‘127.0.0.1’, 1337, 1) //create the socket and store it to a variable

if socket<=0{ //if it is negative that means it errored
show_message(“Could not connect”) //so tell them you couldn’t connect
exit //then exit so it doesn’t keep running the script
}

//Since it didn’t error we can continue with the code here. (If it did error it would have exited this script by now.)

Tuesday, January 27, 2009

Putting it together...

I have been using word pictures to try and help you understand the internet terms. This post will be the culmination of them all. First, a little refresher:

An IP is like your computer's address
A port is like a door
A TCP connection is like a mail system
A packet is like a piece of mail.
A socket is a combination of both an IP and a port

So first, your server will open up a door(port) next your client will open a door(port) Then the client will attempt to 'mail' (Send using TCP) a piece of information(a packet) to the server's 'house'(IP address) and in the open door(port) the combination of the house and door is a socket.

That is the basic gist of online communications. You will be sending packets back and forth between a server and a client. These packets can contain many things from a player's x and y position to a chat message.

Socket

A socket is basically an IP and a port used together(If you do not know what those are look at the older posts). An IP is like a home adress and a Port is like a door, when combined you know were you are going and how to get in.