Sam Virgo Sam Virgo - 3 months ago 24
Dart Question

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

Answer

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.

Comments