This discussion is archived
3 Replies Latest reply: Jul 9, 2013 4:13 AM by PhHein RSS

Sending Data to TCP Client gets Hanged due to heavy data from UDP

praveen2609 Newbie
Currently Being Moderated

I have a program which makes a TCP Connection with multiple clients and stores the socket connection and client id in HashMap and sends the data to client by reading the data from UDP Socket.

Please find the code below and let me know as it work perfectly for some time but after sometimes the data does not get read as the UDP Packets i receive is more than 100 packets in 1 sec.

 

public class multiServer

{

    static protected HashMap<String, String> secMapMulti = new HashMap<String, String>();

    static protected HashMap<String,PrintWriter> writeAndClientId = new HashMap<String,PrintWriter>();

    static PrintWriter pw_nse_eq=null;

    static String nse_eq_client_id = "";

    static String[] split_str_data_nse_eq;

 

 

    public static void getStockConfig()

    {

       try

       {

         if(!hasmap_gen)

         {

           if(!checkDbCon())

           {

             connectDb();

           }

           int i=0;

           rs = stmt.executeQuery("SELECT login_id||'-'||mkt_watch_category||'-'||scrip_id as client_scrip_id,scrip_id||'-'||mkt_watch_category as scrip_id FROM                its_mkt_watch_config");

           while(rs.next())

          {

           secMapMulti.put(rs.getString("client_scrip_id"), rs.getString("scrip_id"));

           i++;

          }

          stocks_length = i;

          hasmap_gen = true;

        }

      }

      catch(Exception e) {}

    }

 

 

public static synchronized void addWriter(PrintWriter out,String client_id,String mw_name)

{

    if(writeAndClientId.containsKey(client_id+"-"+mw_name))

    {

        writeAndClientId.remove(client_id+"-"+mw_name);

        writeAndClientId.put(client_id+"-"+mw_name,out);

    }

    else

      writeAndClientId.put(client_id+"-"+mw_name,out);

}

 

public static synchronized void serveAllWritersNseEQ(String data)

{

      split_str_data_nse_eq = data.split(",");

 

      for(Map.Entry<String,PrintWriter> entry : writeAndClientId.entrySet())

      {

        pw_nse_eq = entry.getValue();

        nse_eq_client_id = entry.getKey();

        if(secMapMulti.containsKey(nse_eq_client_id+"-"+split_str_data_nse_eq[2]))

        {

          pw_nse_eq.println(data);

        }

      }

}

 

public static void main(String[] args)

{

     try

     {

       ServerSocket servsock = new ServerSocket(8858);

       Socket incoming;

       getStockConfig();

 

       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;

      BufferedReader in = null;

      String cliString=null;

      private EventNotifier en;

      int id;

 

      public static String udp_data;

      public static String prev_udp_data;

      private static String[] user_object;

      private boolean prev_str_obj = false;

 

      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();

            user_object = cliString.split(",");

 

            if(user_object.length<2)

            {

              multiServer.client_id = user_object[0];

              multiServer.addWriter(out,user_object[0],"DEFAULT");

            }

            else

            {

              multiServer.client_id = user_object[0];//user_object[0]

              multiServer.addWriter(out,user_object[0],user_object[1]);

            }

            int ictr =0;

            while(true)

            {

              try

               {

                 udp_data = en.getDataFeedNSEEQ(cliString);

                 if(udp_data!=null && udp_data.length()>0)

                 {

                   System.out.println("NSE EQ :: "+udp_data.trim());

                 }

               }

               catch (Exception e)

               {

                 System.out.println("yup it blew up here 1 NSEEQ "+e);

               }

               Thread.sleep(1);

           }

      }

      catch(IOException e)

      {

         System.out.print("IO Exception :: "+ e);

      }

      catch(InterruptedException e)

      {

         System.out.print("boom goes the dynamite :: "+ e);

      }

   }

}

 

class EventNotifier

{

      private InterestingEvent ie;

      public static String modifiedSentence;

      public static int length;

      public static String modified_str;

      public DatagramSocket clientSocket;

      public String[] split_str;

      byte[] receiveData;

 

      public EventNotifier(InterestingEvent event)

      {

        try

        {

           clientSocket = new DatagramSocket(6758);

           receiveData = new byte[1024];

        }

        catch(Exception e3)

        {}

      }

 

      public String getDataFeedNSEEQ(String client_id)

      {

          try

          {

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

            receivePacket.setLength(1024);

            clientSocket.receive(receivePacket);

            modifiedSentence = new String(receivePacket.getData());

            length = modifiedSentence.indexOf('#');

            modified_str = modifiedSentence.substring(0,length);

            split_str = modified_str.split(",");

            if(modified_str.length()>0)

            {

                  if(multiServer.c_secMapMulti.containsKey(split_str[2]))

                        return modified_str;

                  else

                        return "";

            }

            else

            {

                  return "";

            }

          }

          catch(Exception e3) {}

      return "";

    }

}

 

Please suggest if any problem in the code to re-correct the same.

 

Thanks

Praveen Tallakokula

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points