java - Writing messages to client socket using DataOutputStream to Server Socket only sent after closing the Client Socket why? -
i had socket-client programming in java . using dataoutputstream send messages server socket . writed messages on dataoutputstream not sent serversocket . think it's due not flushing after sent message . no use . if terminate class execution receive messages serversocket .
my code :
public class loggingclient { linkedblockingqueue<byte[]> messages = new linkedblockingqueue<byte[]>(); public static loggingclient clientobj; /* * waiting 2 seconds connecting centralized log server . if it's not reachable writing log's in local machine * log's collected on eclipse restart. */ public static final int socket_timeout = 2000; /** * server log server running. */ public static final string server = "bharathi"; /** * port log server running. */ public static final int port = 10000; /** * client socket used connect server . */ socket client = null; public static logger logger = logger.getlogger(loggingclient.class.getname()); /** * used write given logs client output stream. */ dataoutputstream out = null; public boolean isconnected = false; /** * used preventing instaniate loggingclient without calling getinstance method. */ private loggingclient(){ } /** * clear's socket , initialize new socket . */ public void init(){ try{ clear(); client = new socket(); client.settcpnodelay(true); client.connect(new inetsocketaddress(server,port),socket_timeout); //trying make connection within 2 seconds. out = new dataoutputstream(client.getoutputstream()); isconnected = true; }catch(exception e){ isconnected = false; } } public static loggingclient getinstance(){ if(clientobj == null){ clientobj = new loggingclient(); } return clientobj; } public void clear(){ try{ if(out != null){ out.close(); //if call close method invokes flush , closes dataoutputstream. } if(client != null){ client.close(); } }catch(exception e){ logger.log(level.info,"error while closing connection , reason {0}",e); }finally{ try{ client.close(); out.close(); }catch(exception e){ } isconnected = false; } } /** * adding message queue . scheduled thread logs , push central logging server. * @param message */ public synchronized void write(byte[] message){ if(!isconnected){ //has connection. init(); } messages.add(message); //adding message queue . background thread collect log message , sent central log server. } /** * sending logs central log server . if it's not reachable write local machine. * @param message */ public void sendlog(byte[] message){ try { out.write(message); out.flush(); } catch (exception e) { writeinlocalmachine(localloggingpath, message); //in case of failure writing logs in local machine . sync logs when restart of eclipse. } } /** * writing log's machine log synced on plugin startup. * @param file - file path. * @param message - message log. */ public static void writeinlocalmachine(string file, byte[] message) { fileoutputstream filewriter = null; file f = new file(file); try { if(!f.exists()){ f.createnewfile(); } filewriter = new fileoutputstream(file, true); filewriter.write(message); filewriter.flush(); } catch (exception e) { logger.log( level.warning, "this may due given file not found in system , reason {0}", e); } { try{ filewriter.close(); }catch(exception e){ } } } /** * @return - received message queue . returns null if it's empty. */ public byte[] getmessage(){ return messages.poll(); //returns head element , deletes it. } }
testing java class sending messages server socket .
public class logtest implements runnable { public static final string line_separator = system.getproperty("line.separator"); @override public void run() { while(true){ try{ loggingclient client = loggingclient.getinstance(); byte[] message = client.getmessage(); if(message != null){ client.sendlog(message); } }catch(exception e){ } } } public static void startsending(){ for(int i=0;i<10000;i++){ string msg = "msg number" + i+line_separator; loggingclient.getinstance().write(msg.getbytes()); } } public static void main(string args[]){ loggingclient c = loggingclient.getinstance(); system.out.println("start time " + system.currenttimemillis()); thread t = new thread(new logtest(),"log messager"); t.start(); startsending(); system.out.println("end time " + system.currenttimemillis()); } sent messages stored in file .
ouput :
start time 1340815857896 end time 1340815858063 finished putting messages queue . infinite while loop take care of sending logs server socket .
content stored in file 0 bytes why ? . if stop running class receive sent messages why?
you have posted code, 99 times out of 100 in these cases see data after stream / program has closed problem never flushing data after written
Comments
Post a Comment