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

Sockopen
Format: /sockopen [-de] [bindip] 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.

  is a name you supply to describe the connection. This name will be used to reference this connection in the future.   is the IP address or hostname of the computer you wish to connect to.   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
Format: On *:sockopen: :{ [commands] } Example: On *:sockopen:Getwebpage:{ echo -s Connection to webpage successful! }

This is an event which is triggered when a socket is successfully opened with the /sockopen command.

  can be a wildcard.

Sockclose
Format: /sockclose 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
Format: On *:sockclose: :{ [commands] } Example: On *:sockclose:Getwebpage:{ echo -s Remote has closed the connection. }

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.

  can be a wildcard.

Socklisten
Format: /socklisten [-d] [bindip] [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.

  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
Format: On *:socklisten: :{ [commands] } Example: On *:socklisten:Webserver:{ sockaccept Webcon $+ $rand(1,99999) }

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.

  can be a wildcard.

Sockaccept
Format: /sockaccept Example: /sockaccept Helloworld Example 2: /sockaccept Webcon $+ $rand(1,99999)

This command accepts a connection attempt from an On socklisten event.

  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.

Sockwrite
Format: /sockwrite [-tnb] [numbytes]  Example: /sockwrite -n Getwebpage GET /websiteos/example_of_a_simple_html_page.htm HTTP/1.1 Example 2: /sockwrite Helloworld &sendfile

This command sends either text-based or binary data to a remote connection.

  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.  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
Format: on *:sockwrite: :{ [commands] } Example: on *:sockwrite:Helloworld:{ echo -s Done sending data to socket }

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.

  can be a wildcard.

Sockread
Format: /sockread [-fn] [numbytes] <%var|&binvar> Example: /sockread %chatline Example 2: /sockread &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. <%var|&binvar> 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
Format: On *:sockread: :{ [commands] } Example: on *:sockread:Getwebpage:{ sockread %htmllines | echo -s HTML of page: %htmllines }

This event is triggered when data is read from a socket. It almost always encapsulates a sockread command.

  can be a wildcard.