import java.io.*; import java.net.*; import java.util.*; import java.lang.Math; public class Server { public static void main(String args[]) { byte bKbdInput[] = new byte[256]; //buffer ServerSocket ss; Socket s; InputStream is; //potoki OutputStream os; try { System.out.println( "Socket Server Application"); } catch(Exception ioe) { System.out.println(ioe.toString()); } try { ss = new ServerSocket(9999); //adres - proizvolnqi s = ss.accept(); is = s.getInputStream(); os = s.getOutputStream(); byte buf[] = new byte[512]; int lenght; while(true) { lenght = is.read(buf); if (lenght == -1) break; String str = new String(buf, 0); StringTokenizer st; //otrezaem lishnee st = new StringTokenizer(str, "\r\n"); str = new String((String)st.nextElement()); StringBuffer strbuf; if (str.equals("getIt")) { str=Double.toString(Math.random()); } else { str="nothing"; }; str=str +"\n"; strbuf=new StringBuffer(str); //peredelaem stroku v massiv for(int i = 0; i < str.length(); i++) { buf[i] = (byte)strbuf.charAt(i); } os.write(buf, 0, str.length()); //posqlaem os.flush(); //opustoshit' buffer } is.close(); os.close(); s.close(); ss.close(); } catch(Exception ioe) { System.out.println(ioe.toString()); } try { System.out.println( "Press to terminate application..."); System.in.read(bKbdInput); } catch(Exception ioe) { System.out.println(ioe.toString()); } } }