Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Shows how to use Socket, ServerSocket and InputStream. | 6 * Shows how to use Socket, ServerSocket and InputStream. |
| 7 * | 7 * |
| 8 * (This deliberately sends data slowly, to show how InputStream can be | 8 * (This deliberately sends data slowly, to show how InputStream can be |
| 9 * read from asynchronously.) | 9 * read from asynchronously.) |
| 10 */ | 10 */ |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 } | 46 } |
| 47 | 47 |
| 48 // send next 4 bytes slowly | 48 // send next 4 bytes slowly |
| 49 new Timer((Timer t) { | 49 new Timer((Timer t) { |
| 50 sendByte(); | 50 sendByte(); |
| 51 if (bytesSent == bytesTotal) { | 51 if (bytesSent == bytesTotal) { |
| 52 sendSocket.close(); | 52 sendSocket.close(); |
| 53 t.cancel(); | 53 t.cancel(); |
| 54 print("finished sending"); | 54 print("finished sending"); |
| 55 } | 55 } |
| 56 }, 500, true ); | 56 }, 500, true); |
|
Søren Gjesse
2011/12/19 12:10:45
Maybe "repeating: true" here and in places where t
| |
| 57 } | 57 } |
| 58 | 58 |
| 59 void onConnect(Socket connection) { | 59 void onConnect(Socket connection) { |
| 60 receiveSocket = connection; | 60 receiveSocket = connection; |
| 61 inputStream = receiveSocket.inputStream; | 61 inputStream = receiveSocket.inputStream; |
| 62 inputStream.dataHandler = receiveBytes; | 62 inputStream.dataHandler = receiveBytes; |
| 63 } | 63 } |
| 64 | 64 |
| 65 void sendByte() { | 65 void sendByte() { |
| 66 sendSocket.writeList(const [65], 0, 1); | 66 sendSocket.writeList(const [65], 0, 1); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 78 print("received ${numBytes} bytes (${bytesReceived} bytes total)"); | 78 print("received ${numBytes} bytes (${bytesReceived} bytes total)"); |
| 79 if (bytesReceived >= bytesTotal) { | 79 if (bytesReceived >= bytesTotal) { |
| 80 receiveSocket.close(); | 80 receiveSocket.close(); |
| 81 serverSocket.close(); | 81 serverSocket.close(); |
| 82 print("done"); | 82 print("done"); |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 } | 85 } |
| 86 | 86 |
| 87 void main() => new SocketExample().go(); | 87 void main() => new SocketExample().go(); |
| OLD | NEW |