Last active
February 11, 2021 21:58
-
-
Save blisssan/e4cfbb971581b41929927f518d06aa4e to your computer and use it in GitHub Desktop.
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
import 'dart:io'; | |
Socket? socket; | |
void main() { | |
Socket.connect("localhost", 4567).then((Socket sock) { | |
socket = sock; | |
if (socket == null) { | |
throw Exception("Invalid Socket"); | |
} | |
socket?.listen(dataHandler, | |
onError: errorHandler, onDone: doneHandler, cancelOnError: false); | |
}).catchError((e) { | |
print("Unable to connect: $e"); | |
exit(1); | |
}); | |
//Connect standard in to the socket | |
stdin.listen((data) { | |
socket?.write(new String.fromCharCodes(data).trim() + '\n'); | |
}); | |
} | |
void dataHandler(data) { | |
print(new String.fromCharCodes(data).trim()); | |
} | |
void errorHandler(error, StackTrace trace) { | |
print(error); | |
} | |
void doneHandler() { | |
socket?.destroy(); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment