Socket remoteTestClientTCP & remoteTestServerTCP class methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Socket remoteTestClientTCP & remoteTestServerTCP class methods

John Chludzinski
For whatever reason the Socket class did not include these (class methods) as examples, so I offer these:

---------------------------------------------Client-------------------------------------------------

remoteTestClientTCP

"FIRST start up another image, and execute: Socket remoteTestServerTCP.
THEN come back to this image and execute:"

"Socket remoteTestClientTCP"

"Performa 6400/200, Linux-PPC 2.1.24, both images on same CPU:
remoteClient TCP test done; time = 5680
250 packets, 1000000 bytes sent (176 kBytes/sec)
60 packets, 1000000 bytes received (176 kBytes/sec)"

    | socket bytesToSend sendBuf receiveBuf done bytesSent bytesReceived packetsSent packetsReceived t |

   Smalltalk garbageCollect.

   Transcript show: 'starting client/server TCP test'; cr.
   Transcript show: 'initializing network ... '.
   Transcript show: 'ok'; cr.

   socket := Socket newTCP.
   socket connectTo: (NetNameResolver addressForName: 'localhost') port: 54321.
   [
      socket waitForConnectionFor: self standardDeadline.
      Transcript show: 'client endpoint created'; cr.
      bytesToSend := 1000000.
      sendBuf := String new: 4000 withAll: $x.
      receiveBuf := String new: 50000.
      done := false.
      bytesSent := bytesReceived := packetsSent := packetsReceived := 0.

      t := Time millisecondsToRun:[[done] whileFalse: 
          [(socket sendDone and: [bytesSent < bytesToSend]) ifTrue: 
             [ packetsSent := packetsSent + 1.
               bytesSent := bytesSent + (socket sendData: sendBuf)].
               socket dataAvailable ifTrue: 
                  [ packetsReceived := packetsReceived + 1.
                    bytesReceived := bytesReceived + (socket receiveDataInto: receiveBuf) ].
               done := bytesSent >= bytesToSend ].
           [bytesReceived < bytesToSend] whileTrue: 
               [ socket dataAvailable ifTrue: 
                   [ packetsReceived := packetsReceived + 1.
                     bytesReceived := bytesReceived + (socket receiveDataInto: receiveBuf)]]].
       socket closeAndDestroy.
      Transcript show: 'remoteClient TCP test done; time = ' , t printString; cr.
      Transcript
          show: packetsSent printString , ' packets, ' , bytesSent printString , 
                   ' bytes sent (' , (bytesSent * 1000 // t) printString , 
                   ' bytes/sec)'; cr.
      Transcript
          show: packetsReceived printString , ' packets, ' ,
          bytesReceived printString , ' bytes received (' , 
         (bytesReceived * 1000 // t) printString , ' bytes/sec)'; cr.

  ] forkAt: Processor userBackgroundPriority.

---------------------------------------------Server-------------------------------------------------

remoteTestServerTCP

"See remoteTestClientTCP for instructions on running this method."
"Socket remoteTestServerTCP"

    | socket client buffer n |

   Transcript show: 'initializing network ... '.
   self initializeNetwork.
   Transcript show: 'ok'; cr.
   socket := Socket newTCP.
   socket
       listenOn: 54321
       backlogSize: 5
       interface: (NetNameResolver addressForName: 'localhost').

    Transcript show: 'server endpoint created -- run client test in other image'; cr.

    buffer := String new: 4000.

    [
       client := socket waitForAcceptFor: self standardDeadline.

       [ client isConnected] whileTrue: 
          [ client dataAvailable ifTrue: 
             [ n := client receiveDataInto: buffer.
               client sendData: buffer count: n ]].
          client closeAndDestroy.
          socket closeAndDestroy.
          Transcript cr; show: 'server endpoint destroyed'; cr.

   ] forkAt: Processor userBackgroundPriority.