本文共 4774 字,大约阅读时间需要 15 分钟。
2
)对象消息 发送对象消息。服务端有LinkServer和LinkServer2两个,分别支持单客户端与多客户端,方式也有些不一样。
附件解压工程LinkedSocketB,.test包内Test开头的即是测试类了。服务端接收到客户端连接时,直接一个循环发送n个对象过去,没什么大问题^^。
-
-
-
-
-
- public class LinkClient extends Thread {
-
-
- private SocketAddress address;
-
- private int timeout;
-
- private OnLinkClientListener listener;
-
-
-
-
-
-
-
-
- public LinkClient(String host, int port, int timeout) {
- this.address = new InetSocketAddress(host, port);
- this.timeout = timeout;
- }
-
- @Override
- public void run() {
- Socket socket = new Socket();
- try {
- socket.connect(address, timeout);
- if (null != listener) {
- listener.onConnected();
- }
- receiveObj(socket);
- } catch (ConnectException e) {
- if (null != listener) {
- listener.onConnectException();
- }
- } catch (SocketTimeoutException e) {
- if (null != listener) {
- listener.onTimeoutException();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != socket) {
- socket.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
-
- private void receiveObj(Socket socket) {
-
-
- ObjectInputStream is = null;
- try {
- is = new ObjectInputStream(new BufferedInputStream(
- socket.getInputStream()));
-
- while (true) {
- Object obj = is.readObject();
- if (null == obj) {
- break;
- }
- if (null != listener) {
- listener.onReceive(obj);
- }
- }
- if (null != listener) {
- listener.onExited();
- }
- } catch (SocketException e) {
- if (null != listener) {
- listener.onSocketException();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != is) {
- is.close();
- }
- if (null != socket) {
- socket.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
-
- public void setOnLinkClientListener(OnLinkClientListener listener) {
- this.listener = listener;
- }
-
- }
-
-
-
-
-
- public class LinkServer extends Thread {
-
-
- private int port;
-
- private ServerSocket mServerSocket;
-
- private ExecutorService pool;
-
- private OnLinkServerListener listener;
-
-
- private Object lockObj = new Object();
-
- private boolean isWaiting = false;
-
- private ArrayList<Object> sendObjList;
-
- public LinkServer(int port) {
- this.port = port;
- pool = Executors.newCachedThreadPool();
- sendObjList = new ArrayList<Object>();
- }
-
- @Override
- public void run() {
- try {
- mServerSocket = new ServerSocket(port);
- Socket client = null;
- client = mServerSocket.accept();
- if (null != listener) {
- listener.onClientConnected(client.getInetAddress());
- }
- pool.execute(new ThreadServer(client));
- } catch (BindException e) {
-
- if (null != listener) {
- listener.onBindException();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
-
- private class ThreadServer extends Thread {
-
- private Socket client;
-
- public ThreadServer(Socket client) {
- this.client = client;
- }
-
- @Override
- public void run() {
- ObjectOutputStream os = null;
- try {
- os = new ObjectOutputStream(client.getOutputStream());
-
- Object sendObj;
- synchronized (lockObj) {
- while (true) {
- if (sendObjList.size() <= 0) {
- isWaiting = true;
- lockObj.wait();
- }
- sendObj = sendObjList.get(0);
- os.writeObject(sendObj);
- os.flush();
-
-
- if (null == sendObj) {
- if (null != listener) {
- listener.onExited(client.getInetAddress());
- }
- break;
- }
- sendObjList.remove(0);
- }
- }
-
- } catch (SocketException e) {
- if (null != listener) {
- listener.onSocketException(client.getInetAddress());
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- try {
- if (null != os) {
- os.close();
- }
- if (null != client) {
- client.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
-
-
- public void sendObj(Object obj) {
-
- if (null != obj && !isSerializable(obj))
- throw new IllegalArgumentException(
- "Object needs to implement java.io.Serializable!");
-
- sendObjList.add(obj);
-
- if (isWaiting) {
- synchronized (lockObj) {
- lockObj.notifyAll();
- }
- isWaiting = false;
- }
- }
-
-
- private boolean isSerializable(Object obj) {
- Class<?>[] cls = obj.getClass().getInterfaces();
- for (Class<?> clazz : cls) {
- if (clazz.getName().equals(Serializable.class.getName()))
- return true;
- }
- return false;
- }
-
-
- public void setOnLinkServerListener(OnLinkServerListener listener) {
- this.listener = listener;
- }
-
- }
本文转自winorlose2000 51CTO博客,原文链接:http://blog.51cto.com/vaero/893844,如需转载请自行联系原作者