|
-
Mar 14th, 2004, 11:38 AM
#1
Thread Starter
Frenzied Member
Threading... again.
Is it possible to pass an object to a thread? I tried the following, but it didn't work...
VB Code:
Public Sub StartTCPConnection()
Dim HandleThread As Thread
Try
' Set the TcpListener on port 13000.
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
Dim port As Int32 = 13000
Server = New TcpListener(localAddr, port)
' Start listening for client requests.
Server.Start()
While True
Debug.Write("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim Client As TcpClient = Server.AcceptTcpClient
[b]HandleThread = New Thread(AddressOf HandleConnection(Client))[/b]
HandleThread.Start()
Client.Close()
End While
Catch ex As SocketException
Debug.WriteLine("socketexeption:", ex.ToString)
End Try
End Sub
Public Sub HandleConnection(ByVal Client As TcpClient)
' Buffer for reading data
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
Debug.WriteLine("Connected!")
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = Client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = 0
i = stream.Read(bytes, 0, bytes.Length)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Debug.WriteLine([String].Format("Received: {0}", data))
' Process the data sent by the client.
data = CheckValidity(data.ToUpper)
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
debug.WriteLine([String].Format("Sent: {0}", data))
' Shutdown and end connection
Client.Close()
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?
-
Mar 14th, 2004, 12:18 PM
#2
Sleep mode
You have to encapsulate that paramterized method in paramterless method , I mean this how I do it
VB Code:
Dim i As Integer
Private Sub Handler()
mainSub(i)
End Sub
Private Sub mainSub(ByVal i As Integer)
Dim t As Threading.Thread
t = New Threading.Thread(AddressOf Handler)
End Sub
-
Mar 14th, 2004, 12:49 PM
#3
Thread Starter
Frenzied Member
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?
-
Mar 14th, 2004, 01:58 PM
#4
Frenzied Member
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:
Private Sub StartListening()
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](1024) {}
' Establish the local endpoint for the socket.
' Dns.GetHostName returns the name of the
' host running the application.
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, properties.listenPort)
' Create a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
' Bind the socket to the local endpoint and
' listen for incoming connections.
Try
listener.Bind(localEndPoint)
listener.Listen(100)
' Start listening for connections.
While True
Logger.Log("I", Encoding.ASCII.GetBytes("Waiting for a connection on port " & properties.listenPort & "..."))
' Program is suspended while waiting for an incoming connection.
allDone.Reset()
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), listener)
allDone.WaitOne()
End While
Catch e As Exception
Logger.Log("E", Encoding.ASCII.GetBytes(e.ToString()))
End Try
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:
Dim clientThread As New Thread(AddressOf StartListening)
clientThread.IsBackground = True
clientThread.Start()
Mike
-
Jun 6th, 2004, 05:22 PM
#5
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|