Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # peer2peer.py
- # Note: The client and server should be run in separate terminal windows, so they can communicate with each other
- import socket
- import sys
- hosts = 8888, 8080, 7272
- serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- for host in hosts:
- try:
- serversocket.bind(('localhost', host))
- break
- except:
- 0
- serversocket.listen(5) # become a server socket, maximum 5 connections
- clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- clientsocket.connect(('localhost', host))
- c, addr = serversocket.accept()
- print("Connection Accepted From " + repr(addr[1])) + " -- localhost: " + str(host)
- try:
- # Send data
- message = 'Hello. This is the message that will be repeated in segments.'
- print >>sys.stderr, 'sending "%s"' % message
- print
- clientsocket.sendall(message)
- while message:
- data = c.recv(16)
- message = message.replace(data,'')
- print >>sys.stderr, 'received "%s"' % data
- finally:
- 0
- 0
- print
- clientsocket.send('hello world')
- while 1:
- print "The Following Message Has Been Sent --"
- print ">>> " + c.recv(1026)
- print
- data = str(raw_input('Type Anything And Click Enter: '))
- clientsocket.send(data)
- c.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement