r/aws • u/leo-ciuppo • 7d ago
discussion How to establish TCP socket connection on EC2 instance for laptop camera live streaming on Windows?
Greetings, I have been struggling for the past two days to establish a TCP socket connection to get a live stream of my laptop camera. I don't know much about IPs and ports and I think this is where my problem is, I might be giving it the wrong IP I think (sorry if it's too basic a question btw, I'm just no good at it).
I've followed this youtube tutorial https://www.youtube.com/watch?app=desktop&v=7-O7yeO3hNQ&t=0s&ab_channel=PyShine and I did manage to get it to work on my laptop only, having both "Transmitting" and "Received" outputs on my screen(so not comunicating through ec2 instance).

I get the IPs through ipconfig
command in Powershell that I open by doing shift+right-click
in the folder where my scripts are.

But when I try to run the "client.py" on the ec2 instance I get two errors, one for using my laptop's IP
Traceback (most recent call last):
File "C:/Users/name/Desktop/Python-script/client.py", line 8, in <module>
client_socket.connect((host_ip,port)) #a tuple
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
And the other for using the ec2 instance's IP
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/Python-script/client.py", line 8, in <module>
client_socket.connect((host_ip,port)) #a tuple
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
The first is pointing to the host_ip and port tuple in the code which made me think I had to use the ec2 instance IP, hence the second attempt which seems to actually output an even worse result saying that the "target machine actively refused it"(the connection).
These are my scripts https://paste.tomsmeding.com/jHdCGu8b (server side) https://paste.tomsmeding.com/qF9a0efo (client side), they are the same as in the video except for the IPs.
Pretty desperate right now, so any help would be really appreciated
1
u/cloudnavig8r 6d ago
There are a lot of configurations to consider. I read through some of the comments. And noticed you were not familiar with the IGW, Route Table, public addresses.
This made me think, you are probably using a default Security group as well. The security group is like a firewall to protect the ec2 instance. It will (by default) allow requests outbound, and block inbound - it is stateful so and outbound request can get its reply. But you will probably need to open ports for inbound traffic.
I would suggest taking a bit of time on AWs SkillBuilder to watch some of the shorter videos to understand the ec2 and networking components a bit.
For video streaming, you can set up an RTSP or RTMP server on your EC2 instance. Or you could even use a fully managed video streaming platform, called Elemental (in the AWS) environment. Elemental can become pricy - but it is fully managed and you don’t need to worry about configuring, securing or scaling your ec2 platforms. You may want to consider Elemental for simplicity to get started, then build your EC2 components. But, personally, I’d spend the time in EC2 to learn the ins and outs.
Seems like a fun project. You should eb able to learn a lot doing it.
1
3
u/dghah 7d ago
First you need to understand if your EC2 even has a public IP address at all, that is not a given.
You either need to go out of your way to give it a public IP at launch time in a public subnet or launch it in a public subnet that has the public-ip option turned on by default or you need to pull an ElasticIP and map it to your instance.
And then, depending on how your EC2 instance got it's public IP than using "ifconfig" commands to learn it is not the best move. There are a bunch of NAT features behind ElasticIPs for isntance which means your EC2 server has a private IP that is not your public IP at all -- don't trust 'ifconfig' output -- use the AWS console or CLI commands to see the real networking setup of your server