I have an aws ec2 instance where I am trying to create a server to bind with my windows application the ser works but whenever I try to connect to the server from the client on my pc it gives the following error:
JavaScript
x
2
1
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
2
Server.py
JavaScript
1
44
44
1
import socket
2
import threading
3
4
HEADER = 64
5
PORT = 5050
6
SERVER = "0.0.0.0"
7
ADDR = (SERVER, PORT)
8
FORMAT = 'utf-8'
9
DISCONNECT_MESSAGE = "!DISCONNECT"
10
11
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12
server.bind(ADDR)
13
14
def handle_client(conn, addr):
15
print(f"[NEW CONNECTION] {addr} connected.")
16
17
connected = True
18
while connected:
19
msg_length = conn.recv(HEADER).decode(FORMAT)
20
if msg_length:
21
msg_length = int(msg_length)
22
msg = conn.recv(msg_length).decode(FORMAT)
23
if msg == DISCONNECT_MESSAGE:
24
connected = False
25
26
print(f"[{addr}] {msg}")
27
conn.send("Msg received".encode(FORMAT))
28
29
conn.close()
30
31
32
def start():
33
server.listen()
34
print(f"[LISTENING] Server is listening on {SERVER}")
35
while True:
36
conn, addr = server.accept()
37
thread = threading.Thread(target=handle_client, args=(conn, addr))
38
thread.start()
39
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
40
41
42
print("[STARTING] server is starting...")
43
start()
44
Client.py
JavaScript
1
23
23
1
import socket
2
3
HEADER = 64
4
PORT = 5050
5
FORMAT = 'utf-8'
6
DISCONNECT_MESSAGE = "!DISCONNECT"
7
SERVER = "Public IPv4 DNS"
8
ADDR = (SERVER, PORT)
9
10
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11
client.connect(ADDR)
12
13
def send(msg):
14
message = msg.encode(FORMAT)
15
msg_length = len(message)
16
send_length = str(msg_length).encode(FORMAT)
17
send_length += b' ' * (HEADER - len(send_length))
18
client.send(send_length)
19
client.send(message)
20
print(client.recv(2048).decode(FORMAT))
21
22
send("hi")
23
The client code is running on my windows pc and the Server code is running on my aws-ec2 instance which is a Linux Os.
I have a kaspersky firewall in my client windows but Will it effect connecting to a server?
Advertisement
Answer
Assuming your instance is public as I guess you can ssh into it, you need to add inbound rule to the instance’s security group which allows connections to 5050 from 0.0.0.0/0
or better, from your home/work IP address.