一、服务端

#!/usr/bin/python
# -*- coding: UTF-8 -*- import socket
import sys
from thread import * HOST = '127.0.0.1' # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created' #Bind socket to local host and port
try:
s.bind((HOST, PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit() print 'Socket bind complete' #Start listening on socket
s.listen(10)
print 'Socket now listening' #Function for handling connections. This will be used to create threads
def clientthread(conn):
#Sending message to connected client
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string #infinite loop so that function do not terminate and thread do not end.
while True: #Receiving from client
data = conn.recv(1024)
reply = '收到信息:' + data print reply reply = "my name is 徐芳波 "
if not data:
break conn.sendall(reply) #came out of loop
conn.close() #now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1]) #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,)) s.close()

二、客户端

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:client.py import socket # 导入 socket 模块
import time s = socket.socket() # 创建 socket 对象
host = '127.0.0.1' # 获取本地主机名
port = 8888 # 设置端口好 s.connect((host, port)) while True : s.send("my name is 开心 " )
data = s.recv(1024)
print '收到信息:'+data
time.sleep(1) s.close()

三、运行服务端

root@xushi:~/ai/python# python netsharp.pigeon.py
Socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:40114
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心
收到信息:my name is 开心

四、运行客户端

root@xushi:~/ai/python# python netsharp.pigeon.client.py
收到信息:Welcome to the server. Type something and hit enter 收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波
收到信息:my name is 徐芳波

最新文章

  1. mysql数据库导出模型到powerdesigner,PDM图形窗口中显示数据列的中文注释
  2. fir.im Weekly - 从零开始创建 Android 新项目
  3. wap端开发必须基础
  4. VS Bug 当获取其他项目的代码时, F5 无法进入调试模式. 也不报错....
  5. android复制数据库到SD卡(网上搜集,未经验证)
  6. SharePoint的安装配置
  7. MyEclipse使用自己的JDK和Tomcat
  8. Java笔记(三)……基础语法
  9. angular2 学习笔记 ( DI 依赖注入 )
  10. 自制单片机之五……LCD12864的驱动
  11. 图片拉伸(有保护区域) resizableImageWithCapInsets
  12. jQuery的hover()方法(笔记)
  13. mysql 添加[取消]timestamp的自动更新
  14. [Python爬虫]使用Selenium操作浏览器订购火车票
  15. C语言第二次作业-----顺序结构
  16. Java网络爬虫Hello world实现——Httpclient爬取百度首页
  17. FPGA笔试必会知识点1--数字电路基本知识
  18. 实用ExtJS教程100例-002:MessageBox的三种用法
  19. Android5.0新控件RecyclerVIew的介绍和兼容使用的方法
  20. Unity如何内置Visual Studio

热门文章

  1. 使用in ()进行批量删除
  2. Python Flask学习
  3. 并发编程(IO多路复用)
  4. 字符串 String 格式化 format
  5. Python词云(词频统计,掩膜显示)
  6. f5 SSL及证书
  7. c#调用dll接口传递utf-8字串方法
  8. 项目总结12:bootstrap-select下拉框模糊搜索
  9. 项目总结03:window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口
  10. Java使用点滴