正在学计算机网络,照着书上的例子,参考了网上的一段代码,写了这个
在线程那段好像有点问题,运行是会有错误,其它还算正常
SSServer.class
import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class SSServer ...{ /** *//** * @param args * @throws IOException */ public static void main(String[] args) throws IOException ...{ System.out.println ("Server starting... "); ServerSocket server = new ServerSocket (10000); while (true) ...{ Socket s = server.accept (); System.out.println ("Accepting Connection... "); new ServerThread (s).start (); } }}ServerThread.class
import java.io.*;import java.net.*;import java.util.*;public class ServerThread extends Thread ...{ private Socket s; ServerThread (Socket s) ...{ this.s = s; } public void run () ...{ BufferedReader br = null; PrintWriter pw = null; try ...{ InputStreamReader isr; isr = new InputStreamReader (s.getInputStream ()); br = new BufferedReader (isr); pw = new PrintWriter (s.getOutputStream (), true); Calendar c = Calendar.getInstance (); do ...{ String cmd = br.readLine (); if (cmd == null)break;
cmd = cmd.toUpperCase (); if (cmd.startsWith ("BYE"))break;
if (cmd.startsWith ("DATE") || cmd.startsWith ("TIME")) pw.println (c.getTime ().toString ()); if (cmd.startsWith ("DOM")) pw.println ("" + c.get (Calendar.DAY_OF_MONTH)); if (cmd.startsWith ("DOW")) switch (c.get (Calendar.DAY_OF_WEEK)) ...{ case Calendar.SUNDAY : pw.println ("SUNDAY");break;
case Calendar.MONDAY : pw.println ("MONDAY");break;
case Calendar.TUESDAY : pw.println ("TUESDAY");break;
case Calendar.WEDNESDAY: pw.println ("WEDNESDAY");break;
case Calendar.THURSDAY : pw.println ("THURSDAY");break;
case Calendar.FRIDAY : pw.println ("FRIDAY");break;
case Calendar.SATURDAY : pw.println ("SATURDAY"); } if (cmd.startsWith ("DOY")) pw.println ("" + c.get (Calendar.DAY_OF_YEAR)); if (cmd.startsWith ("PAUSE")) try ...{ Thread.sleep (3000); } catch (InterruptedException e)...{} } while (true); } catch (IOException e) ...{ System.out.println(e.toString ()); } finally ...{ System.out.println ("Closing Connection... "); try ...{ if (br != null)br.close (); if (pw != null)pw.close (); if (s!= null) s.close(); } catch (IOException e)...{} } }}SSclient.class
import java.io.*;import java.net.*;public class SSClient ...{ /** *//** * @param args */ public static void main(String[] args) ...{ String host = "localhost"; if (args.length == 1)host = args [0]; BufferedReader br = null; PrintWriter pw = null; Socket s = null; try ...{ s = new Socket (host, 10000); InputStreamReader isr; isr = new InputStreamReader (s.getInputStream ()); br = new BufferedReader (isr); pw = new PrintWriter (s.getOutputStream (), true); pw.println ("DATE"); pw.println ("DATE"); pw.println ("PAUSE"); pw.println ("DOW"); System.out.println (br.readLine ()); System.out.println (br.readLine ()); pw.println ("DOY"); System.out.println (br.readLine ()); } catch (IOException e) ...{ System.out.println (e.toString ()); } finally ...{ try ...{ if (br != null) br.close(); if (pw != null) pw.close(); if (s != null) s.close(); } catch (IOException e )...{} } }}这样的三段代码,可以实现socket的互通信息,供大家参考!