jQuery in Action, 2nd edition*
The moose likes Sockets and Internet Protocols and the fly likes Sending Data to TCP Client gets Hanged after some interval Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sending Data to TCP Client gets Hanged after some interval" Watch "Sending Data to TCP Client gets Hanged after some interval" New topic
Author

Sending Data to TCP Client gets Hanged after some interval

praveen tallakokula
Greenhorn

Joined: May 23, 2013
Posts: 1
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.

import java.net.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.util.HashMap;
import javax.xml.parsers.*;

import org.xml.sax.InputSource;
import org.w3c.dom.*;
import org.w3c.dom.CharacterData;
import java.net.URL;

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)
{
System.out.println("InStock Config !!!");
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;
//rs.close();
}
}
catch(Exception e)
{}
}


public static synchronized void addWriter(PrintWriter out,String client_id,String mw_name) //add third parameter as String market_watch_name
{
if(writeAndClientId.containsKey(client_id+"-"+mw_name)) //client_id-market_watch_name
{
writeAndClientId.remove(client_id+"-"+mw_name); //client_id-market_watch_name
writeAndClientId.put(client_id+"-"+mw_name,out); //change first parameter to client_id-market_watch_name
}
else
writeAndClientId.put(client_id+"-"+mw_name,out);//change first parameter to client_id-market_watch_name
}

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);
System.out.println("servsock :: "+servsock);
Socket incoming;
System.out.println("The Broadcast server is running.");
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();//PR001,market_watch1
user_object = cliString.split(","); //add these to split the client sent data
System.out.println("user_object :: "+user_object.length);

if(user_object.length<2){
System.out.println("user_object :: 1 "+user_object.length+" :: "+cliString);
multiServer.client_id = user_object[0];//user_object[0]
multiServer.addWriter(out,user_object[0],"DEFAULT");//add one more parameter as market_watch_name i.e. (multiServer.addWriter(out,user_object[0],user_object[1]))
}
else{
multiServer.client_id = user_object[0];//user_object[0]
multiServer.addWriter(out,user_object[0],user_object[1]);//add one more parameter as market_watch_name i.e. (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)
{
System.out.println("in NSE EQ");
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 recorrect the same.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sending Data to TCP Client gets Hanged after some interval