在进行压力测试的时候,我们的系统A 高并发的请求另外一个系统B,是tcp通讯方式。A系统采用Java,B系统用的C语言。在高并发的情况下,跑了一段压力测试,weblogic10被系统给kill掉了。没有堆栈异常现象,后来重现了发现有大量的端口time_wait。根据这样的判断应该是welogic在不断的申请系统资源,而这些端口有没有得到及时的释放。后来在通讯层的代码解决了。
现在对这部分还不是很了解。于是自己写了简单的例子看看这个问题。
- Java code
//读取流中字节的方法 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(); }
服务端的程序:服务端只是接收数据。不发送。
- Java code
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(); } } }
客户端代码:客户端只是发送数据
- Java code
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();// } }}