python搭建服务器实现两个Android客户端间收发消息

python搭建服务器实现两个Android客户端间收发消息,第1张

概述本文为大家分享了python搭建服务器实现两个Android客户端收发消息,供大家参考,具体内容如下

本文为大家分享了python搭建服务器实现两个AndroID客户端间收发消息,供大家参考,具体内容如下

python服务器

# Coding:utf-8import socketimport threadingimport timedef handle_clIEnt(clIEnt_socket,clIEnt_ID):  """处理客户端请求"""  # 获取客户端请求数据  while True:    try:      request_data = clIEnt_socket.recv(1024)    except Exception:      time.sleep(0.2)      continue    if len(request_data) > 0:      request_lines = request_data.splitlines()      print(request_lines[0].decode("utf-8"))      #res = int(request_lines[0]) + 1      clIEnt_socket_List[(clIEnt_ID+1) % 2].send(bytes(str(request_lines[0].decode("utf-8"))+"\n","utf-8"))  clIEnt_socket_List.remove(clIEnt_ID)if __name__ == "__main__":  server_socket = socket.socket(socket.AF_INET,socket.soCK_STREAM)  """  socket()是一个函数,创建一个套接字,  AF_INET 表示用IPV4地址族,  SOCK_STREAM 是说是要是用流式套接字  """  # server_socket.setsockopt(socket.soL_SOCKET,socket.so_REUSEADDR,1) # 设置地址重用  server_socket.bind(("10.2.70.42",8000)) # 绑定端口  server_socket.Listen(2) # 开启监听  clIEnt_socket_List = []  clIEnt_num = 0  Isready = False  while True:    clIEnt_ID = clIEnt_num    clIEnt_socket,clIEnt_address = server_socket.accept()    print("[%s,%s]用户连接上了" % clIEnt_address)    handle_clIEnt_thread = threading.Thread(target=handle_clIEnt,args=(clIEnt_socket,clIEnt_ID))    """    tartget表示这个进程到底要执行什么行为    args是target要接受的参数    """    clIEnt_socket_List.append(clIEnt_socket)    clIEnt_num += 1    if len(clIEnt_socket_List) == 3:      clIEnt_socket_List.pop(0)    clIEnt_socket.setblocking(0)    handle_clIEnt_thread.start()

AndroID客户端-Java代码

package com.example.administrator.wuziqi_intenet;import androID.os.Handler;import androID.os.Message;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androID.Widget.Toast;import java.io.BufferedReader;import java.io.BuffereDWriter;import java.io.IOException;import java.io.inputStreamReader;import java.io.OutputStreamWriter;import java.net.socket;import java.net.URLDecoder;import java.net.UnkNownHostException;import static com.example.administrator.wuziqi_intenet.R.ID.button1;public class MainActivity extends AppCompatActivity {  button button = null;  TextVIEw textVIEw = null;  EditText editText = null;  Socket socket;  BuffereDWriter pw=null;  BufferedReader is=null;  String string="baba";  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    new Thread() {      @OverrIDe      public voID run() {          try{            socket = new Socket("10.2.70.42",8000);            socket.setSoTimeout(10000);            pw = new BuffereDWriter(new OutputStreamWriter(socket.getoutputStream()));            is = new BufferedReader(new inputStreamReader(socket.getinputStream()));          } catch (IOException e) {            e.printstacktrace();          }        }      }.start();    button = (button) findVIEwByID(button1);    textVIEw = (TextVIEw) findVIEwByID(R.ID.textvIEw);    editText = (EditText) findVIEwByID(R.ID.input);    handler.sendEmptyMessageDelayed(1,100);    button.setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw vIEw) {        new Thread(){          @OverrIDe          public voID run()          {            String msg = editText.getText().toString();            try{              pw.write(msg);              pw.flush();            } catch (UnkNownHostException e) {              Toast.makeText(MainActivity.this,"失败1",Toast.LENGTH_SHORT).show();              e.printstacktrace();            } catch (IOException e) {              Toast.makeText(MainActivity.this,"失败2",Toast.LENGTH_SHORT).show();              e.printstacktrace();            }          }        }.start();        editText.setText("");      }    });  }  private Handler handler = new Handler() {    public voID handleMessage(Message message) {      try {        Moving();      } catch (IOException e) {        e.printstacktrace();      }    }  };  private voID Moving() throws IOException {    new Thread() {      @OverrIDe      public voID run() {        try {          if (is.ready())            string = is.readline();        } catch (IOException e) {          e.printstacktrace();        }      }    }.start();    byte[] b=string.getBytes();    String s1=new String(b);    System.out.println(s1);    textVIEw.setText(string);    handler.sendEmptyMessageDelayed(1,100);  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:通过Python 获取Android设备信息的轻量级框架基于Python的Android图形解锁程序详解python获取android设备的GPS信息脚本分享Python实现删除Android工程中的冗余字符串Python实现过滤单个Android程序日志脚本分享python服务器与android客户端socket通信实例使用python编写批量卸载手机中安装的android应用脚本使用python编写android截屏脚本双击运行即可python读取Android permission文件用Python脚本生成Android SALT扰码的方法 总结

以上是内存溢出为你收集整理的python搭建服务器实现两个Android客户端间收发消息全部内容,希望文章能够帮你解决python搭建服务器实现两个Android客户端间收发消息所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1204395.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存