i am making a flutter app,and i try to make a connection between a server socket and a client,but the client isnt able to connect to the socket,making this error: "SocketException (SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = 0.0.0.0, port = 44204)" any help would be deeply appreciated,thanks in advance.
here's the server code:
import 'dart:typed_data';
import 'dart:io';
Future<void> main() async {
int port = 10000;
final ip = InternetAddress.anyIPv4;
final server = await ServerSocket.bind(ip, port);
print("Server is running on :${ip.address}");
server.listen((Socket event) {});
void handleConnection(Socket client) {
client.listen((Uint8List data) {
final message = String.fromCharCodes(data);
print(data);
}, onError: (error) {
print(error);
client.close();
}, onDone: () {
print("Server:client left");
client.close();
});
}
}
here's the client code
import 'package:flutter/material.dart';
import 'package:awshare_schoolproject/Pages/login_page.dart';
import 'package:awshare_schoolproject/Pages/register_page.dart';
import 'dart:io';
import 'dart:typed_data';
Socket socket = socket;
Future<void> main() async {
runApp(const MyApp());
String address = "0.0.0.0";
int port = 10000;
socket = await Socket.connect(address, port);
print('Client: Connected to ${socket.remoteAddress.address}');
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: RegisterPage(socket),
);
}
}
tried to change the server and client address to my pc local ip address but it also wont work.
There is a specific process to do socket programming locally and using specific setup for the android emulator. However, I would suggest you to go through this post previously solved by the community. Also please be sure to search and find any existing solution rather posting your problem here in the first step, otherwise the system might prevent you posting any new questions. Well, this is how this website works so yeah, be careful, don't worry much and enjoy your coding. :)
Here's a possible solution posted previously in this website: SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend