Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart: What IP address should I use on aws with HttpServer.bind()?

I have been struggling all evening with deploying my first website on AWS and am failing at the very last hurdle. My server side code starts:

HttpServer server = await HttpServer.bind("51.45.28.101", 4040);

My client begins:

 WebSocket websocket = new WebSocket("ws://51.45.28.101:4040/ws");

51.45.28.101 is the public IP assigned to my AWS server. I am using a linux EC2 server. I start the server with the command:

dart main.dart

This produces the error:

error on initial connection SocketException: Failed to create server socket (OS Error: Cannot assign requested address, errno = 99), address = 51.45.28.101, port = 4040

I have read similar problems on stackoverflow where users were advised to set the IP to '0.0.0.0'. When I do this, I do not get this error. However, then I am unable to connect my client to the server. Please note I have setup the security groups correctly and have installed apache on the server and am able to load basic HTML files using the provided IP or DNS address. I only face problems when trying to run my main.dart server code.

Can somebody advise if I should be binding my server to the public IP?

Many thanks

like image 999
SSS Avatar asked Oct 25 '25 21:10

SSS


2 Answers

You should probably use:

HttpServer server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 4040);

This will bind to any IP, so you don't have to worry about figuring out what your IP address is on every EC2 server.

It's not possible to bind to the public IP address. If you wanted to bind to a specific IP address you'd have to use the private IP address.

like image 58
Mark B Avatar answered Oct 28 '25 18:10

Mark B


Have this working finally - this is actually very simple but for other unexperienced people reading this who may also be struggling:

1) as Mark kindly advised above do not try to bind to a public ip 2) my final problem was my "security groups" in amazon - for websockets it seems to work with a "custom tcp rule" and then set the port to whatever port you are binding to in your code.

If you are still having problems then try setting your rule to allow "all traffic" to check if it's a problem with your rule settings or something else.

like image 31
SSS Avatar answered Oct 28 '25 17:10

SSS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!