Application with threads and a UPD listener

This is  my first question in Experts-Exchange and I hope you can help me.

I'm starting with Visual Basic 2008. I've been programming using VB6 for 8 years.

I have to start a project I think it willhave three thread. One thread will listed a port using UDP. In this process it will received name of files of two types. The other two sockets have to been waiting to have files of a type to send it to another ip using UDP and delete the file from the collection.

I'd like you help with these problems:

1. I don't know how I can have a files collection which is shared with these three threads and how to manage it to avoid one thread bother another thread while it is using the file collection.

2. I don't know how create a thread which listen from a UPD port.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't understand what you are trying to do exactly, so let's take it in stages.  First, you probably don't need all thise threads.  Here is an example program (after you reference the correct assemblies) that listens on a UDP port and sends data to itself on only one application thread.  It is using OS threads to do the listening and its own thread to do the sending.

This functionality can easily be put off in a class or something.

If I understand correctly, the data you receive will be names of files?  You want to take these files and send them somewhere.  This can be done on separete threads if you want, but doesn't have to be.  Depends mostly on performance considerations.  You could easily queue the requests to a single worker thread.  A few more details would be helpful.
Imports System.Text
    Sub Main()
        Dim client As UdpClient = New UdpClient(New IPEndPoint(IPAddress.Loopback, 47808))
        client.BeginReceive(AddressOf HandleConnection, client)
        Dim client2 As UdpClient = New UdpClient()
        Dim sendTo As IPEndPoint = New IPEndPoint(IPAddress.Loopback, 47808)
        For ii As Integer = 1 To 10
            client2.Send(ASCIIEncoding.ASCII.GetBytes("Hello"), 5, sendTo)
        Console.WriteLine("Press enter to finish...")
    End Sub
    Public Sub HandleConnection(ByVal Result As IAsyncResult)
        Dim remoteEP As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
        Dim udpConnection As UdpClient = CType(Result.AsyncState, UdpClient)
        Dim data() As Byte
            data = udpConnection.EndReceive(Result, remoteEP)
            udpConnection.BeginReceive(AddressOf HandleConnection, udpConnection)
        Catch ex As ObjectDisposedException
            'Just to catch the final exception when the socket is closed
            Console.WriteLine("Socket closed")
        End Try
    End Sub

Open in new window

jeaguadoAuthor Commented:
Thanks for answering. The other machine will receive orders to load files and it have only 6 memory blocks. I need to send only the files which it can receive. When the other machine display the file, I send another file to put it in the memory block which it's free now.
This is the reason I have to use another thread. I can't send all files when I recive them. I only can send the files the other machine can use.
jeaguadoAuthor Commented:
In the moment I receive files name, I can't send all files names to the other machine because it has a limited memory block. I can send the files it can load. When the machine display a file, it will have this memory block as a free block and I can send it another file.

I need to have another thread which send files when I have files to send and the other machine have free memory blocks.

I'm spanish and I thought writing in english I could find more experts.  
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

I still don't think you need additional threads, or at least not more than 1.  I assume the other machine will ask for the files it wants?  So your "server" will wait for requests and send out the files that are requested.  That can be done with no extra threads.  Each time a file is reauested, you just send it, or is there more to it than that?

If I understand the problem correctly, you will have a socket open waiting to receive (the BeginReceive in my example), and based on data received on that socket, you will send files out to some known socket(s) on another machine when that machine requests the files.
jeaguadoAuthor Commented:
No,  the other machine only says me: "Loaded file" when it finished the orden I send it  to load a file. my program will say: "play this file" or "load this file". The other machine only say "Loaded file". My program also decided how many time the other machine has to reproduce the file.
OK, so I am sure that you do not need additional threads since assuming you use the asynchronous network read, your program never waits for anything.  You may want to use a background thread for network sends just in case the network is misbehaving.

So the first thing is to get the initialization code going.  You will need to set up the network read, which I gave you code for already.  The you need some code to perform any initial commuincation with the other programs, if necessary.  How do you bootstrap the proces when your program starts?  Do you automatically try to send some files, or do you wait for some input from the other programs?
jeaguadoAuthor Commented:
My program will be a Windows service.  It will receive some files names from a machine and I have to decide if another machine has free memory block to send the orden to download a file. When the other machine downloaded a file, it send me a message. Sometimes my program will have more files than free memory will have the other machine.
For example,
machine 1 says: file1, file2, file3, file4, fille5
machine 2 only have 2 free memory blocks
I have to send to machine2 the filename to download.
I will say to machine2 reproduce this file. The file have a duration and I wiil have to decide the file have finished the playing and that memory block is free and I can send another order to download a file for this memory block.
How many machines will you be sending files to from the service app and how often will they need to be sent?  The answers to these questions will determine how many and what types of threads you will need.  Since it is a service, I assume you will not need feedback during the file sends about how much of the file has been transferred?
jeaguadoAuthor Commented:
The maximum number of machines will be 20. The machines will answer to my program about if the file is correct or if I have to send thie same file name again.
Based on what you've said, I'd have the main thread handle all the incoming data from the other machines using the asynchronous Read/Send methods.  When it is time to send a file, I'd create a state class that contains the file name, a TcpClient, and maybe an event/delegate to signal completion, and send that off to the system thread pool via ThreadPool.QueueUserWorkItem.  The thread method will handle sending files.

Do the files get sent over the same connection that the other machine opened to request the file?
Sorry, I meant ro say UDP client in that past post.  Might be worth using TCP to send the files, though, since UDP does not guarantee delivery.  You'd have to implement some kind of handshake and error checking to make sure all the parts of the files got sent.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.