Exemple #1
0
def run_connect() ->(connectfour_socket.SocketConnection,str):
    '''
    Connects to server running on the given host and listening
    on the given port, returning a SocketConnection object describing
    that connection if successful, or raising an exception if the attempt
    to connect fails.
    If there has exception, the program will ask user to enter again.
    '''
    
    while True:
        host = connectfour_socket.the_host()
        port = connectfour_socket.the_port()
        username = connectfour_socket.the_username()
        print('Connecting to '+host + ' port: '+str(port))
        try:
            Connection = connectfour_socket.connect(host,port)
            print('Connect success')
            break
        except ConnectionRefusedError:
            print ('Refusing connect')
        except TimeoutError:
            print('Connecting time out, Connect again')
        except socket.gaierror:
            print('Invaild host, Enter again')

    return Connection, username
def _connect_to_server() -> 'Connection':
    '''Show the process of connecting and handle user errors'''
    while True:
        host = _get_host()
        port = _get_port()
        print('Connecting to {} on port {} ...'.format(host, port))
        try:
            return connectfour_socket.connect(host, port)
        except:
            print('The host or port is wrong. Try again.')
Exemple #3
0
def _connect_server() -> connectfour_socket.ConnectfourConnection:
    while True:
        connectfour_host = _read_host()
        connectfour_port = _read_port()
        print('Connecting to {} (port {})...'.format(connectfour_host,
                                                     connectfour_port))
        try:
            connection = connectfour_socket.connect(connectfour_host,
                                                    connectfour_port)
            _show_welcome()
            return connection
        except:
            print('Failed to connect, please retry')
def prompt_connection() -> 'Connection':
    '''Prompts user to enter host and port number. Used at the start of the
       online version of the game. True and False indicates whether the
       connection is successful or not'''
    host = input('Please enter a hostname: ')
    host_name = host.strip()
    while True:
        try:
            port_number = int(input('Please enter a port number: '))
            break
        except:
            print('Please enter a valid port number')

    try:
        connection = connectfour_socket.connect(host_name, port_number)
        return connection
    except:
        print('...Unable to connect with the game server at this time.')
def connect_to_server() -> None:
    "Forms connections to server"
    while True:
        try:
            host = connect_host()
            port = connect_port()
            username = choose_username()
            connection = connectfour_socket.connect(host, port)
            connectfour_socket.hello(connection, username)
            start = connectfour_socket.start_ai_game(connection)
            if start == "READY":
                print('Okay! Starting game.')
                print()
                return connection
            elif start == "END":
                print('Goodbye.')
                sys.exit()
        except connectfour_socket.ConnectFourProtocolError:
            print("Invalid host or port. Please try again.")
        except OSError:
            print("Invalid host or port. Please try again.")
Exemple #6
0
def checkServerReady(connection: 'connection'):
    response = cfs.receive_response(connection)
    if (response.startswith('WINNER_')):
        ##            AI_Winner = response[6::]#Not Used
        cfs.close(connection)  ##Connection Cut: Game Over
        return 'GameOver'
    elif (response != 'READY'):
        print('Server Response: ' + response + '\nConnection Cut')
        cfs.close(connection)  ##Connection Cut: Bad Response
        return 'BadConnection'
    else:
        return 'Continue'


connection = cfs.connect(host, port)

#Connect to Server
response = cfs.send_receive(connection, 'I32CFSP_HELLO ' + username)

if (response != 'WELCOME ' + username):
    print('Response:' + response + '\nCut')
    cfs.close(connection)  ##CUT

#Reqeust AI Game
response = cfs.send_receive(connection, 'AI_GAME')
if (response != 'READY'):
    print('Response:' + response + '\nCut')
    cfs.close(connection)  ##CUT

currentBoard = cf.new_game()