# L-25 MCS 275 Wed 12 Mar 2008 : talk_host.py

# Illustration of sockets in a server using TCP,
# swapping information between two clients.
# The code for clients is in talk_client.py.

from socket import *

hostname = ''   # blank so any address can be used
number = 11267  # number for the port
buffer = 80     # size of the buffer

server_address = (hostname, number)
server = socket(AF_INET, SOCK_STREAM)
server.bind(server_address)
server.listen(2)

print 'talk host waits for first client'
first, first_address = server.accept()
print 'server accepted connection from ',\
   first_address
print 'talk host waits for second client'
second, second_address = server.accept()
print 'server accepted connection from ',\
   second_address

print 'talk host waits for first client'
alpha = first.recv(buffer)
print 'talk host received \"' + alpha + '\"'
print 'talk host waits for second client'
beta = second.recv(buffer)
print 'talk host received \"' + beta + '\"'

print 'talk host sends \"' + beta + '\"' \
  + ' to first client'
first.send(beta)
print 'talk host sends \"' + alpha + '\"' \
  + ' to second client'
second.send(alpha)

server.close()
