Grasshopper

algorithmic modeling for Rhino

CAn anyone tell me why the following script locks up Rhino?  I enter it into the Grasshopper Script Editor and hit [OK] and it locks up:

 

''your code goes here…
    Dim server As Net.Sockets.TcpListener    server = Nothing    Try      ' Set the TcpListener on port 6001.      Dim port As Int32 = 6001      Dim localAddr As Net.IPAddress = Net.IPAddress.Parse("127.0.0.1")
      server = New Net.Sockets.TcpListener(localAddr, port)
      ' Start listening for client requests.      server.Start()
      ' Buffer for reading data      Dim bytes(1024) As Byte      Dim data As String = Nothing
      ' Enter the listening loop.      While True        Console.Write("Waiting for a connection... ")
        ' Perform a blocking call to accept requests.        ' You could also user server.AcceptSocket() here.        Dim client As Net.Sockets.TcpClient = server.AcceptTcpClient()        Console.WriteLine("Connected!")
        data = Nothing
        ' Get a stream object for reading and writing        Dim stream As Net.Sockets.NetworkStream = client.GetStream()
        Dim i As Int32
        ' Loop to receive all the data sent by the client.        i = stream.Read(bytes, 0, bytes.Length)        While (i <> 0)          ' Translate data bytes to a ASCII string.          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)          Console.WriteLine("Received: {0}", data)
          ' Process the data sent by the client.          data = data.ToUpper()          Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
          ' Send back a response.          stream.Write(msg, 0, msg.Length)          Console.WriteLine("Sent: {0}", data)
          i = stream.Read(bytes, 0, bytes.Length)
        End While
        ' Shutdown and end connection        client.Close()      End While    Catch e As Net.Sockets.SocketException      Console.WriteLine("SocketException: {0}", e)    Finally      server.Stop()    End Try
    Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")    Console.Read()

Views: 430

Attachments:

Replies to This Discussion

Actually it all seems to boil down to this command:

  Dim client As Net.Sockets.TcpClient = server.AcceptTcpClient()

 

I remove it and I get errors but Rhino does not lock up.

From the MSDN docs:

 

"AcceptTcpClient is a blocking method that returns a TcpClient that you can use to send and receive data. Use the Pending method to determine if connection requests are available in the incoming connection queue if you want to avoid blocking."

 

My guess is the script is simply put on hold until AcceptTcpClient is done with it's task, which may be a very long time, or indeed even infinite.

 

--

David Rutten

david@mcneel.com

Poprad, Slovakia

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service