设为首页 加入收藏

WWW-4444KK-COM【首页★新址】WWW-4444KK-COM_日韩★WWW-4444KK-COM

当前位置: 主页 > www-W4444KK-com > DIV&CSS >

Exception{ Socket s=new Socket(localhost

时间:2012-11-29 02:01来源:网络整理 作者:管理员 点击:

Exception{ Socket s=new Socket(localhost, JAVA根本高级领导:SOCket编程 2012-10-12 18:04:36 字体放大: SOCket编程 1.广播式聊天 public class TCPClient{ public static void main(String[] args)throws Exception,9000); //从网络中读取数据 InputStream is=s.getInputStream(); InputStreamReader isr=new InputStreamReader(is); BufferedReader br=new BufferecReader(isr); //向网络中写出数据 OutputStream os=s.getOutputStream(); PrintWriter pw=new PrintWrter(os); while(true){ System.out.println(Client:); String str=SystemIn.readString(); pw.printIn(str); pw.flush(); String strread=br.readLine(); System.out.println(Server:+strread); } } } public class TCPServer{ public static void main(String[] args){ ServerSocket ss=new ServerSocket(9000); while(true){ Socket s=ss.accept(); Thread t=new ServerThread(s); t.start(); } } } class ServerThread extends Thread{ Socket s; BufferedReader in; PrintWriter out; public ServerThread(Socket s){ super(); this.s=s; try{ InputStream is=s.getInputStream(); InputStreamReader isr=new InputStreamReader(is); in=new BufferedReader(isr); out=new PrintWriter(s.getOutputStream()); }catch(IOException e){ e.printStackTrace(); } } public void run(){ while(true){ read(); write(); } } public void read(){ String str; try{ str=in.readLine(); System.out.println(Client:+str); }catch(IOException e){ e.printStackTrace(); } } public void write(){ System.out.println(Serc=ver:); String str=SystemIn.readString(); out.println(str); out.flush(); } } 2点对点聊天. public class QQClient{ public static void main(String[] args)throws Exception,Exception{ Socket s=new Socket(localhost。www-36ab-com

8000); Thread t1=new ReceiveThread(s); Thread t2=new SendThread(s); t1.start(); t2.start(); } } class ReceiveThread extends Thread{ Socket s; public ReceiveThread(Socket s) { super(); this.s = s; } public void run(){ try{ InputStream is=s.getInputStream();//创建节点流 InputStreamReader isr=new InputStreamReader(is);//桥转换 BufferecReader in=new BufferedReader(isr);//封装过滤流 //BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream)); while(true){ String str=in.readLine(); if(str!=null){ System.out.println(str); } } }catch(IOException e){ e.printStackTrace(); } } } class SendThread extends Thread{ Socket s; public SendThread(Socket s) { super(); this.s = s; } public void run(){ try{ PrintWriter out=new PrintWriter(s.getOutputStream()); while(true){ String str=SystemIn.readString(); if(str!=null){ if(str.equals((bye)) break; out.println(str); out.flush(); } } s.close(); }catch(IOException e){ e.printStackTrace(); } } } public class QQServer { static List l = new ArrayList(); public static void main(String[] args) throws Exception{ ServerSocket ss = new ServerSocket(9000); while(true){ Socket s = ss.accept(); l.add(s); Thread t = new QQServerThread(s); t.start(); } } } class QQServerThread extends Thread{ Socket s; BufferedReader in; PrintWriter out; public QQServerThread(Socket s) { this.s = s; try { InputStream is = s.getInputStream(); InputStreamReader isr = new InputStreamReader(is); in = new BufferedReader(isr); out = new PrintWriter(s.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } public void run() { try { while(true){ //a传haha String str = in.readLine(); //0:key:say //1:上线或者上线 if(str!=null){ for(int i = 0;i//b socket Socket socket = QQServer.l.get(i); //if(socket.equals(s)); //else{ //向b输出数据的流 PrintWriter pw = new PrintWriter(socket.getOutputStream()); pw.println(str); pw.flush(); //} } } } } catch (IOException e) { e.printStackTrace(); } } } 下一篇:Java之争谁最亏损 将此信息分享到: 。

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
用户名: 验证码: 点击我更换图片
栏目列表
推荐内容