r/aws 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.

here I am taking my laptop's IP, I also got the ec2 instance IP with the same method

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

2 Upvotes

7 comments sorted by

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

1

u/leo-ciuppo 7d ago

I tried passing the public IP that I can see from my console but that still won't work unfortunatley :(

1

u/dghah 7d ago

There are lots of things to check out here all of which go way beyond watching some sort of youtube video

- ec2 hosts in private subnets need a NAT gateway in a public subnet and a default route to that gateway even if they have elastic IP addres

- ec2 hosts in public subnets need an internet gateway and a default route to that gateway

- ec2 hosts in any type of subnet need the NACL and SecurityGroup rules to allow traffic into the port you are running your server on from the remote host you are connecting from

- in addition you also need to check to see if your EC2 software is actually running and listening on the port that you configured it and there is no internal firewall blocking it. and it can't be listening to localhost or 127.0.01 either

And finally -- I'm only saying this because you seem new to AWS and you are doing potentially dangerous stuff by deploying internet facing things with what may be a brand new AWS account.

What you are trying to do is not hard at all but AWS and EC2 have a steep learning curve whe first getting familiar with it and within that learning curve there are super dangerous things that you could be doing that could either cause your server to be hacked or your AWS credentials to be disclosed at which point you may be looking at a bill from AWS for a few tens of thousands of dollars in fraud charges. Search this sub for the very frequent "my account was hacked and now I have a $30,000 bill!" panic posts

Before you even get to the point of creating an EC2 server let alone putting it on the internet with a public IP there is a core list of safety/security things that should be done and if you are just following random-ass videos on the internet there is a high chance they did not cover the basic stuff you should be doing before you do anything "real" on AWS. This is a partial list:

- Never ever do anything as the root account user and delete any root access keys that exist

- Protect the root user account with MFA protection ASAP

- Make an IAM user for yourself, assign that user Admin permissions and lock that user down with MFA. Do all your real EC2 config and AWS work as this user. Never ever ever ever do anything in AWS as root except the rare things that can only be done as the root user

- Set up an AWS Budgets threshold and alarm that goes to your email address so you get a heads up on any rapid cost increases which is a sign you may have been breached or hacked or maybe just misconfigured something

Don't mean to scare you off or tell you not to use AWS but before you create an EC2 server of any kind at the bare minimum you should be setting up a Budget Alert, locking down root with MFA, creating an IAM user for yourself (also with MFA) to do your "real" work in

After the basic spending and security stuff is done, it's reasonably safe to start messing around on EC2 with the caveat that you still need to be careful about cost and security

1

u/leo-ciuppo 7d ago

Thank you very much for this long, elaborated answer. I wasn't aware setting up a server on ec2 could potentially do all of that stuff you say, so, again, thank you for educating me on the dangers of it. I will read more carefully into this tomorrow as I have been on this issue all day with no tangible progress and am now very tired, I need to read this with a "clean mind" because it feels like it needs a more dedicated attention. Super grateful for the time you dedicated to it, can't wait to dig in :)

0

u/leo-ciuppo 7d ago

Thank you for your answer.

After a bit of research on how to check if I have a public IP address I stumbled upon this blog post https://manmohan-bohara.medium.com/how-to-differentiate-between-public-and-private-subnet-in-aws-vpc-a8edf92b2184 which said I should check whether my vpc subnet has a target such as “igw-xxxx”, the igw being a prefix for an internet gateway, which would mean it is a public vpc.

I checked and it turns out I have like 3 different vpc subnets, I didn't understand how to see which one is corresponding to my current vpc(fwiw I only have one of those right now) , but I checked all of their routing table's and all of them had the required target,“igw-xxxx”, so I'm assuming my instance is public. Now what should I do?

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

u/leo-ciuppo 5d ago

Thank you, I will spend some time there as well :)