Solved

Application with threads and a UPD listener

Posted on 2008-10-23
11
368 Views
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.
0
Comment
Question by:jeaguado
  • 6
  • 5
11 Comments
 
LVL 19

Expert Comment

by:drichards
Comment Utility
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)

        Next

        Console.WriteLine("Press enter to finish...")

        Console.ReadKey()

        client.Close()

    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

        Try

            data = udpConnection.EndReceive(Result, remoteEP)

            Console.WriteLine(ASCIIEncoding.ASCII.GetString(data))

            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

0
 

Author Comment

by:jeaguado
Comment Utility
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.
0
 

Author Comment

by:jeaguado
Comment Utility
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.  
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
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.
0
 

Author Comment

by:jeaguado
Comment Utility
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 19

Expert Comment

by:drichards
Comment Utility
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?
0
 

Author Comment

by:jeaguado
Comment Utility
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.
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
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?
0
 

Author Comment

by:jeaguado
Comment Utility
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.
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
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?
0
 
LVL 19

Accepted Solution

by:
drichards earned 250 total points
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now