WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Winsock
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Socket Connector and Listener Script

 Keywords:  

In this example there are two separate scripts running on two separate computers. There is the "listener" script that listens for a connection and uses the sListen and sAccept functions, and the "connecter" script that connects to the listener script and requests information. In this example the "connecter" script may request either a quote or the time of day. After the requested information is sent, the socket is closed. The "connecter" script needs to know the IP address and port name/number of the "listener" computer.

For Dial-up connections the winipcfg.exe program will report the assigned ip address. The IP Address usually changes on each separate connect, making it difficult to have a useful "listener" on a dial-up line.

The LISTENER script

;;;The LISTENER script
; Use CTRL-Break to exit
AddExtender("wwwsk34i.dll")
quote0="Few women admit their age, few men act it. "
quote1="Don't anthropomorphize computers. They hate that. "
quote2="ANIMAL LOVER ON BOARD. They're delicious. "
quote3="My karma ran over my dogma. "
quote4="The gene pool could use a little chlorine. "
quote5="Time is what keeps things from happening all at once. "
quote6="I didn't fight my way to the top of the food chain to be a vegetarian. "
quote7="Women who seek to be equal with men lack ambition. "
quote8="Your kid may be an honor student but you're still an idiot! "
quote9="If we aren't supposed to eat animals, why are they made with meat? "
maxquote=9
BoxOpen("Listener","")
listensocket=sOpen()
sListen(listensocket,"ftp")   ; use ftp port for lack of anything better
Served=0
while 1
   datasocket=sAccept(listensocket,@TRUE)  ; Block for a connection
   if datasocket
       gosub ProcessConnect
   else
      sClose(listenSocket)
      msg=wxGetLastErr()
      Message("Socket Error",msg)
      exit
  endif
endwhile
exit   
:ProcessConnect
         Served=Served+1
         rqst=sRecvLine(datasocket,20)
         rqst=strlower(rqst)
         if rqst=="time"
             rsp=TimeDate()
         else
            if rqst=="quote"
               mqr=random(maxquote)
               rsp=quote%mqr%
            else
               rsp="Error: Can only respond to TIME and QUOTE"
            endif
         endif
         BoxTitle(strcat("Served:",Served,"  Last=",wxGetInfo(2,datasocket)))
         BoxText(strcat(rqst," : ",rsp))
         sSendLine(datasocket,rsp)
         sClose(datasocket)
return

The CONNECTER script

AddExtender("wwwsk34i.dll")
while 1
   what=AskLine("Connecter","Enter TIME or QUOTE to get information from listener","QUOTE")
   talksocket=sOpen()
   ret=sConnect(talksocket,"206.63.11.252","ftp")
   sSendLine(talksocket,what)
   response=sRecvLine(talksocket,555)
   sClose(talksocket)
   Message(what,response)
endwhile

Article ID:   W16381
File Created: 2005:02:18:12:20:08
Last Updated: 2005:02:18:12:20:08