Android Tcp服务器端

举报
福州司马懿 发表于 2021/11/19 05:16:12 2021/11/19
【摘要】 package com.chy.socket; import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import jav...

  
  1. package com.chy.socket;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import com.chy.global.GlobalValue;
  12. public class TcpServer {
  13. private ServerSocket serverSocket = null;
  14. private List<Socket> clientList = null;
  15. private OnReceiveListener onReceiveListener = null;
  16. private ExecutorService executorService = null;
  17. public interface OnReceiveListener {
  18. public void onReceive(Socket socket, OutputStream os, byte[] buffer, int len);
  19. }
  20. class ReadRunnable implements Runnable {
  21. private Socket socket = null;
  22. private InputStream is = null;
  23. private OutputStream os = null;
  24. public ReadRunnable(Socket socket) {
  25. this.socket = socket;
  26. try {
  27. is = socket.getInputStream();
  28. os = socket.getOutputStream();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. @Override
  34. public void run() {
  35. int len = 0;
  36. byte[] buffer = new byte[GlobalValue.READ_BLOCK_SIZE];
  37. while (true) {
  38. try {
  39. len = is.read(buffer, 0, GlobalValue.READ_BLOCK_SIZE);
  40. if (len > 0) {
  41. if (onReceiveListener != null) {
  42. onReceiveListener.onReceive(socket, os, buffer, len);
  43. }
  44. }
  45. } catch (IOException ex) {
  46. ex.printStackTrace();
  47. break;
  48. }
  49. }
  50. clientList.remove(socket);
  51. closeClient(socket);
  52. }
  53. }
  54. public TcpServer(int port, OnReceiveListener onReceivedListener) {
  55. try {
  56. serverSocket = new ServerSocket(port);
  57. } catch (IOException ex) {
  58. ex.printStackTrace();
  59. }
  60. if (serverSocket == null) {
  61. return;
  62. }
  63. this.onReceiveListener = onReceivedListener;
  64. clientList = new ArrayList<Socket>();
  65. executorService = Executors.newFixedThreadPool(GlobalValue.TCP_MAX_CONNECT_COUNT);
  66. // 监听线程
  67. executorService.execute(new Runnable() {
  68. @Override
  69. public void run() {
  70. Socket socket = null;
  71. while (true) {
  72. try {
  73. socket = serverSocket.accept();
  74. clientList.add(socket);
  75. executorService.execute(new ReadRunnable(socket));
  76. } catch (IOException ex) {
  77. ex.printStackTrace();
  78. break;
  79. }
  80. }
  81. closeClient(socket);
  82. }
  83. });
  84. }
  85. public synchronized boolean send(Socket socket, OutputStream os, byte[] buffer) {
  86. boolean result = false;
  87. if (os != null) {
  88. try {
  89. os.write(buffer);
  90. os.flush();
  91. result = true;
  92. } catch (IOException ex) {
  93. ex.printStackTrace();
  94. }
  95. }
  96. return result;
  97. }
  98. public void closeClient(Socket socket) {
  99. if (socket == null) {
  100. return;
  101. }
  102. try {
  103. InputStream is = socket.getInputStream();
  104. if (is != null) {
  105. is.close();
  106. }
  107. OutputStream os = socket.getOutputStream();
  108. if (os != null) {
  109. os.close();
  110. }
  111. socket.close();
  112. } catch (IOException ex) {
  113. ex.printStackTrace();
  114. }
  115. }
  116. public void close() {
  117. if (clientList != null) {
  118. for (Socket socket : clientList) {
  119. closeClient(socket);
  120. }
  121. }
  122. if (serverSocket != null) {
  123. try {
  124. serverSocket.close();
  125. } catch (IOException ex) {
  126. ex.printStackTrace();
  127. }
  128. }
  129. }
  130. }

文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/51560178

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。