Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

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


  3. The Following User Says Thank You to PhHein For This Useful Post:

    jps (July 9th, 2013)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    Welcome praveen2609

    Please see The problems with crossposting

Similar Threads

  1. Need Help to acquire data from a connection UDP
    By clavius11 in forum Java Networking
    Replies: 4
    Last Post: May 3rd, 2013, 06:41 AM
  2. [Asking] Sending and Receiving Data via Serial
    By ardisamudra in forum Java Networking
    Replies: 5
    Last Post: January 23rd, 2013, 06:24 AM
  3. Sending data too fast
    By polyfrag in forum Java Networking
    Replies: 1
    Last Post: August 10th, 2012, 04:28 PM
  4. Sending Data Between PC to Mobile
    By ultrabeat in forum Java Theory & Questions
    Replies: 0
    Last Post: July 23rd, 2011, 09:06 AM
  5. HELP with sending data within text file to client with printwriter
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2011, 02:42 PM

Tags for this Thread