Reference source (actually I copied a little from the above):
Python's Socket Programming tutorial http://www.oschina.net/question/12_76126
Python Threading Guide Http://www.open-open.com/lib/view/open1345476194313.htmlPython socket document https://docs.python.org/3/ Library/socket.html#socket-objects
Specific ideas:
Each client has two threads, each responsible for receiving and sending, when not sent, in Raw_input () that stuck, when not receiving, in recv () that stuck
Server opens two threads for each client, processing both receive and send. Each thread that is sent is blocked in con.wait (), waiting for notify. Each received thread waits for input from the client at recv (), receives input, emits a notify, activates all output threads, and itself waits for the loop to be in the next recv ().
The advantage of doing this is that the CPU is idle during the wait, and the CPU is used only in the instant that it is received and sent.
There is no better way to think about ...
Attention:
I did a little rough, and after copying some comments and the code did not match, and no new comments added
Tags:< no > Code Snippets (4) [Full screen view all code]1. [File] client.py ~ 976B Download (283)?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 24 17:35:50 2013
@author: zbg
"""
import socket
import threading
inString
= ‘‘
outString
= ‘‘
nick
= ‘‘
def DealOut(s):
global nick, outString
while True
:
outString
= raw_input
()
outString
= nick
+ ‘: ‘ + outString
s.send(outString)
def DealIn(s):
global inString
while True
:
try
:
inString
= s.recv(
1024
)
if not inString:
break
if outString !
= inString:
print inString
except
:
break
nick
= raw_input
(
"input your nickname: "
)
ip
= raw_input
(
"input the server‘s ip adrress: "
)
sock
= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip,
8888
))
sock.send(nick)
thin
= threading.Thread(target
= DealIn, args
= (sock,))
thin.start()
thout
= threading.Thread(target
= DealOut, args
= (sock,))
thout.start()
#sock.close()
|
2. [File] server.py ~ 2KB Download (285)?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677 |
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 25 10:33:44 2013
@author: zbg
"""
import socket
import sys
import threading
con
= threading.Condition()
HOST
= raw_input
(
"input the server‘s ip adrress: "
)
# Symbolic name meaning all available interfaces
PORT
= 8888 # Arbitrary non-privileged port
data
= ‘‘
s
= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ‘Socket created‘
s.bind((HOST, PORT))
s.listen(
10
)
print ‘Socket now listening‘ #Function for handling connections. This will be used to create threads
def clientThreadIn(conn, nick):
global data
#infinite loop so that function do not terminate and thread do not end.
while True
:
#Receiving from client
try
:
temp
= conn.recv(
1024
)
if not temp:
conn.close()
return
NotifyAll(temp)
print data
except
:
NotifyAll(nick
+ " leaves the room!"
)
print data
return
#came out of loop
def NotifyAll(sss):
global data
if con.acquire():
data
= sss
con.notifyAll()
con.release()
def ClientThreadOut(conn, nick):
global data
while True
:
if con.acquire():
con.wait()
if data:
try
:
conn.send(data)
con.release()
except
:
con.release()
return
while 1
:
#wait to accept a connection - blocking call
conn, addr
= s.accept()
print ‘Connected with ‘ + addr[
0
]
+ ‘:‘ + str
(addr[
1
])
nick
= conn.recv(
1024
)
#send only takes string
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
NotifyAll(
‘Welcome ‘ + nick
+ ‘ to the room!‘
)
print data
print str
((threading.activeCount()
+ 1
)
/ 2
)
+ ‘ person(s)!‘
conn.send(data)
threading.Thread(target
= clientThreadIn , args
= (conn, nick)).start()
threading.Thread(target
= ClientThreadOut , args
= (conn, nick)).start()
s.close()
|
3. [Image] Qq20131027115134.png 4. Picture Qq20131027115232.png
Python socket multi-person chat room