Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Application with threads and a UPD listener

Posted on 2008-10-23
11
Medium Priority
?
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 19

Expert Comment

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

Author Comment

by:jeaguado
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.  
0
Industry Leaders: 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!

 
LVL 19

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

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

Accepted Solution

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

670 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