23 lines
		
	
	
		
			561 B
		
	
	
	
		
			Dart
		
	
	
	
			
		
		
	
	
			23 lines
		
	
	
		
			561 B
		
	
	
	
		
			Dart
		
	
	
	
| import 'dart:io';
 | |
| 
 | |
| class SocketTool {
 | |
|   connect() {
 | |
|     Socket.connect('127.0.0.1', 12345).then((socket) {
 | |
|       print(
 | |
|           'Connected to: ${socket.remoteAddress.address}:${socket.remotePort}');
 | |
| 
 | |
|       socket.write('GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n');
 | |
| 
 | |
|       socket.listen((data) {
 | |
|         print(String.fromCharCodes(data).trim());
 | |
|         print('消息');
 | |
|       }, onDone: () {
 | |
|         print('Connection closed');
 | |
|         socket.destroy();
 | |
|       });
 | |
|     }).catchError((error) {
 | |
|       print('Error: $error');
 | |
|     });
 | |
|   }
 | |
| }
 |