-
Nov 13th, 2012, 08:40 PM
#1
Thread Starter
Fanatic Member
Receive Paremeters on Multithreaded Server
I have created a multi-threaded server but can not figure out how to send an object/string. I think I have the ability to receive it already.
Server created:
vb Code:
serverThread = New Threading.Thread(AddressOf startHosting)
How do I specify an object or string parameter here?
vb Code:
connection.Connect(ip, port)
When new users connect my thread accepts an object parameter:
vb Code:
Private Sub clientWorker(ByVal param As Object) End Sub
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 13th, 2012, 09:09 PM
#2
Re: Receive Paremeters on Multithreaded Server
How do I specify an object or string parameter here?
You don't. When you connect, all you do is connect. Once you're connected, then you can send data.
-
Nov 14th, 2012, 09:56 AM
#3
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Ok, I had a feeling that was the case, so I re-wrote things to work around that.
My issue I have now is, how do I know what thread index/ip address data is arriving on?
This is my data arrival evenet
And this is the function that handles the data (yes, I know it is in C#)
C# Code:
private void winsock_DataArrival(string message)
{
}
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 15th, 2012, 04:16 AM
#4
Re: Receive Paremeters on Multithreaded Server
I don't remember Sockets in VB.Net having a DataArrival event. They use Receive or BeginReceive to get data. Where did that event come from ?
-
Nov 15th, 2012, 02:40 PM
#5
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
It gets passed into the function from this event. Should this have more or different parameters?
vb Code:
Public Event DataArrival(ByVal message As String)
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 15th, 2012, 02:42 PM
#6
Re: Receive Paremeters on Multithreaded Server
Originally Posted by rex64
It gets passed into the function from this event. Should this have more or different parameters?
vb Code:
Public Event DataArrival(ByVal message As String)
What class is that event declared in ? The Socket class in the .Net framework has no such event.
-
Nov 15th, 2012, 04:36 PM
#7
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Here is the stream watcher:
vb Code:
''' <summary> ''' Watches a stream while a user is connected? Stays alive while user is connected? ''' </summary> Private Sub streamWatcher(ByVal objStream As Object) Try Dim curStream As NetworkStream = CType(objStream, NetworkStream) Do Until Disposing = True Or curStream Is Nothing If curStream.DataAvailable Then Dim message As String = receiveText(curStream) RaiseEvent(DataArrival(message)) End If Threading.Thread.Sleep(10) Loop Catch End Try End Sub
And he is called from here:
vb Code:
Dim t As New Threading.Thread(AddressOf streamWatcher) t.Start(client.GetStream) client = Nothing
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 03:45 AM
#8
Re: Receive Paremeters on Multithreaded Server
You're using the TcpClient class....its documented in the MSDN. Read here. It will show you how to send data.
-
Nov 16th, 2012, 11:25 AM
#9
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
I looked at that, but we are doing multi-threaded and I am not sure how to check which client is communicating with the server. Sorry, I am a little new to multi-threaded networking.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 01:12 PM
#10
Re: Receive Paremeters on Multithreaded Server
You didn't define that DataArrival event properly....well at least according to the standard practice. Notice that all events in the framework have two parameters, a "sender As Object" parameter and and one based on an EventArgs object. This pattern ensures you can always get a reference to the object that raised the event through the "sender" parameter.
-
Nov 16th, 2012, 01:23 PM
#11
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Ok. Can I raise the event like this:
vb Code:
RaiseEvent DataArrival(Me, message)
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 02:55 PM
#12
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
How can I keep track of which client index this belongs with? I have a ClientInfo list that I add clients to when they connect.
vb Code:
Public Structure ClientInfo
Dim ip As System.Net.IPEndPoint
Dim username As String
Dim stream As NetworkStream
Dim status As connectionStatusEnum
Dim lastPing As DateTime
End Structure
Public clientList As New List(Of ClientInfo)
''' <summary>
''' Watches a stream while a user is connected? Stays alive while user is connected?
''' </summary>
Private Sub streamWatcher(ByVal objStream As Object)
Try
Dim curStream As NetworkStream = CType(objStream, NetworkStream)
Do Until Disposing = True Or curStream Is Nothing
If curStream.DataAvailable Then
Dim message As String = receiveText(curStream)
RaiseEvent DataArrival(Me, message) 'I added 'me' here<<<<<<<<<<<
End If
Threading.Thread.Sleep(10)
Loop
Catch
End Try
End Sub
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 03:32 PM
#13
Re: Receive Paremeters on Multithreaded Server
So let me get this straight, you want your streamWatcher sub to be able to identify which client receives data before raising DataArrival ? Is this correct ?
-
Nov 16th, 2012, 04:02 PM
#14
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
I just need to know which client or thread index the data is coming from. What do you recommend? I think I have multiple stream watchers. Maybe each one should pass in an index or a reference to the ClientInfo?
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 04:04 PM
#15
Re: Receive Paremeters on Multithreaded Server
What is the class that contains that code you posted in post #12 ? What is its purpose ?
-
Nov 16th, 2012, 04:34 PM
#16
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
This is a networking class we have written to work with a 3D game we are creating.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 04:36 PM
#17
Re: Receive Paremeters on Multithreaded Server
Yes I understand but what does this class in particular do ? Could you post the code for the entire class ?
-
Nov 16th, 2012, 05:11 PM
#18
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 05:31 PM
#19
Re: Receive Paremeters on Multithreaded Server
Impressive....did you write all that ? However I must complain that this class doesn't really seem to have a single purpose. It has stuff that seems to imply its some kind of server and yet also code that implies its a client. I really think you should separate these things. If you create a class to handle clients alone then it should come naturally to determine which client received or sent a message. What you have now are basically a collection of functions and I can see now why you're having trouble. Its a bad design. Sit back and really think about how you're designing these classes.
For example, I have a Client/Server application and every connection is represented by a kind of socket object I made. It raises events when it receives or sends data and I've no trouble determining who sent or received what because its all naturally encapsulated in a class instance.
[EDIT]
Hmm...looking more at your code it seems that you did indeed create this class as a client....Can you clear this up ? Does the clsNetEasy class represent a client connection or is it just a collection of network functions ?
-
Nov 16th, 2012, 06:11 PM
#20
Re: Receive Paremeters on Multithreaded Server
Also, take a look at this post where I have attached a project that simulates multiple devices being pinged. It should serve to adequately demonstrate how to identify which device is doing the communication at any given time and also how to multi-thread it properly.
Last edited by Niya; Nov 16th, 2012 at 06:14 PM.
-
Nov 16th, 2012, 08:54 PM
#21
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
It looks like you are creating a different instance of the class for each thread to keep track of them. Is that what you recommend I do? It sounds like it would use more resources, but it should work better. Let me know your thoughts, and if I figured out the puzzle
It looks like I am creating the threads on the fly, and not managing them very well. Should I have a list of threads? Or a list of classes with a single thread in each one? Or what is the best?
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 16th, 2012, 09:11 PM
#22
Re: Receive Paremeters on Multithreaded Server
Originally Posted by rex64
It looks like you are creating a different instance of the class for each thread to keep track of them. Is that what you recommend I do?
Not only do I recommend it...I strongly recommend it.
Originally Posted by rex64
...It sounds like it would use more resources...
Well we are not in the DOS era anymore where you only had 640KB of memory to play with. You can go hog wild in this case. Its more than worth it.
Originally Posted by rex64
It looks like I am creating the threads on the fly, and not managing them very well. Should I have a list of threads? Or a list of classes with a single thread in each one? Or what is the best?
You should have a list of objects and each of these objects can manage their own threads internally. You shouldn't have to interact with these threads outside of the object. Interact with the object's methods, properties and events.
-
Nov 19th, 2012, 02:40 PM
#23
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Ok, so this is what I am planning:
clsNetClient (connects to the server)
clsNetServer (has list of clsNetServerConnection)
clsNetServerConnection (each connection will be its own class)
Originally Posted by Niya
You should have a list of objects and each of these objects can manage their own threads internally. You shouldn't have to interact with these threads outside of the object. Interact with the object's methods, properties and events.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 19th, 2012, 06:43 PM
#24
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Here is my plan. Let me know your thoughts.
Last edited by rex64; Nov 19th, 2012 at 07:10 PM.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 19th, 2012, 07:32 PM
#25
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Should I have one total or 1 per client of these:
vb Code:
Dim server As TcpListener = Nothing
And what about this?
vb Code:
Dim connection As TcpClient = Nothing
Last edited by rex64; Nov 19th, 2012 at 07:49 PM.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 20th, 2012, 04:31 PM
#26
Re: Receive Paremeters on Multithreaded Server
Originally Posted by rex64
Here is my plan. Let me know your thoughts.
Yes this is a very good design. Its exactly what I had in mind.
Originally Posted by rex64
Should I have one total or 1 per client of these:
vb Code:
Dim server As TcpListener = Nothing
And what about this?
vb Code:
Dim connection As TcpClient = Nothing
Well clients don't need to listen do they
-
Nov 20th, 2012, 04:39 PM
#27
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Should each client connection on the server have its own TcpListner?
Dim server As TcpListener = Nothing
So if 100 clients were connected I would have:
QTY1: clsNetServer
QTY100: clsNetConnection
And on the Client's computer, each would have their own clsNetClient.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 21st, 2012, 05:00 PM
#28
Re: Receive Paremeters on Multithreaded Server
Originally Posted by rex64
Should each client connection on the server have its own TcpListner?
What is a TcpListener for ?
-
Nov 21st, 2012, 05:44 PM
#29
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
I think TcpListener is for watching an ip address and port. So I am thinking I should only need 1 of these. Are the clients just network streams? To disconnect a client do you just dispose of the stream?
vb Code:
''' <summary>
''' Starts hosting...
''' </summary>
Public Sub hostServer(ByVal username As String, ByVal ServerString As String)
If myIpInfo.Count > 0 Then
server = New TcpListener(IPAddress.Parse(myIpInfo(0).IPAddress), serverPort) '1244
server.Start()
End If
_ServerString = ServerString
serverThread = New Threading.Thread(AddressOf startHosting)
serverThread.Start(username)
End Sub
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 21st, 2012, 08:11 PM
#30
Re: Receive Paremeters on Multithreaded Server
A server's job is to listen for client connections, not just in this scenario but in any scenario where there is a server/client paradigm. In this case I'm assuming you are opting to encase a TcpListener within a larger server class. The TcpListener has an AcceptTcpClient method which returns a TcpClient object when a client attempts to make a connection. The TcpClient object represents the connection between the specific client machine and the server So just think of it, you have one server machine that can accept incoming connections from multiple client machines, each client having its own stream. If you have a client class that wraps a TcpClient object inside then you wouldn't need a TcpListener. A server listens not a client.
If you want to close a connection then you do so through the TcpClient class which has a Close method. All your communication would be through a TcpClient object on both ends. The TcpListener is only responsible for creating TcpClient objects for communication between client and server.
-
Nov 21st, 2012, 08:14 PM
#31
Re: Receive Paremeters on Multithreaded Server
You questioning gives me an instinctive feeling that you don't fully understand the Client/Server paradigm. I suggest you read this to wrap your head around it.
-
Nov 23rd, 2012, 12:40 PM
#32
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Thanks. I do understand the client server, but I am trying to learn the best way to implement it in VB .NET. So if I understand this correctly:
clsNetServer would have a single TcpListener. When a client connect to the server the TcpListener would automatically create a TcpClient object which I could then pass to the constructor of ClsNetConnection. Does that sound about correct? I would not need a stream because the TcpClient contains a stream. Hope you enjoyed Thanksgiving!
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 23rd, 2012, 02:04 PM
#33
Re: Receive Paremeters on Multithreaded Server
Originally Posted by rex64
Thanks. I do understand the client server, but I am trying to learn the best way to implement it in VB .NET. So if I understand this correctly:
clsNetServer would have a single TcpListener. When a client connect to the server the TcpListener would automatically create a TcpClient object which I could then pass to the constructor of ClsNetConnection. Does that sound about correct? I would not need a stream because the TcpClient contains a stream.
Ya...you got the idea. The TcpClient however is not really created automatically, not by the TcpListener anyway. A call to AcceptTcpClient blocks until a client establishes a connection. Only then it returns a TcpClient.
Originally Posted by rex64
Hope you enjoyed Thanksgiving!
Well we don't celebrate thanks giving but thank you all the same
-
Nov 23rd, 2012, 08:41 PM
#34
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Things are working better, but for some reason I think the stream may be getting damaged. I can send data to the server and it receives it but I am not getting any data back on the stream after I run a few commands. It goes to the sendData inside the else statement, but the client (on the same computer) does not receive the chat messages. Other things seem to arrive ok. We have been having weird problems like this for a while. It seems like if we comment certain things out that it works, but we have not figured out why. Right now I do not see any reason it should not be working at this point.
What tests do you recommend on the stream?
vb Code:
''' <summary>
''' Sends text on the stream to the server.
''' </summary>
Public Sub sendText(ByVal message As String)
If stream Is Nothing Then
If connection IsNot Nothing Then
If connection.Connected Then
sendData(System.Text.Encoding.ASCII.GetBytes(message), connection.GetStream)
End If
End If
Else
sendData(System.Text.Encoding.ASCII.GetBytes(message), stream)
End If
End Sub
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 24th, 2012, 12:00 PM
#35
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Also, it looks like this stream loop is not active all the time. Should this loop be running the entire time that it is connected? Or should this close at some point? Is this a good way to do it?
vb Code:
''' <summary> ''' Watches a stream while a user is connected? Stays alive while user is connected? ''' </summary> Private Sub streamWatcher(ByVal objStream As Object) Try Dim curStream As NetworkStream = CType(objStream, NetworkStream) Do Until Disposing = True Or curStream Is Nothing If curStream.DataAvailable Then Dim message As String = receiveText(curStream) RaiseEvent DataArrival(Me, message) End If Threading.Thread.Sleep(10) Loop Catch End Try End Sub
Last edited by rex64; Nov 24th, 2012 at 12:10 PM.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 25th, 2012, 11:51 AM
#36
Re: Receive Paremeters on Multithreaded Server
The loop should be running the entire time on another thread. It should exit on a disconnect. This is what I do for my network applications.
-
Nov 26th, 2012, 10:20 AM
#37
Thread Starter
Fanatic Member
Re: Receive Paremeters on Multithreaded Server
Ok, I think I am understanding everything. The loop is getting stuck in startHosting. Disposing is = to true in the debugger, but it does not seem to get off that line of code. It has a green arrow and when I press F8 (step into) or Start it acts like it is playing (which is weird for F8 (Step Into)). Then when I press pause it is right back on that exact same line of code.
I may be crazy, but looking at this code closer, during normal running, isn't this stuck in an infinite loop of creating threads? This sounds very bad
It is getting stuck on this line:
client = server.AcceptTcpClient()
In here:
vb Code:
''' <summary> ''' Used by host server. ''' </summary> Private Sub startHosting(ByVal username As Object) Dim client As TcpClient _username = CType(username, String) 'before anyone tries changing this, it MUST BE AN OBJECT ARRAY!!!! 'it stores a TCPClient and a string, so explicit typing WON'T work. -Ray Do Until _close Or Disposing = True Try client = server.AcceptTcpClient() Dim myThread As New Threading.Thread(AddressOf clientWorker) myThread.Start(client) Catch _close = True End Try Loop End Sub
Last edited by rex64; Nov 26th, 2012 at 10:28 AM.
I use VB .NET 2022. Currently developing StudyX educational software, PlazSales POS system and Yargis a space ship shooter game.
-
Nov 26th, 2012, 02:45 PM
#38
Re: Receive Paremeters on Multithreaded Server
client = server.AcceptTcpClient() is a blocking call. I expect it would only proceed after receiving a connection in which case the loop would proceed and eventually reach right back there. startHosting should be running on another thread so as to prevent locking up the UI.
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
|