0

My Task: I am trying to build a simple client-server messaging system in Python. I want to initialize a server on one machine (just Python socket server code that binds(), listens(), and connects() to a client), and start a client on another machine somewhere else in the world and have the server and client be able to send/receive message to each other.

My Local Setup: On my local machine, I am able to start the server code on IP address 127.0.0.1 and port 55555 and then have the client connect to it. I open one terminal for the server and one terminal for the client. They are then able to send and receive messages between each other. The server is initialized by asking the user to input an IP address and port number that the server machine is running on (and that the client will connect to). Here is just the initialization part of the code for the Server class:

import socket
import threading

class Server:
    def __init__(self, ip_addr, port):

        self.my_port = port             # an int representing the port number
        self.my_ip_addr = ip_addr       # a string representing the IP address

        #initialize socket
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        
        #initialize base server to my port and my IP address
        self.server.bind((self.my_ip_addr, self.my_port))
        
        #listen for connection from clients
        self.server.listen()
                
        #other code for accepting connection from client which I will leave out
        ...

The Problem: While this works perfectly as intended locally, this is not working when I try to replicate this on an external machine. I have created one AWS EC2 instance and tried to start the server on the public IPv4 address provided by AWS. Let's say its 12.345.678.90, and I choose the port is 55555.

I am receiving this error:

self.server.bind((self.my_ip_addr, self.my_port))
OSError: [Errno 99] Cannot assign requested address

What I have tried: When setting up the EC2 instance, I have disabled the firewalls and allowed all HTTP and HTTPS traffic. I have tried many different port numbers, the public IP address, private IP address, and even 127.0.0.1 on the EC2. They all yield the same result. I have also tried the solution from socket.error:[errno 99] cannot assign requested address and namespace in python, but while the socket does bind for the server, it is not allowing the client to connect to the server (the connection just times out and never connects).

I am not sure what is is needed to initialize the server on my EC2 instance to bind the socket, then begin accepting requests from the client which I ultimately want to be from another EC2 instance.

  • I would try using "0.0.0.0" as the IP in the bind. – CryptoFool Mar 05 '23 at 21:27
  • While this does allow the socket to bind successfully, I mentioned that I want the client (running on a different EC2 instance) to be able to connect to the already running server. If I have the server bind to "0.0.0.0" and then have the client connect to "0.0.0.0", it is not allowing me to connect. From my understanding the server's IP address should be unique to that machine, such that any client, anywhere in the world, can connect to it. – network_learner Mar 05 '23 at 21:33
  • The *server* uses `'0.0.0.0'` (or just `''`) which means "listen on all interfaces". The server could have multiple network cards, each with it's own IP address, for example. The client uses the *server's* IP address to connect to it. If the server has multiple IPs, it has to be the IP that has access to the network of the client. – Mark Tolonen Mar 05 '23 at 22:08
  • This did not work. I initiated the server code on the EC2 with 0.0.0.0 on port 55555 successfully. Then on a separate EC2, I ran the client code to connect to the public IP address of the EC2 that the server code is running on with port 55555. It was unable to connect (Timeout Error). – network_learner Mar 05 '23 at 22:17
  • Check your firewall – Mark Tolonen Mar 06 '23 at 13:46

1 Answers1

0

As mentioned by user Mark Tolonen, updating the firewall rules on the EC2 security groups to allow the intended port was necessary. This allowed client-server communication between the EC2s, by allowing them to use the desired port.