mircscripting.info Forum Index mircscripting.info
#mIRCscripting Forum
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie in sockets....

Post new topic   Reply to topic    mircscripting.info Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message

Joined: 28 Dec 2003
Posts: 4
Location: Romania

PostPosted: Sun Jan 04, 2004 3:51 am    Post subject: Newbie in sockets.... Reply with quote

So what mIRC sockets really is?
Socket allows you to create your own raw socket connections in order to
send and receive information.
The main idea is connect PC's and them send and/or recieve data.

All Pc's have a lot of ports, your PC 'suport' 65536 ports. So imagine if you are
listenning one of these 65536 and a certain computer try to connect to that port
you can accept or not, if you accept, you have a connection. Now you are the server
computer and the other is the client computer, like a FTP Server and a client when you
connect to a FTP server you can send and recieve data from server and vice-versa.
The most used ports are:
FTP: 21
Telnet: 23
smtp: 25
Http: 80
POP3: 110
Newsgroups: 119

mIRC Sockets
Actually mIRC Suport 2 diferent type's of Sockets, The TCP "Transmission Control Protocol"
protocol, and UDP protocol.
The TCP protocol is a better protocol. We would talk about UDP later.

On mIRC exists identifiers, events and commands for sockets.

Socket Identifiers
We will use most of this identifiers in this doc

$sock(name,N) - returns about a socket connection. N parameter is optional
you can use wildcard name.
These identifier have many props.
.name - return a socket name, a name that you give to a connection
.sent - return a number of bytes sent over that connection
.rcvd - return a number of bytes recieved over that connection
.sq and .rq - return the number of bytes queued in the send and receive buffers
.ls and .lr - return the number of bytes queued in the send and receive buffers
.mark - return some information allocad to that socket (/sockmark) for more
.type - return if is TCP or UDP socket
.saddr .sport - return the source address and port of the last received UDP packet
.to - returns the number of seconds the socket has been open
.wserr - returns the last winsock error number that occurred on a socket
.wsmsg - returns the last winsock error message match the error number

$sockname - returns the socketname for the corrent socket event
$sockerr - return if exist error connecting, sending/recieving data
$sockbr - is set to the number of bytes read by a /sockread command. It is used to
test whether any information was in fact read from the buffer
$portfree(N) - returns $true if a port is not on use and $false if is on use

Now we have the socket commands.
Comments: [optional]
The examples: is for a IRC Clone Smile connecting to aaia.PTlink.net

This try to connect to a host/ip trough port
EX: /sockopen clone aaia.PTlink.net 6667

When a connection is maded we have an event.
on *:sockopen:name:commands
on *:sockopen:clone: {
if ($sockerr) return
;If we can't establish connection this command will stop the event
else sockwrite -n $sockname Nick SocketClone $+ $crlf $+ User clone $ip irc.PTlink.net :mIRC Socket clone
;If $sockerr is false we will send information with sockwrite (later), on an IRC server
;we need to enter nickname, ident, ip, server and realname. The $crlf is like an enter $cr = $chr(13) and $lf = $chr(10)

/sockclose sockname
This will close the sockname
/sockclose clone

When a socket is closed by the other part we have an event.
on *:sockclose:name:commands
on *:sockclose:clone: {
echo -s Connection lost to PTlink

With this command you will listen for a connection to the specifyed port
/socklisten test 45

/sockaccept name
With this command you will accept a connection that is being listen. You need to
specify a socket name that is not on use, never accept with the listen name.
When you accept you continue listen the socketname
/sockaccept accept.test

The socklisten event is triggered when some try to connect to a listen port,
to accept connection you need the sockaccept else the connection will lost.
on *:socklisten:name:commands
on *:socklisten:test: {
sockaccept accept.test
;this accept the connection, now a active connection trough accept.test name.
echo -s Connection from $sock($sockname).ip on $sock($sockname).port
sockwrite -n accept.test Hello there

This allows you to write something to a socket
/sockwrite [-ntb] [numbytes]
The -n parameter switch appends a CRLF to the line being sent if it's not a &binvar and
if it doesn't already have a CRLF.
If you specify the -t switch, it forces mIRC to send anything beginning with a & as
normal text instead of interpreting it as a binary variable.
The -b switch indicates that you are specifying the numbytes value which is the number
of bytes you want sent.

Ex: /sockwrite -n clone join #Portugal

When sockwrite finish we have an event
on *:sockwrite:name:commands
on *:sockwrite:clone: {
if ($sockerr) return
echo -s Information to $sockname sent succefully

/sockread [-fn] [numbytes] <%var|&binvar>
This command reads data, and put the data to a %varibale or a &binvaribale (binary variable)
EX: /sockread %data

For sockread we need an event
on *:sockread:name:commands
on *:sockread:clone: {
if ($sockerr) return
sockread %data
echo -s (Clone) %data
unset %data

/sockmark name [text]
This mark something to a socket, you can see this later with $sock(name,N).mark
If you don't specify text the mark is cleared
EX: /sockmark clone Socketclone aaia.PTlink.net
Reading this $sock(clone).mark

Now we know all commands and all identifiers for TCP protocol

NOTE: all sockname can be wildcards to identifiers, commands and events
EX: $sock(*g*).mark | /sockwrite -n *g* bla bla | on *:sockread:*g*:commands

I will put some useful examples to this.
Note: this examples are only maked for this

;* mIRC clone Sockets *
;* All on remotes *
;* Usage: /clone [port] *
alias clone {
sockopen clone $$2 $iif($3,$3,6667)
;open a socket to server, if you specify parameter 3 the port will take that value
;else will take 6667.
sockmark clone $1 $2
;this marks a the socket clone with $1 = nick and $2 = server
on *:sockopen:clone: {
if ($sockerr) { echo -s (Clone) Unable to connect ( $+ $sock($sockname).wsmsg | return }
;check exist sockerr. The wsmsg show why socket don't connected
else {
window -e @Clone
;this open a mIRC window with editbox
sockwrite -n $sockname Nick $gettok($sock($sockname).mark,1,32) $+ $crlf $+ User SocketClone $ip $gettok($sock($sockname).mark,2,32)
;this writes to the socket, and put nick and user.
;$gettok($sock($sockname).mark,1,32) return the nick
;$gettok($sock($sockname).mark,2,32) return the server
;the $crlf is like an 'enter'
echo @clone Connected to $gettok($sock($sockname).mark,2,32)
on *:sockread:clone: {
sockread %data
;this put the recieved information to a variable %data
echo @clone %data
unset %data
on *:sockclose:clone: {
echo @clone Connection terminated to $gettok($sock($sockname).mark,2,32)
;this will triggered if the clone is closed from remote machine
on *:input:@clone: {
if ($sock(clone)) sockwrite -n clone $1-
;when you input something to @clone if the socket clone is true you will
;sockwrite to clone socket
on *:close:@clone: {
if ($sock(clone)) sockclose clone
;if you close @clone the socket clone will close to

;Here is the basic, you can do with these a lot of thinks, like multiserver
;and other

Another example

;* Socket Chat *
;* If 2 persons have this script, they can talk using sockets *
;* Note: One of the person need to by the server, the server need to type
; /achat and them client need to type: /schat *

alias achat if ($1) { socklisten achat $$1 | echo -s Now listenning port $1 for connection }
;this listen a port that you specify, this for server
alias schat if ($2) { sockopen schat $$1 $$2 | echo -s Now trying to connect to $1 Port: $2 }
;this open a socket to a host/ip of server with the server port
;this is used for client
on *:socklisten:achat: {
;this listenning the socket achat for connections, if a connection happen, the connection
;will be accepted. This only triggered on server
sockaccept schat
sockclose $sockname
;this close the sockname because no more connections are accepted and accept schat
window -ne @Schat
;open a mIRC window with editbox to input and recieving data
echo $colour(info) @Schat Connected
on *:sockopen:schat: {
;this is triggered only by the client
if ($sockerr) { echo -s Unable to connect, check if host/ip and port are correct | return }
;check if error happen
else { window -ne @Schat | echo $colour(info) @Schat Connected }
;* Now the rest of events are for server and client *
on *:sockclose:schat: {
echo $colour(info) @Schat Disconnected
;this is triggered when other part close the connection
on *:sockread:schat: {
sockread %schat
echo @schat %schat
unset %schat
on *:input:@schat: {
if ($sock(schat)) { sockwrite -n schat ( $+ $me $+ ) $1- | echo @schat ( $+ $me $+ ) $1- }
;when you input something on @schat if schat socket is $true you will sockwrite to the socket
on *:close:@schat: {
if ($sock(schat)) { sockclose schat
echo -s Closed Schat


;Listenning some ports. Netbus port
;Usage: /listnetbus this will listen the port 12345 (Netbus) /stoplisten this will
;stop listenning 12345 port. When someone connects to you by netbus you will be
;warnned with client ip and them close the connection.
;Note: some ppl if scan's your ports maybe think that you're infected with netbus
;because you have port 12345 listenning.

alias listnetbus if ($sock(netbus) == $null) socklisten netbus 12345
alias stoplisten if ($sock(netbus)) sockclose netbus
;the first aliase will listen for connection on port 12345 and the second will close
;the socket on listening
on *:socklist:netbus:{
sockaccept nbactive | echo -s Connection from $sock(nbactive).ip on $sock(nbactive).port $+ . Closing...
sockclose nbactive
;After this examples I guess you understand this Smile

Now I will talk about UDP Sockets

UDP is a connection-less protocol, ie. you can send information via UDP to other UDP
addresses without needing to connect to them first.
UDP does not guarantee that any information you send will actually reach it's destination.

We have one command and one event for UDP Sockets, almost sockets identifiers work
here to.

The command is:
/sockudp [-bntk] [port] [ [numbytes] [text|%var|&binvar]

The -b,-n and -t is the same of sockwrite. The -k switch makes the socket open and
wainting for information (is bit like on socklisten), if you don't specify -k the
sockupd is open send information and close.
Ex: /sockudp -k teste 49
This make UDP socket test open to that ip on that port

The event is:
on *:udpread:name:commands
The udpread event is triggered when there is info waiting to be read on a UDP socket.
You can read this info using the /sockread command.
on *:udpread:test:sockread %data | echo -s %data | unset %data

Here comes an example of who to use UDP Sockets

;This listenning BO port 31337 for connection. Bo use UDP Protocol so we also
;need to use UDP mIRC Sockets for that
;Usage: /listbo this will listen the port 31337 (Bo) /stoplistenbo this will
;stop listenning 31337 port. When someone connects to you by Bo you will be
;warnned with client ip and them close the connection.
;Note: some ppl if scan's your ports maybe think that you're infected with BO
;because you have port 31337 listenning.

alias listbo sockudp -k bo 31337
;we use -k to keep socket UDP 'alive'
alias stoplistenbo sockclose bo
on *:udpread:bo:{
echo -s Connection attemped from $sock($sockname).saddr (Bo). Closing
sockclose $sockname | .timerrelisten 1 5 listbo
;We need to relisten for connection because we close the connection when someone
;receive some information.

mIRC Sockets are a very important mIRC Tool, The better protocol is TCP.
If you want more help for some commands, events and identifers please check the
mIRC help for sockets :]
It's all for now.
Have fun with mIRC Sockets.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    mircscripting.info Forum Index -> Tutorials All times are GMT - 5 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

Powered by phpBB © 2001, 2005 phpBB Group