// Javaによるサーバーアプリケーション // // 2002/ 6/ 5 宍戸 輝光 // import java.io.*; import java.net.*; public class fserver { public static void main(String args[]) { fserver app=new fserver(); app.server(); } public void server() { ServerSocket ss=null; Socket sc=null; OutputStream os=null; InputStreamReader isr=null; int c=0; String line=null; boolean go=true; try { // サーバーソケット作成 ss=new ServerSocket(7777); } catch (Exception e) { System.out.println("Error"); return; } while (go) { // サーバー処理ループ try { // 接続受け入れ sc=ss.accept(); } catch (Exception e) { System.out.println("Accept error"); return; } try { os=sc.getOutputStream(); isr=new InputStreamReader(sc.getInputStream()); } catch (Exception e) { System.out.println("getStreams error"); return; } String mes="Test Server!\r\n"; try { os.write(mes.getBytes()); } catch (Exception e) { System.out.println("Write error"); return; } boolean logon=true; while (logon) { boolean bRead=true; StringBuffer sb=new StringBuffer(); while (bRead) { // 入力文字列受取 try { c=isr.read(); } catch(Exception e) { bRead=false; } if (c=='\r' || c=='\n') bRead=false; else sb.append((char)c); } line=sb.toString(); if (line.length()>0) { mes="Command:"+line+"\r\n"; try { os.write(mes.getBytes()); } catch (Exception e) { System.out.println("Write error"); return; } if (line.equals("host")) { mes=ss.toString()+"\r\n"; try { os.write(mes.getBytes()); } catch (Exception e) { System.out.println("Write error"); return; } } if (line.equals("local")) { mes=sc.toString()+"\r\n"; try { os.write(mes.getBytes()); } catch (Exception e) { System.out.println("Write error"); return; } } if (line.equals("logout")) logon=false; if (line.equals("exit")) { try { sc.close(); } catch (Exception e) { System.out.println("Close error"); return; } logon=false; go=false; } } } try { sc.close(); } catch (Exception e) { System.out.println("Close error"); return; } } try { ss.close(); } catch (Exception e) { System.out.println("Close error"); return; } } }