厚德博学 敬业乐群
@sky
全部博文(252)
2015年(2)
2014年(1)
2013年(1)
2012年(16)
2011年(42)
2010年(67)
2009年(87)
2008年(36)
25742040
shijiulo
niuxlinu
ebayboy
hayand66
大鬼不动
acer1025
醉鬼的故
小雅贝贝
XINGCHEN
wzy_yzw
十的9次
zds05
bjywxc
zlhc1
smile124
cynthia
格伯纳
分类:
2009-06-24 15:11:50
import java.io.*; import java.net.*; class Handler extends Thread { Socket socket; DataInputStream in; DataOutputStream out; Handler(Socket socket) { this.socket = socket; } public void run() { try { in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); } catch(Exception e) { e.printStackTrace(); } byte[] data = new byte[1024]; int dlen; while (true) { try { dlen = in.read(data, 0, 1024); if (dlen == -1) { break; } out.write(data, 0, dlen); } catch(Exception e) { System.out.println("exception: " + e.getMessage()); break; } } try { in.close(); out.close(); socket.close(); } catch(Exception e) { e.printStackTrace(); } } } public class server { public static void main(String args[]) throws Exception { ServerSocket serv = new ServerSocket(8888); while (true) { Socket socket = serv.accept(); Handler h = new Handler(socket); h.start(); } } } import java.io.*; import java.net.*; class Sender extends Thread { String msg; Socket socket; DataInputStream in; DataOutputStream out; Sender(String str) { msg = str; } public void run() { try { socket = new Socket("127.0.0.1", 8888); in = new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); DataOutputStream stdout = new DataOutputStream(System.out); byte[] data; data = msg.getBytes(); out.write(data, 0, data.length); byte[] buf = new byte[1024]; int dlen; while (true) { dlen = in.read(buf, 0, 1024); if (dlen == -1) { System.out.println("client in.read -1."); break; } out.write(buf, 0, dlen); stdout.write(buf, 0, dlen); } } catch(Exception e) { e.printStackTrace(); } } } public class client { public static void main(String args[]) { for (int i = 0; i < 100; i++) { Sender sender = new Sender(String.valueOf(i)); sender.start(); } } }
上一篇:java发送邮件
下一篇:java nio echo 服务
登录 注册