TCP Sockets Tutorial
 
Post new topic   Reply to topic    Hawkee.com Forum Index -> mIRC Tutorials
 
KuTsuM  -  Sat Oct 08, 2005 2:52 pm Reply with quote
TCP Sockets

This tutorial requires atleast basic knowledge of mIRC. If you don't have at least that, come back when you do. Also, at the end of this tutorial will be a working IRC SocketBot that you can mess with :)


Introduction
Q. What is a socket?
A. A socket is one form of creating a two way communication link between two systems connected to the internet, using Windows' WinSock, and WinSock2 libraries.

Q. What is an IP?
A. IP stands for Internet Protocol. It's used for passing information between two systems. Each system has it's own unique number called their IP.

Q. What is TCP?
A. TCP stands for Transmission Control Protocol, and is used along with the Internet Protocol for passing packets of information.


Opening Connections with Sockets

mIRC Help wrote

/sockopen [-de] [bindip] <name> <address> <port>


The sockopen alias is used to tell mIRC to open a connection to the specified network. To do this, we need all of the following parameters:
Switches [-de] - The -d switch specifies that you have declared bindip as the address to bind to. The -e switch (new in mIRC 6.17), declares that the socket be opened over an SSL connection. These switches are optional.
Name - This parameter is the socket's name. Each open socket must have a unique name. This is used to pass information between the systems, while being able to use other sockets simultaneously.
Address - This parameter is the IP(IE: 127.0.0.1) or host(IE: www.hawkee.com) of the system you would like the socket to connect to.
Port - This parameter is the remote system's port number you wish to connect to. There are millions of port numbers, but you must connect to the one that is currently listening(For example, if you wanted to connect to a website, you'd use port number 80, if you wanted to connect to a typical IRC server, the common port is 6667). For a list of the popular ports, go to Here

When you use the sockopen alias, you must follow it with the SOCKOPEN event. This is used to send information to the remote system immediately after the connection is made. This is needed for getting authorized with the majority of all servers. For example, with IRC you need to pass the 'USER' and 'NICK'information along with respond to a 'PING' packet with a 'PONG'.
The SOCKOPEN event calls for 1 detail, and that's the sockets name. The event is read like this
Code
 


Putting it all together, It's done like this:
Code
 


Sending Information

mIRC Help wrote

/sockwrite [-tnb] <name> [numbytes] <text|%var|&binvar>


The sockwrite alias is used to send information through the socket. There are 2 parameters needed. Name and either the Text you'd like to send (this can be in a variable) or a binary variable (or binvar).
By now you should know what a variable is. If not, a variable is a string of information either publicly or privately saved by mIRC for later retrieval.
A binary variable is basically the same thing, except it is used to store a string of Binary (A computer language used to pass information between systems, consists of only 1's and 0's [heard that before? ;)]). Binary variables are generally used for uploading or downloading files such as .exe, .mp* (1,2,3,4), .dll, etc.

-n - The n switch is very important. It will tell mIRC to append a CrLf to the end of the information, if there isn't one already. CrLf tells the remote system to create a new line (Used so 2 packets will not be blended together like so:
USER information
NICK information
/*
That's how it should be, and will do with CrLF
at the end. But this is without:
*/
USER informationNICK information)
-t - The t switch specifys that the information being sent is not a binary variable. This means that something like "You &me" is not evaluated and will be treated as just text.
-b - The b switch tells mIRC that you want to set a limit in size of the packet (basically). The information that you tell it to send will be limited to the byte size you specify in the "[numbytes]" parameter
Name - This, of course, is the specified socket name set earlier when opening the socket.[/color]
Text/%var/&binvar - Pretty much as it sounds, and as explained above.

Putting this information with the previous code, looks like this:
Code
 


Also, each sockwrite event triggers the SOCKWRITE event (This is optional, and only needed if you want to run certain commands whenever information is sent)
SOCKWRITE works the same way as the SOCKOPEN event.
IE:
Code
 


Reading Information Received from Remote System

These functions are very vital to using sockets. It allows you to create a 2-way communication between the remote system (Like decimating ignorance :).. sorry).

This is done with the SOCKREAD event. In which, again, uses the same parameters as the SOCKOPEN and SOCKWRITE events. It also uses the sockread alias.

mIRC Help wrote

/sockread [-fn] [numbytes] <%var|&binvar>


-f - The f switch tells mIRC you'd like to put the received string of information into a variable, even if it does not contain a CrLf.
-n - The n switch tells mIRC to check the string for a CrLf. If there is a CrLf, it will place the information in the specified &binvar (binary variable). If it doesn't, mIRC will do nothing with the string. If the two switches are put together mIRC will place the string into a binary variable even if there is no CrLf.
%var/&binvar - Exactly as it sounds. Either place a variable that you'd like to hold the string, or the binary variable with the above switches.

Example with the above codes:
Code
 


Closing Sockets

Closing sockets is very important. No matter what you do, sockets will always remain open unless the remote system closes. Sockets use up 'bandwidth' and as mIRC says, it's a very limited resource. Closing sockets is done by the sockclose alias.

mIRC Help wrote

/sockclose <name>


After all the above information, this is rather self-explanitory.
Name - The name of the socket, as specified earlier, of course.

Of course, this triggers the SOCKCLOSE event. You can use this event to run commands whenever a socket by the specified name is closed.

Used with the above code, as usual:
Code
 


Marking Sockets

Socket Marking is used for your own use. It is able to contain a string of up to 512 bytes (Something like 51 characters, I'm not sure). You can use this information for retreive from sockets, while allowing the ability to avoid the use of variables.

Code
 


Renaming Sockets

Socket renaming is done so you can open the current socket's name for use, and gives this socket a more desired name. This is all done without having to close the connection and reopen it :)

Code
 


Pausing Incoming Data (Added in mIRC 6.17)
Pausing the sockets are used to ignore any information being recieved by the socket temporarily, until told to begin listening again.

Code
 


-r - This switch indicates that mIRC should restart the socket listening to data. This means incoming information will no longer be ignored. If this switch is not identified, then mIRC will begin ignoring information.
--

Now, we have all the basic required elements out of the way. We can begin to get a little bit more in-depth. ;D

--

Socket Identifiers

mIRC Help wrote

$sock(name,N)
This returns information about a socket connection that you created using the socket commands.

Properties: name, ip, port, status, sent, rcvd, sq, rq, ls, lr, mark, type, saddr, sport, to, wserr, wsmsg, bindip, bindport


The Sock() function is used like this:
Code
 


Name - The name prop is used to retreive the sockets name. This can also be done with the $sockname identifier.
IP - The ip prop is used to retreive the Internet Protocol number of the remote system.
Port - The port prop is used to retreive the local port being used by the socket.
Sent and Rcvd - The sent prop is used to show the number of bytes sent to the remote system, and the rcvd prop is used to show the number of bytes received from the remote system.
Sq and Rq - The sq prop is used to see how many bytes sent are being que'd (Held until it's able to send the information). The rq prop is used to find out how many bytes received are being que'd before running their commands. This is used so that information is not missed at all.
Ls and Lr - The ls prop tells you how long it's been since you last sent information, and the lr is the same but since you last received information.
Mark - The mark prop tells you the information (512 bytes max.) that was saved earlier by using the sockmark alias.
to - The to prop shows how long the socket has been connected to the remote system
wserr - The wserr prop shows the last WinSock error number.
wsmsg - The wsmsg shows the WinSock error message that corresponds with the error number.
Code
 

ssl - The ssl prop returns $true or $false whether the socket is opened over an SSL connection or not (specified in /sockopen). [Added in version 6.17]
Code
 

pause - The pause prop returns $true or $false depending on whether or not the socket has been paused from receiving incoming data (specified in the /sockpause alias, which was added in mIRC 6.17). [Added in version 6.17]

- Other Identifiers

$sockerr - This is used to tell you if there has been an error within the socket. If there has been an error, it will set a value. If there hasn't, the value will be null.
$sockbr - This identifier returns the number of bytes in size that the last string received was.
$portfree(<port number>) - This will tell you if the specified port is not being used. This is more oftenly used with listening sockets (Will be learned next), because mIRC doesn't allow you to listen on a port already being listened to.


--

Creating a Server with Sockets (Listening Sockets)

Listening sockets is used for creating servers so that other's may connect to you. With the time and dedication, you can create anything from FTP servers, IRC servers, HTTP servers, etc.

Listening sockets are done with just the socklisten alias, and the SOCKLISTEN event.

The socklisten alias is needed before the socklisten event can be used:
Code
 


The SOCKLISTEN event is triggered whenever a remote system attempts to connect. This, also, uses the sockaccept alias.
Code
 

The above code will listen to the socket named server, and when a connection is attempted, it will accept the connection and name it "client."

But what if you want to read for specific information to be retreived from the client, or remote system? This is easy. You can pass information between the client and server easily with the SOCKREAD event and sockwrite alias.

Code
 


--

As I promised, here's a little socketbot code to practice with. To connect to an IRC server, type /connect host port (just the same as you'd use /server). Type /cquit to quit the server

Code
 


--

For more information on IRC Protocol, view the RFC 1459, here.

--

Please notify me of any informations, or other errors therefor and I will replace them :)

P.S. I wrote this tutorial on TCP sockets only, not UDP. Reason being I am not familiar with UDP, and you'll have to find that on your own time. Sorry ;/


Last edited by KuTsuM on Tue Sep 30, 2008 2:02 am; edited 7 times in total
DarthReven  -  Sat Oct 08, 2005 3:38 pm Reply with quote
$sockerr will return $null if there are no socket errors the way you have it set up it will give you invalid data. i would sugest you use "if (!$sockerr) {." Also that if statement would be more useful before the sockwrite commands. As to your comment "Creating Server with Sockets" that would be incorrect what you doing is just opening anouther connection elsewere. Yes its true you can create an FTP w/ the socket but its not secure and has risks.
KuTsuM  -  Sat Oct 08, 2005 3:54 pm Reply with quote
the only difference between mIRC and an application with servers is the fact that applications that are dedicated to the servers are more elaborately built. Technically it is a server. If you were to look up programming sites and look at server examples, it's pretty much the samething. Just a listening socket with a bunch of functions, sub-routines, etc. Also, all servers have their own exploits and risks. Mainly FTP servers, they always have exploitation. Yes, there will be more risks due to mIRC's limitations

As for the $sockerr, thanks ;/ I don't normally use it Smile I edited the tutorial to fit it.
Repz  -  Sun Jan 01, 2006 11:00 pm Reply with quote
Nice, Im a newbie so it kinda helped me out. thanks!
KuTsuM  -  Sat Feb 18, 2006 11:14 pm Reply with quote
EDITED: I edited this tutorial to fit some additions in mIRC 6.17. Any of the additions I added I marked as being of version 6.17, so those with older versions can tell the difference. If I missed any, please let me know, I'll add them ;p
PuNkTuReD  -  Thu May 29, 2008 11:19 am Reply with quote
replace
Code
 

with
Code
 

and you said its /cclose to shut it but its /cquit

awesome tut man definitly gonna help me get into sockets
KuTsuM  -  Tue Sep 30, 2008 2:03 am Reply with quote
Thanks PunkTuReD!

Errors fixed.
rocking111  -  Mon May 10, 2010 4:33 am Reply with quote

KuTsuM wrote

Thanks PunkTuReD!

Errors fixed.


Thats good. Check out this one.
function mosDBTable ($table='', $keyname='id', $db='') {
if ($table) $this->_tbl = $table;
else $this->_tbl = $this->tableName();
$this->_tbl_key = $keyname;
if (is_object($db)) $this->_db = $db;
cisa
Crying or Very sad
Display posts from previous:   
Post new topic   Reply to topic    Hawkee.com Forum Index -> mIRC Tutorials All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.