⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 JAVA NIO实现的 客户端连接池程序。 其功能主要是调用服务器函数
💻 JAVA
字号:
package org.client;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.InetSocketAddress;import java.net.SocketException;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;import org.bean.CallParam;import org.bean.ResultParam;import org.pool.ISocketChannelPool;import org.pool.SocketChannelPool;import org.pool._SocketChannel;public class Client {	private ISocketChannelPool socketChannelPool;	private ByteBuffer buffer = ByteBuffer.allocate(4096);	private _SocketChannel channel;				public Client(String host, int port, int minSize, int maxSize)			throws Exception {		socketChannelPool = SocketChannelPool.newInstance(host, port, minSize,				maxSize);	}				public Object call() throws IOException, ClassNotFoundException,			InterruptedException {		CallParam cp = new CallParam("functionTest", null);		channel = socketChannelPool.getConnection();		SendObject(cp);		ResultParam rp = (ResultParam) getObject();		channel.close();		return rp;	}	public Object getObject() throws IOException, ClassNotFoundException,			InterruptedException {		//channel.socketChannel.close(); 		long start = System.currentTimeMillis();		Object result = null;		if (channel.socketChannel.finishConnect()) {			int reads = 0;			do {				reads = channel.socketChannel.read(buffer);			} while (reads == 0);			buffer.flip();			byte[] tmp_buffer = buffer.array();			ByteArrayInputStream byteIn = new ByteArrayInputStream(tmp_buffer);			ObjectInputStream objIn = new ObjectInputStream(byteIn);			result = objIn.readObject();			objIn.close();		}		return result;	}	public void SendObject(Object object) throws IOException {		if (channel.socketChannel.finishConnect()) {			ByteArrayOutputStream baos = new ByteArrayOutputStream();			ObjectOutputStream oos = new ObjectOutputStream(baos);			oos.writeObject(object);			oos.close();			byte[] bs = baos.toByteArray();			ByteBuffer buffer = ByteBuffer.wrap(bs, 0, bs.length);			while (buffer.hasRemaining()					& channel.socketChannel.write(buffer) != -1) {				// Thread.currentThread().wait(1000);			}			buffer.flip();		}	}	public static class Ttest implements Runnable {		public Ttest() {			// TODO Auto-generated constructor stub		}		@Override		public void run() {			try {				Client client = new Client("127.0.0.1", 1234, 1, 1);				for (int i = 0; i < 5000; i++) {					ResultParam rp = (ResultParam) client.call();					System.out.println(Thread.currentThread().getName() + rp.getResult());				}			} catch (Exception e) {				e.printStackTrace();			}		}	}	public static void main(String args[]) {		try {			for (int i = 0; i < 10; i++) {				new Thread(new Ttest(), "Thread --- " + i).start();			}		} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -