Results 1 to 5 of 5

Thread: Threading... again.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026

    Threading... again.

    Is it possible to pass an object to a thread? I tried the following, but it didn't work...

    VB Code:
    1. Public Sub StartTCPConnection()
    2.         Dim HandleThread As Thread
    3.         Try
    4.             ' Set the TcpListener on port 13000.
    5.             Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
    6.             Dim port As Int32 = 13000
    7.             Server = New TcpListener(localAddr, port)
    8.  
    9.             ' Start listening for client requests.
    10.             Server.Start()
    11.  
    12.             While True
    13.                 Debug.Write("Waiting for a connection... ")
    14.                 ' Perform a blocking call to accept requests.
    15.                 ' You could also user server.AcceptSocket() here.
    16.                 Dim Client As TcpClient = Server.AcceptTcpClient
    17.                 [b]HandleThread = New Thread(AddressOf HandleConnection(Client))[/b]
    18.                 HandleThread.Start()
    19.                 Client.Close()
    20.             End While
    21.         Catch ex As SocketException
    22.             Debug.WriteLine("socketexeption:", ex.ToString)
    23.         End Try
    24.     End Sub
    25.  
    26.     Public Sub HandleConnection(ByVal Client As TcpClient)
    27.         ' Buffer for reading data
    28.         Dim bytes(1024) As [Byte]
    29.         Dim data As [String] = Nothing
    30.  
    31.  
    32.         Debug.WriteLine("Connected!")
    33.  
    34.         data = Nothing
    35.  
    36.         ' Get a stream object for reading and writing
    37.         Dim stream As NetworkStream = Client.GetStream()
    38.  
    39.         Dim i As Int32
    40.  
    41.         ' Loop to receive all the data sent by the client.
    42.         i = 0
    43.         i = stream.Read(bytes, 0, bytes.Length)
    44.         ' Translate data bytes to a ASCII string.
    45.         data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    46.         Debug.WriteLine([String].Format("Received: {0}", data))
    47.  
    48.         ' Process the data sent by the client.
    49.         data = CheckValidity(data.ToUpper)
    50.  
    51.         Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
    52.  
    53.         ' Send back a response.
    54.         stream.Write(msg, 0, msg.Length)
    55.         debug.WriteLine([String].Format("Sent: {0}", data))
    56.  
    57.         ' Shutdown and end connection
    58.         Client.Close()
    59.     End Sub

    what is the correct way to do this?

    thanx in advance,

    SQ1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You have to encapsulate that paramterized method in paramterless method , I mean this how I do it


    VB Code:
    1. Dim i As Integer
    2.     Private Sub Handler()
    3.         mainSub(i)
    4.     End Sub
    5.     Private Sub mainSub(ByVal i As Integer)
    6.         Dim t As Threading.Thread
    7.         t = New Threading.Thread(AddressOf Handler)
    8.     End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2001
    Location
    USA
    Posts
    1,026
    But if i do it that way, then I has to be external of the thread and stay the same.

    What I'm trying to do is have the server accept a connection and start a thread to handle that client and then wait to accept another connection... I thought that I would be able to do that by "copying" the clients object into the thread so that the thread would be able to handle the client's data coming in and going out. Am I going about this the wrong way?


    Thanks again,

    SQ1
    Now happily married and still crankin' away at the keyboard. Life is grand for a coder, no?

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Check out the examples for "Asynchronous socket server" and client - or something like that. Here's an example from some code that works:

    VB Code:
    1. Private Sub StartListening()
    2.         ' Data buffer for incoming data.
    3.         Dim bytes() As Byte = New [Byte](1024) {}
    4.  
    5.         ' Establish the local endpoint for the socket.
    6.         ' Dns.GetHostName returns the name of the
    7.         ' host running the application.
    8.         Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
    9.         Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
    10.         Dim localEndPoint As New IPEndPoint(ipAddress, properties.listenPort)
    11.  
    12.         ' Create a TCP/IP socket.
    13.         Dim listener As New Socket(AddressFamily.InterNetwork, _
    14.             SocketType.Stream, ProtocolType.Tcp)
    15.  
    16.         ' Bind the socket to the local endpoint and
    17.         ' listen for incoming connections.
    18.         Try
    19.             listener.Bind(localEndPoint)
    20.             listener.Listen(100)
    21.  
    22.             ' Start listening for connections.
    23.             While True
    24.                 Logger.Log("I", Encoding.ASCII.GetBytes("Waiting for a connection on port " & properties.listenPort & "..."))
    25.                 ' Program is suspended while waiting for an incoming connection.
    26.                 allDone.Reset()
    27.  
    28.                 listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), listener)
    29.  
    30.                 allDone.WaitOne()
    31.             End While
    32.  
    33.         Catch e As Exception
    34.             Logger.Log("E", Encoding.ASCII.GetBytes(e.ToString()))
    35.         End Try
    36.     End Sub 'StartListening

    allDone is a ManualResetEvent - so this code blocks until a connection is accepted. Since this class does more that just accept connections, StartListening is called from a new thread:

    VB Code:
    1. Dim clientThread As New Thread(AddressOf StartListening)
    2.         clientThread.IsBackground = True
    3.         clientThread.Start()

    Mike

  5. #5
    New Member
    Join Date
    Jun 2004
    Posts
    2
    A simple way to pass data/objects to threads is to create an intermediate class.
    Create a member object of the client class inside the intermediate class and a public method without any parameters

    Public Class ThreadDataShare

    Private clientobject As Client

    Sub ThreadLaunch()
    'thread exection function
    end sub

    Include another method in the class which will take a client object and clone it into the class private member.
    Before starting a thread, create an instance of ThreadDataShare class, and pass its ThreadLaunch function to the thread to run.
    You can put whatever data you need to exchange between your thread as members of this class, and create property subs to let you write read the members. Then both your main thread, and the new thread will be able to access the object's members.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width