P2P applications
Compared to client - server architecture, P2P has a self scalability, greater performance in peer N, minimum distribution time is leveling off. Direct cause of this self scalability are: peer addition to being a bit of consumers or their re-distributor.
There are two typical Internet applications is very suitable for P2P architecture, one is file distribution, the other is a large-scale peer community database; P2P architecture has a good self scalability;
BitTorrent is a popular P2P protocol for file distribution; BitTorrent set of terms, the participation of a particular document of all peers is called a torrent; peer in a torrent length is downloaded from each other file block; when a file is downloaded to the other blocks, the plurality of blocks is also transmitted to other peer; Once a peer access to the full document can be selfish away flood or continue to stay impartially other peer to send the file;
Socket Programming
UDP socket programming:
Function: Customer into his characters read from the keyboard, distributed server, the server will transfer it back to the client to uppercase
UDPClient.py
from socket import *
serverName = "127.0.0.1"
serverPort = 10021
clientSocket = socket (AF_INET, SOCK_DGRAM)
while True:
message = raw_input('Input lowercase sentence:')
if message=='quit':
break
clientSocket.sendto(message,(serverName,serverPort))
modifiedMessage,serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
AF_INET indicates the bottom of IPv4, SOCK_DGRAM expressed as a UDP socket
UDPServer.py
from socket import *
serverName = "127.0.0.1"
serverPort = 10021
serverSocket = socket (AF_INET, SOCK_DGRAM)
serverSocket.bind((serverName,serverPort))
print ("The server is ready to receive")
while True:
message,clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage,clientAddress)
TCP socket programming:
TCPClient.py
from socket import *
serverName = "127.0.0.1"
serverPort = 10021
clientSocket = socket (AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
while True:
message = raw_input('Input lowercase sentence:')
if message=='quit':
break
clientSocket.send(message)
modifiedMessage,serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
TCPServer.py
from socket import *
serverName = "127.0.0.1"
serverPort = 10021
serverSocket = socket (AF_INET, SOCK_STREAM)
serverSocket.bind((serverName,serverPort))
serverSocket.listen(1)
print ("The server is ready to receive")
while True:
connectionSocket,addr = serverSocket.accept()
message = connectionSocket.recv(2048)
modifiedMessage = message.upper()
connectionSocket.send(modifiedMessage)
connectionSocket.close()