How to handle socket disconnects in Dart? - sockets

I'm using Dart 1.8.5 on server.
I want to implement TCP Socket Server that listens to incoming connections, sends some data to every client and stops to generate data when client disconnects.
Here is the sample code
void main() {
ServerSocket.bind(
InternetAddress.ANY_IP_V4,
9000).then((ServerSocket server) {
runZoned(() {
server.listen(handleClient);
}, onError: (e) {
print('Server error: $e');
});
});
}
void handleClient(Socket client) {
client.done.then((_) {
print('Stop sending');
});
print('Send data');
}
This code accepts connections and prints "Send data". But it will never print "Stop sending" even if client was gone.
The question is: how to catch client disconnect in listener?

A Socket is bidirectional, i.e. it has an input stream and an output sink. The Future returned by done is called when the output sink is closed by calling Socket.close().
If you want to be notified when the input stream closes try using Socket.drain() instead.
See the example below. You can test it with telnet. When you connect to the server it will send the string "Send." every second. When you close telnet (ctrl-], and then type close). The server will print "Stop.".
import 'dart:io';
import 'dart:async';
void handleClient(Socket socket) {
// Send a string to the client every second.
var timer = new Timer.periodic(
new Duration(seconds: 1),
(_) => socket.writeln('Send.'));
// Wait for the client to disconnect, stop the timer, and close the
// output sink of the socket.
socket.drain().then((_) {
print('Stop.');
timer.cancel();
socket.close();
});
}
void main() {
ServerSocket.bind(
InternetAddress.ANY_IP_V4,
9000).then((ServerSocket server) {
runZoned(() {
server.listen(handleClient);
}, onError: (e) {
print('Server error: $e');
});
});
}

Related

Send web socket message to server directly after connection

The WebSocket server I use needs to receive a message after the connection.
The only way seems to use WebSocket.connect instead of WebSocketChannel.connect (https://github.com/dart-lang/web_socket_channel/issues/209):
import 'package:web_socket_channel/io.dart';
// ...
IOWebSocketChannel? _channel;
// ...
WebSocket.connect("ws://...").then((ws) {
this._channel = IOWebSocketChannel(ws);
_channel!.sink.add(msg);
}
But this is not supported by web. How is it possible to do that with both mobile and web support ?
WebSocket.connect(socketUrl).then((value) {
_channel = IOWebSocketChannel(value);
_channel?.stream.listen(onData,
onError: onError, onDone: onDone, cancelOnError: true);
debugPrint(
"webSocket——readyState:${_channel?.innerWebSocket?.readyState}");
if (_channel?.innerWebSocket?.readyState == 1) {
} else {}
}).timeout(const Duration(seconds: 10));

Flutter need client server Tcp

A server that will keep a permanent connection via Tcp. And close when the program is shut down.
An example of connecting a server that reconnects every time:
class Server {
void connectToServer() async {
try {
Socket _socket = await Socket.connect('127.0.0.1', 7890);
log('connected: ${_socket.address}:${_socket.port}');
} catch (e) {
log(e.toString());
}
}
void send(temp) async {
Socket _socket = await Socket.connect('127.0.0.1', 7890);
_socket.listen((List<int> event) {
log(utf8.decode(event));
});
_socket.add(utf8.encode(temp));
}
}

Client only listens to server when sends a message

I'm implementing socket.
Two clients connect to the server with no problem, when client1 sends a message to the server, the server publishes it to every other client (which in this case is client2). but client2 won't get the message unless it sends a message. It seems the listener of the client doesn't work. Obviously, I want client2 to get the message from client1 instantly.
here is my sever code:
import 'dart:io';
import 'dart:typed_data';
void main() async {
// bind the socket server to an address and port
MySocket mySocket = MySocket();
await mySocket.init();
}
class MySocket {
ServerSocket? server;
List<Socket> clients = [];
//initialize the socket
init() async {
server = await ServerSocket.bind("192.168.0.112", 4000);
// listen for client connections to the server
server!.listen((client) {
handleConnection(client);
addClient(client);
});
}
void handleConnection(Socket client) async {
print('Connection from'
' ${client.remoteAddress.address}:${client.remotePort}');
// listen for events from the client
client.listen(
// handle data from the client
(Uint8List data) async {
await Future.delayed(Duration(seconds: 1));
final message = String.fromCharCodes(data);
print(message);
publish(message, client);
},
// handle errors
onError: (error) {
print(error);
client.close();
},
// handle the client closing the connection
onDone: () {
print('Client left');
client.close();
},
);
}
void addClient(Socket client) {
//if client doesn't already exist add it to the list of clients
if (!clients.any((element) =>
'${client.remoteAddress.address}:${client.remotePort}' ==
'${element.remoteAddress.address}:${element.remotePort}')) {
clients.add(client);
}
}
void publish(String message, Socket client) {
//write the message to every client except the author of it
clients.forEach((element) async {
if ('${client.remoteAddress.address}:${client.remotePort}' !=
'${element.remoteAddress.address}:${element.remotePort}') {
element.write(message);
}
});
}
}
here is my client-side code:
import 'dart:io';
import 'dart:typed_data';
void main() async {
//gets the username
String name = '';
while (name.isEmpty) {
print('Enter your name: ');
name = stdin.readLineSync() ?? '';
}
// connect to the socket server
final socket = await Socket.connect("192.168.0.112", 4000);
print('Connected to: ${socket.remoteAddress.address}:${socket.remotePort}');
// listen for responses from the server
socket.listen(
// handle data from the server
(Uint8List data) {
final serverResponse = String.fromCharCodes(data);
print('$serverResponse');
},
// handle errors
onError: (error) {
print(error);
socket.destroy();
},
// handle server ending connection
onDone: () {
print('Left server.');
socket.destroy();
},
);
// sending messages to the server
String message = "";
while (message != "exit") {
message = stdin.readLineSync() ?? '';
await sendMessage(socket, name, message);
}
socket.close();
}
Future<void> sendMessage(Socket socket, String name, String message) async {
socket.write('$name: $message');
await Future.delayed(Duration(seconds: 2));
}
Thank you in advance.
The issue is because stdin.readLineSync() in your client blocks the current thread. You can get around this by spawning an isolate to handle that portion of the code, so that it does not block the socket.listen from printing out the responses from the server.
See updated client code below:
import 'dart:io';
import 'dart:isolate';
import 'dart:typed_data';
void main() async {
//gets the username
String name = '';
while (name.isEmpty) {
print('Enter your name: ');
name = stdin.readLineSync() ?? '';
}
// connect to the socket server
final socket = await Socket.connect("192.168.0.112", 4000);
print('Connected to: ${socket.remoteAddress.address}:${socket.remotePort}');
// listen for responses from the server
socket.listen(
// handle data from the server
(Uint8List data) {
final serverResponse = String.fromCharCodes(data);
print('$serverResponse');
},
// handle errors
onError: (dynamic error) {
print(error);
socket.destroy();
},
// handle server ending connection
onDone: () {
print('Left server.');
socket.destroy();
},
);
final receive = ReceivePort();
final isolate = await Isolate.spawn(readMessages, receive.sendPort);
await for (final message in receive) {
if (message == 'exit') break;
await sendMessage(socket, name, message as String);
}
socket.close();
}
void readMessages(SendPort port) {
String message = '';
while (message != 'exit') {
message = stdin.readLineSync() ?? '';
port.send(message);
}
Isolate.exit(port);
}
Future<void> sendMessage(Socket socket, String name, String message) async {
socket.write('$name: $message');
await Future<void>.delayed(Duration(seconds: 2));
}

How to send image over socket connection?

I have been trying to send image over socket connection from client to server which will be saved on the server.
The socket connection works fine for string messages but when I try to send an image, it is not transmitted correctly. Please give me a clue on what's the right way to do it.
Server-side code:
import 'dart:io';
import 'dart:typed_data';
void main() async {
Uint8List bytes= await File('1.jpg').readAsBytes();
final socket = await Socket.connect('localhost', 8000);
print('Connected to: ${socket.remoteAddress.address}:${socket.remotePort}');
// listen for responses from the server
socket.listen(
// handle data from the server
(Uint8List data) {
final serverResponse = String.fromCharCodes(data);
print('Server: $serverResponse');
},
// handle errors
onError: (error) {
print(error);
socket.destroy();
},
// handle server ending connection
onDone: () {
print('Server left.');
socket.destroy();
},
);
// send some messages to the server
await sendMessage(socket, bytes);
}
Future<void> sendMessage(Socket socket, Uint8List message) async {
print('Client: $message');
socket.write(message);
}
Client-side code:
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:image/image.dart';
void main() async {
// bind the socket server to an address and port
final server = await ServerSocket.bind('127.0.0.1', 8000);
// listen for clent connections to the server
server.listen((client) {
handleConnection(client);
});
}
void handleConnection(Socket client) {
print('Connection from'
' ${client.remoteAddress.address}:${client.remotePort}');
// listen for events from the client
client.listen(
// handle data from the client
(Uint8List data) async {
// final message = String.fromCharCodes(data);
print(data);
await File('new.jpg').writeAsBytes(data);
},
// handle errors
onError: (error) {
print(error);
client.close();
},
// handle the client closing the connection
onDone: () {
print('Client left');
client.close();
},
);
}
Faulty image on server-side:
Just change socket.write(message) to socket.add(message) at the client-side and you are good to go
Future<void> sendMessage(Socket socket, Uint8List message) async {
print('Client: $message');
socket.add(message);
}
because socket.write(object) Converts object to a String by invoking Object.toString.
have a nice day:)

Cannot catch SocketException

I am trying out Dart and I've been struggling with this for quite a bit now. Calling:
runServer() {
HttpServer.bind(InternetAddress.ANY_IP_V4, 8080)
.then((server) {
server.listen((HttpRequest request) {
request.response.write('Hello, World!');
request.response.close();
});
});
}
Once works like a charm. And then, trying
try {
runServer();
} on Error catch (e) {
print("error");
} on Exception catch(f) {
print("exception");
}
Now I'd expect that if I were to use this try-catch and start listening to the same port more than once, because I'm catching ALL exceptions and ALL errors, the program wouldn't crash. However, after running the code twice, instead of entering any try/catch clause I get:
Uncaut Error: SocketException: Failed to create server socket (OS Error: Only one usage of each socket address (protocol/network address/port) is normally permitted.
While I understand what the error is, I don't understand why doesn't it simply enter the catch Error/Exception clause?
Asynchronous errors can't be caught using try/catch (https://www.dartlang.org/docs/tutorials/futures/) at least unless you are using async/await (https://www.dartlang.org/articles/await-async/)
See also https://github.com/dart-lang/sdk/issues/24278
You can use the done future on the WebSocket object to get that error, e.g.:
import 'dart:async';
import 'dart:io';
main() async {
// Connect to a web socket.
WebSocket socket = await WebSocket.connect('ws://echo.websocket.org');
// Setup listening.
socket.listen((message) {
print('message: $message');
}, onError: (error) {
print('error: $error');
}, onDone: () {
print('socket closed.');
}, cancelOnError: true);
// Add message, and then an error.
socket.add('echo!');
socket.addError(new Exception('error!'));
// Wait for the socket to close.
try {
await socket.done;
print('WebSocket donw');
} catch (error) {
print('WebScoket done with error $error');
}
}