[TUT] Introduction to sockets
Introduction
This tutorial is meant for beginner scripters that know how to use the set_task natives and explains basic things about sockets. After that it gives a simple example on how to do a version checker. What is a socket? I will give the definition that is provided on wikipedia. In computer networking, an Internet socket or network socket is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the Internet. A socket address is the combination of an IP address (the location of the computer) and a port (which is mapped to the application program process) into a single identity, much like one end of a telephone connection is the combination of a phone number and a particular extension. The Internet Protocol is responsible for addressing hosts and routing datagrams (packets) from a source host to the destination host across one or more IP networks. For this purpose the Internet Protocol defines an addressing system that has two functions. Addresses identify hosts and provide a logical location service. If we summarize what above has bees stated. We can conclude that through sockets (a connection) we can transmit to another host (person) using an internet protocol (a known language) information for a specific port (application). Note: The terms that been placed between () and have been bolded are placed there to enable users to compare a socket with a human dialogue for easy understanding. What are the basic functions of sockets? I will also give the list that is posted on wikipedia since it is a good one. Code:
socket() creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it. Well this depends on the module that you would like to use. There are 2 modules here on AlliedMods: 1. Sockets - this is the default module that comes with the AmxModX package and has some of the functions that are presented above. 2. Sockets_HZ - this is a custom module that is the same as the one before but has an advantage of offering more of the functions that are posted above. The module that we will talk about will be the basic Sockets module since we want to focus only on the basics. Info about the functions/defines that come in the socket module. First of all we take a look at the include since it will provide us with the most information that we need. At first we look at the defines: PHP Code:
What is the difference between them? TCP - stands for Transmission Control Protocol and offers complete and correct data transmission between the end devices but the only problem of it is that if the data between the end devices is damaged it will be retransmitted. Because of this this protocol sometimes can be slow. UDP - stands for User Datagram Protocol and does not have control of the data integrity. Because of this is faster. If you want to know more about them you just need to access wikipedia. And now about the functions As you can see if you understand the principles and the role of everything that has been explained above you will understand the purpose and the necessity of the arguments in the functions. PHP Code:
I will now give the example on how to check if a plugin has a new version to see how all of these things that have been explained above come together in the end. First of all we need to think on the things that we want to do. In this example I want to check the forums from AlliedModders to see if my Block Wallhack plugin is up to date, in order to do that we need the plugin posted somewhere on the forums because we cannot check something that does not exist. The plugin has the following link: http://forums.alliedmods.net/showthread.php?t=100886 The next step is changing the link into a socket type addressing schematic. These are the things we need:
The server address (host name) is already specified in the link of the plugin: http://forums.alliedmods.net/showthread.php?t=100886. I have marked it with the orange color. Now what about the port, this is also specified in the link of the plugin http://forums.alliedmods.net/showthread.php?t=100886. The port is HTTP (Hyper Text Transfer Protocol), if we search the net we can see that the value of this port is 80 for server access. Now what about the protocol? Well this is something that you need to search on the net to see what type of protocol is used with HTTP. In our situation TCP is the one. What about the last part of the link? http://forums.alliedmods.net/showthread.php?t=100886. This will be used to access the correct page in order to do the checks that we are interested in, you will see that in the example. So the list comes together like this:
The main parts of the plugin. This is the part where all the theory comes together in one small script. We already have all the info that we need. I will first post the functions separately and comment them and after that I will post the entire script for better understanding. First of all let's make some defines PHP Code:
PHP Code:
PHP Code:
This will be done through this: PHP Code:
Now we need to use set_tasks in order to wait for a response from the server. We need this part because the network traffic can be huge and the response from the forums might take some time. We just need to make sure that nothing happens. That is why I will use 2 tasks. One for getting the server respons and another one for closing the connection if no info has been sent. PHP Code:
First of all we check if the socket has info in it. PHP Code:
PHP Code:
Code:
HTTP/1.1 200 OK PHP Code:
Putting all together. PHP Code:
Other tuts/applications that can be useful after reading this |
Re: [TUT] Introduction to sockets
Very interesting, good job.
|
Re: [TUT] Introduction to sockets
There is no way to detect when we receive an "answer"?
|
Re: [TUT] Introduction to sockets
Other tuts/examples that can be useful -> "Sorry - no matches. Please try some different terms."
That's not the way you should post your searches :P |
Re: [TUT] Introduction to sockets
Quote:
If so, not. Just with the set_task way. Quote:
|
Re: [TUT] Introduction to sockets
I'm working on a plugin that sends a player's steamid to a server & then receives answer to check if he's banned or not & there are a few things that aren't clear to me.
1. Is it okay to use a seperate socket for every player ? 2. If not, can i send new data to a socket after i received data from an earlier request or do i need to close & reopen it every time ? 3. Is it okay to connect to multiple servers at the same time ? |
Re: [TUT] Introduction to sockets
Quote:
2. You can keep the connection open if you use TCP protocol. You may want to use UDP which is a connectionless protocol. 3. Yes. |
Re: [TUT] Introduction to sockets
Thanks Bugsy,
About #1, lets say a 32 slot full server changes map, is there a possibility that it causes lagg or something when the plugin sends data to 32 sockets at the same time ? |
Re: [TUT] Introduction to sockets
Quote:
|
Re: [TUT] Introduction to sockets
Okay, thanks
|
All times are GMT -4. The time now is 03:43. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.