日期:2014-05-20 浏览次数:20885 次
//读取流中字节的方法 private byte[] readMessage(InputStream is) throws IOException{ int bufferSize = 0; int bufferCapacity = maxbytes; byte[] b = new byte[bufferCapacity]; int numberRead = 0; do{ if((numberRead = is.read(b,bufferSize,bufferCapacity - bufferSize)) == -1){ break; } bufferSize += numberRead; if(bufferSize == bufferCapacity){ bufferCapacity = 2 * bufferCapacity; byte[] newBuffer = new byte[bufferCapacity]; System.arraycopy(b,0,newBuffer,0,bufferSize); b = newBuffer; } }while(true); //过滤后面的多余字节 while(b[bufferSize - 1] == 0x00){ bufferSize--; } byte[] res = new byte[bufferSize]; System.arraycopy(b,0,res,0,res.length); return res; } //写数据的方法 private void writeMessage(OutputStream os,byte[] buff) throws IOException{ os.write(buff); os.flush(); }
package com.haowei.socket.tcp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import com.haowei.JavaSocketUtil; public class Server { public static void main(String[] args) throws IOException { int count = 0; ServerSocket server = new ServerSocket(9999); System.out.println("......服务开启监听....."); while(true){ Socket socket = server.accept(); new Thread(new ServerThread(String.valueOf(count),socket)).start(); count ++; } } } class ServerThread implements Runnable{ private Socket socket; private String name; public ServerThread(String name,Socket socket) { super(); this.name = name; this.socket = socket; } @Override public void run() { System.out.println("线程 [" + name + "]开始执行...."); OutputStream os = null; InputStream is = null; byte[] data = null; try { os = socket.getOutputStream(); is = socket.getInputStream(); data = JavaSocketUtil.readMessage(socket); System.out.println("线程 [" + name + "]收到的数据:-->" + new String(data)); // String resp = "我是服务端返回的数据。"; // JavaSocketUtil.writeMessage(socket,resp.getBytes()); } catch (IOException e1) { e1.printStackTrace(); } try { if(socket!=null){ System.out.println("线程[" + name + "]关闭连接。"); socket.close(); } } catch (IOException e) { e.printStackTrace(); } } }
package com.haowei.socket.tcp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import com.haowei.JavaSocketUtil; public class Client { public static void main(String[] args) throws UnknownHostException, IOException{ int count = 0; while(count<1000){ Socket socket = new Socket("127.0.0.1",9999); new Thread(new ClientThread(String.valueOf(count),socket)).start(); count ++; } } } class ClientThread implements Runnable{ private Socket socket; private String name; public ClientThread(String name,Socket socket) { super(); this.socket = socket; this.name = name; } public void run(){ System.out.println("客户端线程 [" + name + "]开始执行...."); OutputStream os = null; InputStream is = null; byte[] data = null; try { os = socket.getOutputStream(); is = socket.getInputStream(); String resp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; JavaSocketUtil.writeMessage(socket,resp.getBytes()); // data = JavaSocketUtil.readMessage(socket); // System.out.println("读取的数据:-->" + new String(data)); } catch (Exception e1) { e1.printStackTrace(); } // try { // socket.close(); // } catch (IOException e) { // e.printStackTrace(); // } } }