为服务端增加多线程解决方案

1、服务端代码如下:

***这个版本并没有真正的起到多线程的作用,主要原因在于t.join();以后的版本会改进这个问题***

#!/usr/bin/python
#!coding:utf-8 import os,sys,time
from socket import *
import threading def handleClient(conn):
print '[info] handleClient is :{0}'.format(os.getpid())
while True:
data = conn.recv(1024)
if not data : print '[info] handleClient client is stoped ..';break
print '[info] handleClient recive this --> {0}'.format(data.encode())
reply='[info] handleClient this is the information from server --> {0}'.format(data.decode())
conn.send(reply.encode())
conn.close()
sys.exit(0) if __name__ == "__main__":
hostIp='127.0.0.1'
port=2048
sock=socket(AF_INET,SOCK_STREAM)
sock.bind((hostIp,port))
sock.listen(5)
print '[info] parent pid is :{0} start listen {1}'.format(os.getpid(),(hostIp,port))
while True:
conn,addr=sock.accept()
print '[info] parent get a client {0}'.format(addr)
t=threading.Thread(target=handleClient,args=(conn,))
t.start()
t.join()

2、客户端代码如下:

#!/usr/bin/python
#!coding:utf-8 from socket import *
import os,sys if __name__ == "__main__":
#定义套接字
hostIp='127.0.0.1'
port=2048
sock=socket(AF_INET,SOCK_STREAM)
messages=['hello I am a client']
messages=messages+sys.argv[1:]
sock.connect((hostIp,port))
print '[info] 已经连接到server ' for message in messages:
sock.send(message.encode())
print sock.recv(1024).decode()
sock.close()

最新文章

  1. 4.在MVC中使用仓储模式进行增删查改
  2. 在类库中引用WebService的注意事件
  3. 【iCore3 双核心板】例程十:RTC实时时钟实验——显示日期和时间
  4. HDU 1829 A Bug's Life(种类并查集)
  5. PHP高效获取远程图片尺寸和大小(转)
  6. 一步一步学Remoting系列文章
  7. hdu2041java
  8. 经典的C++库【转帖】
  9. javascritpt 原型链
  10. jquery hide和show使用
  11. Java数据类型在实际开发中的应用一
  12. python3学习笔记11(函数)
  13. FPGA中分数分频器的实现代码
  14. 2.03-handler_openner
  15. (深度好文)重构CMDB,避免运维之耻
  16. Oracle总结一
  17. centos7 开机自动连网
  18. POI简易帮助文档系列--读取Excel文件
  19. October 07th 2017 Week 40th Saturday
  20. Head First Java & static

热门文章

  1. 基于DateTime Picker修改成类似旅游网站出发日期选择的功能
  2. javascript模式
  3. 隐私模式启动IE 谷歌浏览器
  4. rsyslog 同时发生nginx 访问日志和错误日志
  5. codevs1166 矩阵取数游戏
  6. 蓝桥杯之JAM的计数法
  7. [Leetcode][Python]51: N-Queens
  8. Qt widget--杭州小笼包
  9. they're hiring
  10. POJ 1811 Prime Test 素性测试 分解素因子