MIRC Scripting/Advanced/Sockets

mIRC Sockets.
A socket is a programming interface for the purpose of handling IP-related connections. Sockets can be very useful depending on what they are used for, and mIRC's socket handler can do quite a number of things from obtaining information or files from a website, connecting to IRC networks or working with other protocols.
Transmission Control Protocol (TCP).
The Transmission Control Protocol, otherwise known as TCP or TCP/IP is an Internet/networking protocol which relies on the existence of servers and clients. In this protocol, connections have to be established before they are considered open. This is done via what is known as a "3-way handshake". When a connection is made between a client and a server, information may be freely transferred between the two nodes.
Servers and clients.
A "Server" in this context is a program or script which is responsible for accepting and managing connections, whereas a "Client" is a program or script which connects to a server. The most commonly noticed example of servers and clients are the web sites we browse every day. Our browser functions as a client, which connects to a server located at the address you type in, and downloads the information to the screen.
Both servers and clients can be written using mIRC's sockets
Client scripting in mIRC:.
Sockopen.
Format: /sockopen [-de] [bindip] &lt;name&gt; &lt;address&gt; &lt;port&gt;
Example: /sockopen Getwebpage www.comcity.com 80
This command attempts to open a connection to a server at the specified IP address or hostname and port. If no server is found running at that location/port, the connection will fail.
&lt;name&gt; is a name you supply to describe the connection. This name will be used to reference this connection in the future.
&lt;address&gt; is the IP address or hostname of the computer you wish to connect to.
&lt;port&gt; is the Port number of the service you wish to connect to on the remote machine.
The -d switch means that you specified an ip address as the bind address.
The -e switch creates an SSL connection.
On sockopen.
This is an event which is triggered when a socket is successfully opened with the /sockopen command.
&lt;name&gt; can be a wildcard.
Sockclose.
Format: /sockclose &lt;name&gt;
Example: /sockclose Getwebpage
This command closes an open connection based upon its reference name. If the socket is not open, this command does nothing.
On sockclose.
This is an event which is triggered when a socket-based connection has been closed by the remote node. This event does not trigger when the socket is closed locally using /sockclose.
&lt;name&gt; can be a wildcard.
Server scripting in mIRC.
Socklisten.
Format: /socklisten [-d] [bindip] &lt;name&gt; [port]
Example: /socklisten Webserver 80
This command listens for a connection from a client node on the specified port. If a port isn't specified, one from the range in the DCC options is selected at random.
&lt;name&gt; is the reference name you would like to give this connection.
[port] is an optional parameter which sets the port that the server is to run from.
On socklisten.
This is an event which is triggered when a node attempts to connect to your server on the port you are running the service on. The main goal with this event is to accept (or reject) incoming connections.
&lt;name&gt; can be a wildcard.
Sockaccept.
Format: /sockaccept &lt;name&gt;
Example: /sockaccept Helloworld
Example 2: /sockaccept Webcon $+ $rand(1,99999)
This command accepts a connection attempt from an On socklisten event.
&lt;name&gt; is the reference name you would like to give this connection.
If accepting multiple connections, it is recommended to use a $rand() identifier to assign a random number so that more than one connection can be made at one time. This requires the use of wildcards in many of the events.
Reading and Writing information.
Sockwrite.
Format: /sockwrite [-tnb] &lt;name&gt; [numbytes] &lt;text|%var|&amp;binvar&gt;
Example: /sockwrite -n Getwebpage GET /websiteos/example_of_a_simple_html_page.htm HTTP/1.1
Example 2: /sockwrite Helloworld &amp;sendfile
This command sends either text-based or binary data to a remote connection.
&lt;name&gt; is the reference name of the connection you plan to send data to.
[numbytes] is an optional parameter which works in conjunction with the -b switch to indicate you are specifying the amount of bytes to send.
&lt;text|%var|&amp;binvar&gt; describes the data to be sent to the remote node.
The -t switch forces data contained in a binary variable to be interpreted as normal text rather than binary data.
The -n switch adds a Carriage return/Line feed to the end of the data ($crlf)
The -b switch indicates you are specifying the [numbytes] parameter.
On sockwrite.
This event is triggered when mIRC has finished sending the specified data to the specified connection. In the case that more data is added before the previous data is sent, it is added to a data queue.
&lt;name&gt; can be a wildcard.
Sockread.
Format: /sockread [-fn] [numbytes] &lt;%var|&amp;binvar&gt;
Example: /sockread %chatline
Example 2: /sockread &amp;filepart
This command reads data that has been received over a connection. It is always run inside of an On sockread event.
[numbytes] is an optional parameter describing the number of bytes to read from a binary variable. It defaults to 4096 if not specified.
&lt;%var|&amp;binvar&gt; is a variable to contain the data read from the socket.
The -f switch forces mIRC to process data from a sockread event even if it does not end with a $crlf
The -n switch allows you to add a $crlf-terminated line into a binary variable.
On sockread.
This event is triggered when data is read from a socket. It almost always encapsulates a sockread command.
&lt;name&gt; can be a wildcard.