********************* TCL ***************************** proc send_msg {s msg} { set dlugosc [string length $msg] set wyslac [binary format i $dlugosc] append wyslac $msg puts -nonewline $s $wyslac; flush $s } proc recv_msg {s msg} { set x1 [read $s 4] if {$x1=="" && [eof $s]} return if {[string length $x1]!=4} {error "blad recv_msg /1"} binary scan $x1 i dlugosc set x2 [read $s $dlugosc] if {[string length $x2]!=$dlugosc} {error "blad recv_msg /2"} upvar $msg y; set y $x2 } # zapis komunikatow do pliku set f [open qqq.txt w] fconfigure $f -translation binary fconfigure $f -encoding utf-8 send_msg $f "A ku ku !!! 1" send_msg $f "A ku ku !!! 2" close $f # odczyt komunikatow z pliku set f [open qqq.txt r] fconfigure $f -translation binary fconfigure $f -encoding utf-8 recv_msg $f msg; set msg recv_msg $f msg; set msg close $f ********************* Java ***************************** public class mh_kom { static public String recv_msg(java.io.InputStreamReader r) { try { java.nio.ByteBuffer bb= java.nio.ByteBuffer.allocateDirect(4); bb.put(3, new Integer(r.read()).byteValue()); bb.put(2, new Integer(r.read()).byteValue()); bb.put(1, new Integer(r.read()).byteValue()); bb.put(0, new Integer(r.read()).byteValue()); int len= bb.getInt(0); char[] buf= new char[len]; //r.read(buf); // !!! blad !!! // nie zawsze wczytuje sie wszystko! czyta po okolo 20KB... int i,j,k; j=len; k=0; while(j>0) { i= r.read(buf, k, j); j-=i; k+=i; } return new String(buf, 0, len); } catch (java.io.IOException e) { System.out.println("blad recv_msg !!!"); } return ""; } static public void send_msg(java.io.OutputStreamWriter w, String s) { try { int len= s.length(); java.nio.ByteBuffer bb= java.nio.ByteBuffer.allocateDirect(4); bb.putInt(0, len); w.write(bb.get(3)); w.write(bb.get(2)); w.write(bb.get(1)); w.write(bb.get(0)); w.write(s); } catch (java.io.IOException e) { System.out.println("blad send_msg !!!"); } } } // zapis komunikatow do pliku java.io.FileOutputStream os= new java.io.FileOutputStream("qqq.txt"); java.io.OutputStreamWriter w= new java.io.OutputStreamWriter(os, "utf-8"); mh_kom.send_msg(w, "A ku ku !!! 1"); w.flush(); mh_kom.send_msg(w, "A ku ku !!! 2"); w.flush(); w.close(); // odczyt komunikatow z pliku java.io.FileInputStream is= new java.io.FileInputStream("qqq.txt"); java.io.InputStreamReader r= new java.io.InputStreamReader(is, "utf-8"); System.out.println(mh_kom.recv_msg(r)); System.out.println(mh_kom.recv_msg(r)); r.close();