zoukankan      html  css  js  c++  java
  • android中socket网络通信加入多线程

    服务器端:

     1 public class MyServer {
     2     public static ArrayList<Socket> socketList = new ArrayList<Socket>();
     3 
     4     /**
     5      * @param args
     6      */
     7     public static void main(String[] args) throws IOException {
     8         ServerSocket ss = new ServerSocket(30000);
     9         while(true){
    10             Socket s = ss.accept();
    11             socketList.add(s);
    12             new Thread(new ServerThread(s)).start();//接受到新的socket连接时启动新的子线程处理
    13         }
    14     }
    15 }
     1 public class ServerThread implements Runnable {
     2     Socket s = null;
     3     BufferedReader br = null;
     4 
     5     public ServerThread(Socket s) throws IOException {
     6         this.s = s;
     7         br = new BufferedReader(new InputStreamReader(s.getInputStream(),
     8                 "utf-8"));
     9     }
    10 
    11     public void run() {
    12         try {
    13             String content = null;
    14             while ((content = readFromClient()) != null) {
    15                 for (Iterator<Socket> it = MyServer.socketList.iterator(); it
    16                         .hasNext();) {
    17                     Socket s = it.next();
    18                     try {
    19                         OutputStream os = s.getOutputStream();
    20                         os.write((content + "
    ").getBytes("utf-8"));
    21                     } catch (SocketException e) {
    22                         e.printStackTrace();
    23                         it.remove();
    24                         System.out.println(MyServer.socketList);
    25                     }
    26                 }
    27             }
    28         } catch (IOException e) {
    29             e.printStackTrace();
    30         }
    31     }
    32 
    33     private String readFromClient() {
    34         try {
    35             return br.readLine();
    36         } catch (IOException e) {
    37             e.printStackTrace();
    38             // MyServer.socketList.remove(s);
    39             MyServer.socketList.remove(s);
    40         }
    41         return null;
    42     }
    43 
    44 }

    客户端:

     1 public class MainActivity extends Activity {
     2     EditText input;
     3     TextView show;
     4     Button send;
     5     Handler handler;
     6     ClientThread clientThread;
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         super.onCreate(savedInstanceState);
    11         setContentView(R.layout.activity_main);
    12         input = (EditText) findViewById(R.id.input);
    13         send = (Button) findViewById(R.id.send);
    14         show = (TextView) findViewById(R.id.show);
    15         handler = new Handler(){
    16             @Override
    17             public void handleMessage(Message msg) {
    18                 if(msg.what == 0x123){
    19                     show.append("
    " + msg.obj.toString());
    20                 }
    21             }
    22         };
    23         clientThread = new ClientThread(handler);
    24         new Thread(clientThread).start();
    25         send.setOnClickListener(new OnClickListener() {
    26             
    27             public void onClick(View v) {
    28                 try {
    29                     Message msg = new Message();
    30                     msg.what = 0x345;
    31                     msg.obj = input.getText().toString();
    32                     clientThread.revHandler.sendMessage(msg);
    33                     input.setText("");
    34                 } catch (Exception e) {
    35                     e.printStackTrace();
    36                 }
    37                 
    38             }
    39         });
    40     }
    41 }
     1 public class ClientThread implements Runnable {
     2     private Socket s;
     3     private Handler handler;
     4     public Handler revHandler;
     5     BufferedReader br = null;
     6     OutputStream os = null;
     7 
     8     public ClientThread(Handler handler) {
     9         this.handler = handler;
    10     }
    11 
    12     public void run() {
    13         try {
    14             s = new Socket("49.123.81.167", 30000);//参数1为主机ip
    15             br = new BufferedReader(new InputStreamReader(s.getInputStream()));
    16             os = s.getOutputStream();
    17             new Thread() {
    18                 public void run() {
    19                     String content = null;
    20                     try {
    21                         while ((content = br.readLine()) != null) {
    22                             Message msg = new Message();
    23                             msg.what = 0x123;
    24                             msg.obj = content;
    25                             handler.sendMessage(msg);
    26                         }
    27                     } catch (IOException e) {
    28                         e.printStackTrace();
    29                     }
    30                 };
    31             }.start();
    32             Looper.prepare();
    33             revHandler = new Handler() {
    34                 public void handleMessage(Message msg) {
    35                     if (msg.what == 0x345) {
    36                         try {
    37                             os.write((msg.obj.toString() + "
    ")
    38                                     .getBytes("utf-8"));
    39                         } catch (Exception e) {
    40                             e.printStackTrace();
    41                         }
    42                     }
    43                 };
    44             };
    45             Looper.loop();
    46         } catch (SocketTimeoutException e) {
    47             System.out.println("网络连接超时!");
    48         } catch (Exception e) {
    49             e.printStackTrace();
    50         }
    51     }
    52 }
  • 相关阅读:
    H5页面尺寸兼容rem
    Mysql索引、explain执行计划
    mysql物理结构
    mysql 架构
    excel 写
    好的开源项目
    批量插入大量数据
    文件下载回显
    shardingsphere 实现 springboot集成 多数据源
    前后端代码特殊符号乱码问题
  • 原文地址:https://www.cnblogs.com/baorantHome/p/6898326.html
Copyright © 2011-2022 走看看