-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add a couple of simple programmes to help with testing where we…
… want to shove lots of data around
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
} |