Skip to content

Commit

Permalink
test: add a couple of simple programmes to help with testing where we…
Browse files Browse the repository at this point in the history
… want to shove lots of data around
  • Loading branch information
gkc committed Nov 14, 2024
1 parent cb6d209 commit 13c4568
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packages/dart/sshnoports/bin/demo_socket_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:io';

/// - simple client which talks to a demo_socket_server
/// - sends a request for N kBytes of data to be sent to us
/// - listens for the data and writes a message at the end
/// saying how much it received vs expected
void main(List<String> args) async {
// connect to socket on localhost, port args[0]
int port = int.parse(args[0]);
final socket = await Socket.connect('127.0.0.1', port);

// send a request for N kBytes of data to be sent to us
int numKbsToRequest = int.parse(args[1]);
socket.writeln('$numKbsToRequest');

int expected = numKbsToRequest * 1024;
int received = 0;
socket.listen(
(data) {
received += data.length;
},
onDone: () {
stdout.writeln('Received $received bytes ($expected expected)');
exit(0);
},
onError: (err) {
stdout.writeln('Error: $err');
stdout.writeln('Received $received bytes ($expected expected)');
exit(0);
},
);
}
36 changes: 36 additions & 0 deletions packages/dart/sshnoports/bin/demo_socket_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:io';

/// Simple server program which binds to port args[0] and then,
/// for each new socket connection
/// - listens for a request to send data (see demo_socket_client.dart)
/// - sends that much data
/// - closes the socket
///
/// Sample usage:
/// - dart demo_socket_server.dart 12345
/// - dart demo_socket_client.dart 12345
void main(List<String> args) async {

int port = int.parse(args[0]);
final server = await ServerSocket.bind(
InternetAddress.loopbackIPv4,
port,
);

List<int> kb = List.filled(1024, 65);

server.listen((socket) {
stdout.writeln('New socket connection');
socket.listen((bytes) async {
int numKbsToSend = int.parse(String.fromCharCodes(bytes));
stdout.writeln('Received request to send $numKbsToSend kBytes');
for (int i = 0; i < numKbsToSend; i++) {
socket.add(kb);
}
await socket.flush();
stdout.writeln('Wrote $numKbsToSend kBytes');
socket.destroy();
stdout.writeln('Socket closed.\n');
});
});
}

0 comments on commit 13c4568

Please sign in to comment.