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.