Exemple #1
0
def udp_sender():
    target_address = SERVER_ADDRESS, SERVER_PORT
    string = f"Hello, this is a {PROTOCOL_STRING} test!"
    # string = f"Hello World!"
    data = string.encode(encoding='utf-8')
    print_protected(f"Test string to be sent: {string}")
    bytes_sent = CLIENT_SOCKET.sendto(data, target_address)
    print_protected(f"{AnsiColors.CYAN}Sender: Sent {bytes_sent} bytes to server")
Exemple #2
0
def tcp_server():
    listen_socket = socket(AF_INET, SOCK_STREAM)
    bind_address = SERVER_ADDRESS, SERVER_PORT
    listen_socket.bind(bind_address)
    listen_socket.listen()
    conn_socket, conn_addr = listen_socket.accept()
    listen_socket.close()
    while True:
        bytes_rcvd = conn_socket.recv(BUFFER_SIZES)
        if len(bytes_rcvd) > 0:
            print_protected(
                f"{AnsiColors.GREEN}Server: Received {len(bytes_rcvd)} bytes from {conn_addr[0]} "
                f"and port {conn_addr[1]}")
            bytes_sent = conn_socket.send(bytes_rcvd)
            print_protected(
                f"{AnsiColors.GREEN}Server: Sent back {bytes_sent} bytes to client"
            )
        elif len(bytes_rcvd) == 0:
            print_protected(
                f"{AnsiColors.GREEN}Server: Client closed connection..")
            break
        else:
            print_protected(
                f"{AnsiColors.GREEN}Server: Error receiving data..")
            break
    conn_socket.close()
Exemple #3
0
def tcp_client():
    conn_socket = socket(AF_INET, SOCK_STREAM)
    target_address = SERVER_ADDRESS, SERVER_PORT
    string = "Hello, this is a UDP test!"
    data = string.encode(encoding='utf-8')
    conn_socket.connect(target_address)
    bytes_sent = conn_socket.sendto(data, target_address)
    print_protected(
        f"{AnsiColors.CYAN}Client: Sent {bytes_sent} bytes to server")
    conn_socket.shutdown(SHUT_WR)
    bytes_rcvd = conn_socket.recv(BUFFER_SIZES)
    print_protected(
        f"{AnsiColors.CYAN}Client: Received back {len(bytes_rcvd)} bytes: {bytes_rcvd}"
    )
Exemple #4
0
def udp_client():
    global ERR_IDX
    server_socket = socket(AF_INET, SOCK_DGRAM)
    target_address = SERVER_ADDRESS, SERVER_PORT
    string = "Hello, this is a UDP test!"
    data = string.encode(encoding='utf-8')
    bytes_sent = server_socket.sendto(data, target_address)
    print_protected(
        f"{AnsiColors.CYAN}Client: Sent {bytes_sent} bytes to server")

    try:
        udp_listen_for_reply(server_socket=server_socket)
    except ConnectionResetError:
        # This can happen if the delay between server start and client start is not large enough
        print("Connection reset error, trying again..")
        server_socket.close()
        if ERR_IDX == 3:
            return 0
        else:
            ERR_IDX += 1
        # Try again
        udp_client()
    server_socket.close()
Exemple #5
0
def tcp_sender():
    target_address = SERVER_ADDRESS, SERVER_PORT
    string = f"Hello, this is a {PROTOCOL_STRING} test!"
    data = string.encode(encoding='utf-8')
    print_protected(f"Test string to be sent: {string}")
    CLIENT_SOCKET.connect(target_address)
    bytes_sent = CLIENT_SOCKET.sendto(data, target_address)
    print_protected(f"{AnsiColors.CYAN}Client: Sent {bytes_sent} bytes to server")
    CLIENT_SOCKET.shutdown(SHUT_WR)
    bytes_rcvd = CLIENT_SOCKET.recv(BUFFER_SIZE)
    print_protected(f"{AnsiColors.CYAN}Client: Received back {len(bytes_rcvd)} bytes: {bytes_rcvd}")
    pass
Exemple #6
0
def udp_server():
    server_socket = socket(AF_INET, SOCK_DGRAM)
    bind_address = SERVER_ADDRESS, SERVER_PORT
    server_socket.bind(bind_address)
    bytes_rcvd, sender_addr = server_socket.recvfrom(BUFFER_SIZES)
    print_protected(
        f"{AnsiColors.GREEN}Server: Received {len(bytes_rcvd)} from {sender_addr[0]} "
        f"and port {sender_addr[1]}")
    print_protected(f"{AnsiColors.GREEN}Server: String received: {bytes_rcvd}")
    bytes_sent = server_socket.sendto(bytes_rcvd, sender_addr)
    print_protected(
        f"{AnsiColors.GREEN}Server: Sent back {bytes_sent} bytes to client")
    server_socket.close()
Exemple #7
0
def udp_listener_thread(args: any):
    reply, from_addr = CLIENT_SOCKET.recvfrom(BUFFER_SIZE)
    print_protected(f"{AnsiColors.CYAN}Client: Received back {len(reply)} bytes: {reply}")
Exemple #8
0
def udp_listen_for_reply(server_socket: socket):
    reply, from_addr = server_socket.recvfrom(BUFFER_SIZES)
    print_protected(
        f"{AnsiColors.CYAN}Client: Received back {len(reply)} bytes: {str(reply)}"
    )