""" A simple TCP server/client pair. The application protocol is a simple format: For each file uploaded, the client first sends four (big-endian) bytes indicating the number of lines as an unsigned binary number. The client then sends each of the lines, terminated only by '\\n' (an ASCII LF byte). The server responds with "A" if it accepts the file, and "R" if it rejects it. Then the client can send the next file. """ __author__ = "Mark Sebern, Josiah Yoder, Phileus Fogg" # import the "socket" module -- not using "from socket import *" in order to selectively use items with "socket." prefix import socket import struct import time import sys # Port number definitions # (May have to be adjusted if they collide with ports in use by other programs/services.) TCP_PORT = 12100 # Host (Server) address when acting as "receiver" ("server"). # The address '' means accept any connection for our "receive" port from any network interface # on this system (including 'localhost' loopback connection). LISTEN_ON_INTERFACE = '' # Address of the "other" ("server") host that should be connected to for "send" operations. # When connecting on one system, use 'localhost' OTHER_HOST = 'localhost' # When "sending" to another system, use its IP address (or DNS name if there it has one) #OTHER_HOST = '155.92.x.x' def main(): # Get chosen operation from the user. action = raw_input('Select "(1-TS) tcpsend", or "(2-TR) tcpreceive":') # Execute the chosen operation. if action in ['1','TS','ts','tcpsend']: tcp_send(OTHER_HOST,TCP_PORT); elif action in ['2','TR','tr','tcpreceive']: tcp_receive(TCP_PORT); else: print "Unknown action: '{0}'".format(action) # Send multiple messages over a TCP connection to a designated host/port. # Receive a one-character response from the "server". # Print the received response. # Close the socket # Return def tcp_send(server_host,server_port): print "tcp_send: dst_host='{0}', dst_port={1}".format(server_host,server_port) tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcp_socket.connect((server_host,server_port)) num_lines = int(raw_input("Enter the number of lines you want to send (0 to exit):")) while num_lines != 0: # Here, I only pack one byte of the range, limiting the number of lines this # client can send. # # You will need to use a different approach to unpack to meet the specification. # (Most of your designs already do this correctly -- congratulations!) # # Feel free to upgrade this code to handle a higher number of lines, too. tcp_socket.sendall(chr(0)+chr(0)) time.sleep(1) # Just to mess with your servers. :-) tcp_socket.sendall(chr(0)+chr(num_lines)) # Enter the lines of the message. Each line will be sent as it is entered. for line_num in range(0,num_lines): line = raw_input('') tcp_socket.sendall(line+'\n') print 'Done sending. Awaiting reply.' response = tcp_socket.recv(1) if response == 'A': # Note: == in Python is like .equals in Java print 'File accepted' elif response == 'R': print 'File rejected' else: print 'Unknown response:',response num_lines = int(raw_input("Enter the number of lines you want to send (0 to exit):")) tcp_socket.close() # Listen for a TCP connection on a designated "listening" port # Accept the connection, creating a connection socket # Print the address and port of the sender # Repeat Forever: # Receive a message, saving it to a text-file (1.txt for first file, 2.txt for second file, etc.) # Send a single-character response 'A' to indicate that the upload was accepted. def tcp_receive(listen_port): print "tcp_receive (server): listen_port={0}".format(listen_port) # Replace this comment with your code. # Add more methods here (Delete this line) # Read the next byte from the socket data_socket. # The data_socket argument should be an open tcp data connection socket, not a tcp listening socket. # # Returns the next byte, as a string of one character. # # If the byte is not yet available, this method blocks (waits) # until the byte becomes available. # If there are no more bytes, this method blocks indefinitely. # def next_byte(data_socket): return data_socket.recv(1) # Invoke the main method to run the program. main()