python搭建服務(wù)器實(shí)現(xiàn)兩個(gè)Android客戶端間收發(fā)消息
本文為大家分享了python搭建服務(wù)器實(shí)現(xiàn)兩個(gè)Android客戶端間收發(fā)消息,供大家參考,具體內(nèi)容如下
python服務(wù)器
# coding:utf-8
import socket
import threading
import time
def handle_client(client_socket, client_id):
"""處理客戶端請(qǐng)求"""
# 獲取客戶端請(qǐng)求數(shù)據(jù)
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()是一個(gè)函數(shù),創(chuàng)建一個(gè)套接字,
AF_INET 表示用IPV4地址族,
SOCK_STREAM 是說是要是用流式套接字
"""
# server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 設(shè)置地址重用
server_socket.bind(("10.2.70.42", 8000)) # 綁定端口
server_socket.listen(2) # 開啟監(jiān)聽
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表示這個(gè)進(jìn)程到底要執(zhí)行什么行為
args是target要接受的參數(shù)
"""
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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- PythonPC客戶端自動(dòng)化實(shí)現(xiàn)原理(pywinauto)
- python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件
- python mqtt 客戶端的實(shí)現(xiàn)代碼實(shí)例
- python使用多線程編寫tcp客戶端程序
- 基于Python的ModbusTCP客戶端實(shí)現(xiàn)詳解
- python實(shí)現(xiàn)websocket的客戶端壓力測(cè)試
- python3+PyQt5 創(chuàng)建多線程網(wǎng)絡(luò)應(yīng)用-TCP客戶端和TCP服務(wù)器實(shí)例
- Python一個(gè)簡(jiǎn)單的通信程序(客戶端 服務(wù)器)
- python如何創(chuàng)建TCP服務(wù)端和客戶端
- Python 實(shí)現(xiàn)簡(jiǎn)單的客戶端認(rèn)證
相關(guān)文章
Python創(chuàng)建增量目錄的代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于Python創(chuàng)建增量目錄的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-11-11
pytorch如何定義新的自動(dòng)求導(dǎo)函數(shù)
這篇文章主要介紹了pytorch如何定義新的自動(dòng)求導(dǎo)函數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。2022-12-12
Python網(wǎng)絡(luò)編程之ZeroMQ知識(shí)總結(jié)
這篇文章主要介紹了Python網(wǎng)絡(luò)編程之ZeroMQ知識(shí)總結(jié),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Python?matplotlib?plotly繪制圖表詳解
plotly本身是個(gè)生態(tài)非常復(fù)雜的繪圖工具,它對(duì)很多編程語言提供接口。交互式和美觀易用應(yīng)該是?Plotly?最大的優(yōu)勢(shì),而?Matplotlib?的特點(diǎn)則是可定制化程度高,但語法也相對(duì)難學(xué),各有優(yōu)缺點(diǎn)。本文將通過示例詳細(xì)講解二者是如何繪制圖表的,需要的可以參考一下2022-03-03
Python自動(dòng)創(chuàng)建Excel并獲取內(nèi)容
這篇文章主要介紹了Python自動(dòng)創(chuàng)建Excel并獲取內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
淺談Python中的函數(shù)(def)及參數(shù)傳遞操作
這篇文章主要介紹了淺談Python中的函數(shù)(def)及參數(shù)傳遞操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

