Go Premium for a chance to win a PS4. Enter to Win


Application with threads and a UPD listener

Posted on 2008-10-23
Medium Priority
Last Modified: 2008-11-01
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.
Question by:jeaguado
  • 6
  • 5
LVL 19

Expert Comment

ID: 22793013
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


Author Comment

ID: 22794057
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.

Author Comment

ID: 22794086
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.  
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 19

Expert Comment

ID: 22794139
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.

Author Comment

ID: 22794912
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.
LVL 19

Expert Comment

ID: 22796490
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?

Author Comment

ID: 22800854
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.
LVL 19

Expert Comment

ID: 22819039
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?

Author Comment

ID: 22819819
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.
LVL 19

Expert Comment

ID: 22828675
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?
LVL 19

Accepted Solution

drichards earned 750 total points
ID: 22828681
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question