最近又练习了一下sokect通信的程序 吃了点苦头 但又感觉到一丝甜蜜呵呵 得到一点经验 那就是要习惯用flush()几乎所有的输出流都有flush() 我太菜了 !!不用这个方法出现的情况就是: 1、你的程序没有编译错误 2、你的程序不会抛出任何异常 3、你的程序只会在readLine()处等待,让你不知所措!! 下面是服务器的代码 /** * @(#)NetServer.java * * * @author * @version 1.00 2008/4/20 */ package samples; import java.net.ServerSocket; import java.net.Socket; import java.net.InetAddress; public class NetServer extends ServerSocket implements Runnable{ public NetServer() throws java.io.IOException{ super(); } public NetServer(int port) throws java.io.IOException{ super(port); } public void decide(){//判断得到命令该执行哪段程序 } public int read(Socket client){//读入来自客户端的命令并将其转化为相应的整型变量 int cmd=0; String scmd=""; try{ System.out.println("Begin To Read"); java.io.BufferedReader in=new java.io.BufferedReader( new java.io.InputStreamReader( client.getInputStream() ) ); if(in.ready()==false){//输入流是否准备好! scmd=in.readLine(); } System.out.println("From client:"+scmd); cmd=1; }catch(java.io.IOException ioe){ System.out.println(ioe.toString()); } return cmd; } public void run(){ int j=6; try{ Socket client; System.out.println("this is server running:"); client=this.accept();//等待客户端 while(true){ if(client!=null){ System.out.println("reading……"); this.read(client);//读取来自客户端的信息 } j--; new Thread(this).sleep(0); } }catch(InterruptedException e){ System.out.println(e.toString()); }catch(java.io.IOException ioe){ System.out.println(ioe.toString()); } } public static void main(String args[]){ try{ NetServer ns=new NetServer(3333); new Thread(ns).start();//启动服务器 }catch(java.io.IOException ioe){ System.out.println(ioe.toString()); } } } 下面是客户端的代码: /** * @(#)NetClient.java * * * @author * @version 1.00 2008/4/20 */ package samples; import java.net.ServerSocket; import java.net.Socket; import java.net.InetAddress; import java.net.SocketAddress; import java.io.BufferedReader; public class NetClient extends Socket implements Runnable{ public NetClient() { super(); } public NetClient(java.net.InetAddress address, int port) throws java.io.IOException { super(address,port); } public NetClient(InetAddress address, int port, InetAddress localAddr, int localPort) throws java.io.IOException{ super(address,port,localAddr,localPort); } public void linkToServer(SocketAddress server){ try{ this.connect(server); }catch(java.io.IOException ioe){ System.out.println(ioe.toString()); }catch(java.nio.channels.IllegalBlockingModeException ibm){ System.out.println(ibm.toString()); }catch(java.lang.IllegalArgumentException iae){ System.out.println(iae.toString()); } } public String getCMD(){//读取用户输入的命令,其实就是一段字符串 呵呵 System.out.println("Please input your Command:"); try{ //这里先假设程序能正确读入命令;并且用户也不是傻子--不输命令; BufferedReader i= new BufferedReader(new java.io.InputStreamReader(System.in)); String s=i.readLine(); System.out.println("s:"+s); return s;//new BufferedReader(new java.io.InputStreamReader(System.in)).readLine(); }catch(java.io.IOException ioe){ System.out.println(ioe.toString()); System.out.println("Please input your Command twice:"); return "error"; } } public void toServer(String cmd){//将命令发往服务端 try{ java.io.PrintWriter pw=new java.io.PrintWriter( new java.io.OutputStreamWriter(this.getOutputStream()) ); pw.println(cmd); pw.flush();//就是这个flush()让我郁闷了好几天!! }catch(java.io.IOException ioe){ System.out.println(ioe.toString()); } } public void run(){ int i=6; while(i>=0){ this.toServer(this.getCMD());//将命令发往服务器 try{ new Thread(this).sleep(1000); }catch(InterruptedException e){ System.out.println(e.toString()); } } } public static void main(String args[]){ try{ //启动客户端,并连接至指定端口 NetClient nc=new NetClient(java.net.InetAddress.getLocalHost(),3333);//,java.net.InetAddress.getLocalHost(),2222); new Thread(nc).start(); }catch(java.io.IOException ioe){ System.out.println(ioe.toString()); } } } |
...
2008年4月22日星期二
Socket通信实验-经验总结【原】
订阅:
博文评论 (Atom)
没有评论:
发表评论