我有一个程序,它与多个客户端建立TCP连接,并在HashMap中存储套接字连接和客户端ID,并通过从UDP套接字读取数据将数据发送到客户端。
请找到下面的代码并告诉我,因为它可以正常工作一段时间,但有时数据无法读取,因为我收到的UDP数据包在1秒内超过100个数据包。
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;
}
}
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)
{
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 "";
}
}
请建议如果代码中的任何问题重新纠正相同的内容。