Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8151451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:23:36+00:00 2026-06-06T15:23:36+00:00

Im facing one problem in streaming data capture for reading the broadcast data during

  • 0

Im facing one problem in streaming data capture for reading the broadcast data during multithreading, pls help or suggest,

Actually there is one class which is reading data from one of the udp socket. Another class accepts the tcp connection from every client request, creates a thread for every client and request the same udp class for data. The thing is working with 1st thread which gets created. But when i request with another client from another pc/ip the packets get losted to the 2nd client/thread

I have made a workaround by creating a list where im storing the Threads outputstream object
and looping it to send the data to all the client. But this is just temporary as it ll delay the packets if clients/connections gets increased.

code for reading UDP Data

   public class EventNotifier    
    {
      private InterestingEvent ie;
      public DatagramSocket clientSocket;
      public String[] split_str;

      byte[] receiveData;

      HashMap<String, String> secMap = new HashMap<String, String>();


      public EventNotifier(InterestingEvent event)
      {
        ie = event; 
     clientSocket = new DatagramSocket(9050);
        receiveData = new byte[500];
      }



      public String getDataFeed(String client_id)
      {
        try 
        {
             DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
             clientSocket.receive(receivePacket);
             String s = new String(receivePacket.getData());
             String split_str = s.split(","); 
             if(secMap.containsValue(split_str[0]))
                  return s;
             else
                  return "";
             } catch(Exception e3) {}
      }
     }// end of eventNotifier class

code for multithreading handling client requests

public class multiServer 
{

    static protected List<PrintWriter> writers = new ArrayList<PrintWriter>();
    static String client_id = "";
    public static void main(String[] args)
    { 
       try
       {
           ServerSocket servsock = new ServerSocket(8858);
           Socket incoming;
           while(true)
           {
             incoming = servsock.accept();
             multiServerThread  connection = new multiServerThread(incoming);

             Thread t1 = new Thread(connection);
             t1.start();
           }
       }
       catch(IOException e)
       {
            System.out.println("couldnt make socket");
       }
    }
}

class multiServerThread extends Thread implements InterestingEvent
{

    Socket incoming;
    PrintWriter out=null;
    PrintWriter broad=null;
    BufferedReader in = null;
    String cliString=null;
    private EventNotifier en; 
    int id;

    public static String udp_data;

    public void interestingEvent(String str1)
    {
          this.udp_data = str1;
    }
    public String getUdpData()
    {
      String _udp_data = this.udp_data;
      return _udp_data;
    }
    multiServerThread(Socket incoming)
    {  
        this.incoming=incoming;
        en = new EventNotifier(this);
    } 
    public void run()
    {
        try
        {
            out = new PrintWriter(incoming.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
        cliString = in.readLine();
        multiServer.writers.add(out);
        while(true)
                {
               try
                   {
                    udp_data = en.getDataFeed(cliString);
                    if(udp_data!=null && udp_data.length()>0)
                    {
              //workaround for serving the data to all cleints who are connected    
                        for (int i=0; i<multiServer.writers.size();i++)
                        {
                            broad=multiServer.writers.get(i);
                            broad.println(udp_data.trim());
                        }
             //else will directly write to the outputstream object for every thread which is connected
             // out.println(udp_data.trim());
                    }

               }
               catch (Exception e)
               {
                 System.out.println("exception "+e);
               }
               Thread.sleep(1);

            }
        } catch(IOException e)
        {
            System.out.print("IO Exception :: "+ e);
        }
        catch(InterruptedException e)
        {
            System.out.print("exception "+ e);
        }        
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:23:38+00:00Added an answer on June 6, 2026 at 3:23 pm

    You need mutual exclusion (or a different design).

    For example, what will happen if two threads call multiServer.writers.add(out); concurrently?

    From the ArrayList Javadocs

    Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or […])

    Another problem is two calling udp_data = en.getDataFeed(cliString); concurrently. The second thread might overwrite the result of the first. You’ll loose data!

    What happens if one thread calls for (int i=0; i<multiServer.writers.size();i++) while another thread is busy doing multiServer.writers.add(out);? The size may have increased, before out has actually been added to the list!

    public class multiServer 
    {
    
        private List<PrintWriter> writers = new ArrayList<PrintWriter>();
    
        public synchronized void addWriter(PrintWrite out) {
             writers.add(out);
        }
        public synchronized void serveAllWriters(String data) {
             for (int i=0; i<multiServer.writers.size();i++)
             {
                 broad=multiServer.writers.get(i);
                 broad.println(data);
             }
        }
    }
    

    Now when a thread tries to add a writer, the synchronizeds will make sure no other thread is adding or printing. So multiServerThread should be fixed to use the new methods:

    class multiServerThread extends Thread implements InterestingEvent
    {
          //...
          private String udp_data;
          //...
          myMultiServer.addWriter(out);
    
          //...
          udp_data = en.getDataFeed(cliString);
          if(udp_data!=null && udp_data.length()>0)
                myMultiServer.serveAllWriters(udp_data.trim());
          //...
    }
    

    There might be more problems, not sure I don’t fully understand your code. The question you must ask yourself is, can another thread read and/or write the same data or object? Yes? Then you’ll need proper synchronization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing one problem ,that when the google map is loading then there
We are facing one problem in managing test data (xmls which is used to
I am facing a problem creating a Json which is actually a complex one
i am facing one problem.i have a class named ReportingService and it should be
I am learning OpenCV using Learning OpenCV book. One problem I am facing while
I am facing a problem while disabling the command button after one click. I
Facing a problem, it seems my data stored in SQL Server does not stored
When facing the problem of validating a property in a JSF2 application there are
We are facing one problem with facebook. we integrated FB in our web application,
I am working on one application, i am facing one problem on Internet Explorer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.