mIRC Homepage

/socklisten

Posted By: pheonix

/socklisten - 08/09/03 07:54 PM

i dont really get this, if i were to socklisten on port 6667, would this mean that if someone opens a socket to my ip, it would allow me to accept the connection in the on socklisten event?
Posted By: r0ck0

Re: /socklisten - 08/09/03 07:56 PM

it would listen for connections on port 6667 yes
Posted By: pheonix

Re: /socklisten - 08/09/03 08:00 PM

is there a way to see whats trying to connect?
also, is there a way that someone can /sockwrite to my ip, and i can see what they /sockwrite'ed?
Posted By: LocutusofBorg

Re: /socklisten - 08/09/03 08:29 PM

If you detect someone trying to connect to a certain port you are listening on via /socklisten, you can accept the connection with /sockaccept, after which the $sock identifier becomes available to give you information.
Posted By: pheonix

Re: /socklisten - 08/09/03 08:29 PM

do you know about my 2nd question?
also, if someone opens a socket to my ip using port 6667, will that use 6667 on my comp as well?
im trying to make a server :tongue:
Posted By: LocutusofBorg

Re: /socklisten - 09/09/03 04:24 AM

I have never tried anything like that yet, but I imagine it would have to use the on sockread event the trigger when data is received through the socket, in which you /sockread the data and do whatever you want with it.
Also, someone else using a mIRC socket can just /sockwrite to the socket...
Posted By: bloupx

Re: /socklisten - 09/09/03 12:44 PM

if you do /sockaccept it'll use a random free port as specified in mirc dcc options.
and you can check for who's connecting by checking for $sock($sockname).ip in the socklisten event.
Posted By: pheonix

Re: /socklisten - 09/09/03 04:13 PM

thats the problem, i managed to get 16 people to connect, they all tried using /sockwrite and i didnt receive any message, i tried using on sockwrite and on sockread.
Posted By: Rich

Re: /socklisten - 09/09/03 04:47 PM

Code:
;open listening socket
alias startlisten socklisten listensocket 6667
;close listening socket
alias stoplisten sockclose listensocket
;close all accepted connections
alias closeall sockclose acceptsock*

;wait for incoming connections
on *:socklisten:listensocket:{
  if ($sockerr) { echo listenerror! | return }
  ;generate unique name
  var %name = acceptsocket $+ $ticks
  ;accept connection
  sockaccept %name
  ;use $sock to retreive info
  echo accepted connection from $sock(%name).ip
}

;wait for incoming data (note the wildcard, it will respond to any socket opened in the ON SOCKLISTEN)
on *sockread:acceptsock*:{
  if ($sockerr) { echo readerror! | return }
  echo Received data from $sock(%name).ip :
  ;read data
  sockread %line
  ;check if any data was read
  while ($sockbr) {
    ;echo data, and check for empty line
    echo $iif(%line,$ifmatch,[empty line])
    ;read next line
    sockread %line
  }
}

Now if you type /startlisten, people should be able to connect to you on port 6667 and any data they send will be echoed.

Note that is just the idea, and far from perfect (if they send you a number as data you'll get an error from echo for example smile )
Posted By: pheonix

Re: /socklisten - 09/09/03 05:25 PM

Code:
alias serv.open {
  socklisten connect 6667
}
alias serv.close {
  sockclose connect
  sockclose client.*
}
alias serv.connect {
  sockopen test $ip 6667
}
alias serv.test {
  sockwrite -tn client.* TEST bkshdg
  sockwrite -tn test test test
}
on *:socklisten:connect:{
  sockaccept client.1
  echo -a client. [ $+ [ $sock(client.1).ip ] ]
  sockrename client.1 client. [ $+ [ $sock(client.1).ip ] ]
}
on *:sockread:client.*:{
  if ($sockerr) { return }
  sockread %client
  tokenize 32 %client
  echo -s $sockname - $1-
}

for the above, i have to /sockwrite to "test", but the sockread event still occurs, but if i /sockwrite to client.* it doesnt work :tongue:
Posted By: Rich

Re: /socklisten - 09/09/03 09:02 PM

if you /sockwrite to client.* that means client.* will send data. This will then be received by connect, but since there is no ON SOCKREAD:connect: mIRC discards the data.

Some small things btw:
-you don't need the -t switch for sockwrite if the text you send doesn't start with an ampersand (doesn't do any harm though)
-If you make the name of the accepting socket (client.*) dependant on only the ip of the connection, there could be problems: if someone connects to you twice the ip will be the same, so there will be an error because that socket already exists. To prevent this you could include the remote port in the sockname as well, or use another way of picking a unique name (like using $ticks).
Posted By: pheonix

Re: /socklisten - 09/09/03 09:08 PM

Code:
alias serv.start {
  socklisten connect 6667
}
alias serv.close {
  sockclose client.*
  sockclose connect
}
alias serv.connect {
  sockopen client.me $ip 6667
}
alias serv.test {
  sockwrite -tn client.* TEST
}
on *:socklisten:connect:{
  sockaccept client.1
  echo -a client. [ $+ [ $sock(client.1).ip ] ]
  sockrename client.1 client. [ $+ [ $sock(client.1).ip ] ]
}
on *:sockread:client.*:{
  if ($sockerr) { return }
  sockread %client
  tokenize 32 %client
  echo -a $sockname - $1-
}
menu status {
  Open Server: serv.start
  Close Server: serv.close
  Connect: serv.connect
  Test: serv.test
}

this works how i want it :tongue:
© 2022 mIRC Discussion Forums