#!/usr/bin/python ############################################################################ # Server side: open a TCP/IP socket on a port, listen for a message from # a client, and send an echo reply; this is a simple one-shot listen/reply # conversation per client, but it goes into an infinite loop to listen for # more clients as long as this server script runs; the client may run on # a remote machine, or on same computer if it uses 'localhost' for server ############################################################################ from socket import * # get socket constructor and constants myHost = 'silo.cs.indiana.edu' # server machine, '' means local host myPort = 15854 # listen on a non-reserved port number sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object sockobj.bind((myHost, myPort)) # bind it to server port number sockobj.listen(3) # listen, allow 3 pending connects while True: # listen until process killed print "I am waiting now for someone to call..." connection, address = sockobj.accept( ) # wait for next client connect print 'Server connected by', address # connection is a new socket while True: data = connection.recv(1024) # read next line on client socket print "sending ", data if not data: break # send a reply line to the client connection.send('Echo=>' + data) # until eof when socket closed connection.close( )