title: How to use RawSocket In Dart?
body: <p>I have server socket and when I access it with java setting the socket.setSoLinger(true,0); is works very well. I am trying to implement java client code with dart and I keep getting an error. Also it takes time to get the error. Dart suppose to be fast!</p>
  
  <p>In java I open the socket as <strong>socketOut = new DataOutPutStream(socket.getOutputStream()); and socketIn = new DataInputStream(socket.getInputStream());</strong> </p>
  
  <p>and than I can use <strong>socketOut.write(queryStr.getBytes());</strong> to write and I can use <strong>response += socketIn.readLine();</strong> to read.</p>
  
  <p>For my demo Dart Project I wrote below code and I keep getting the beloww error.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Unhandled exception:
  SocketException: OS Error: Broken pipe, errno = 32, address = ***.***.***.***, port = 48620
  #0      _rootHandleUncaughtError.&lt;anonymous closure&gt; (dart:async/zone.dart:1112:29)
  #1      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
  #2      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
  #3      _runPendingImmediateCallback (dart:isolate/runtime/libisolate_patch.dart:115:13)
  #4      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:172:5)
  </code></pre>
  
  <p><strong>Dart Code:</strong></p>
  
  <pre><code>import 'package:untitled/untitled.dart' as untitled;
  import 'dart:io';
  import 'dart:async';
  
  Socket socket;
  String _dataToBeSent = "00802020N0307809491508590203000000000000490012001629   01700CD0";
  var reply;
  
  // TODO: MAIN
  main(List&lt;String&gt; arguments) {
  
    _remoteServerConnect();
  
  }
  
  // TODO: REMOTE SERVER  CONNECT
  void _remoteServerConnect() async {
    try {
        var socket = await Socket.connect('***.***.***.***', *****);
        socket.writeln('$_dataToBeSent\n');
  
        await for (var data in socket) {
          reply = new String.fromCharCodes(data).trim();
          print("reply: $reply");
  
          socket.close();
          socket.destroy();
        } // await socket
  
        socket.handleError((data) {
          print("error: $data");
        });
  
    } on Error catch (e) {
      print(e.toString());
    } on Exception catch (f) {
      print(f.toString());
    }
  }
  </code></pre>
  
  <p>I am not sure. But something tells me that I should have to use RawSocket. And my quesiton is How to use RawSocket In Dart?</p>
  
  <p><strong>UPDATE-1:</strong> After reading a link I manage to recode my socket as shown below. But this time I am getting socket.write error.</p>
  
  <pre><code>// TODO: REMOTE SERVER  CONNECT
  void _remoteServerConnect()  {
    Socket.connect('***.***.***.***', *****).then((Socket sock) {
      socket = sock;
      socket.listen(dataHandler,
          onError: errorHandler,
          onDone: doneHandler,
          cancelOnError: false);
    }).catchError((AsyncError e) {
      print("Unable to connect: $e");
      exit(1);
    });
  
    print(_dataToBeSent);
    socket.write(_dataToBeSent);
  }
  
  void dataHandler(data){
    print(new String.fromCharCodes(data).trim());
  }
  
  void errorHandler(error, StackTrace trace){
    print(error);
  }
  
  void doneHandler(){
    socket.destroy();
    exit(0);
  }
  </code></pre>
  
  <p>I got an error in "socket.write(_dataToBeSent);" line saying that The method 'write' was called on null.</p>
  
  <pre><code>00802020N0307809491508590203000000000000490012001629   01700CD0
  Unhandled exception:
  NoSuchMethodError: The method 'write' was called on null.
  Receiver: null
  Tried calling: write(""00802020N0307809491508590203000000000000490012001629   01700CD0"")
  #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:48:5)
  #1      _remoteServerConnect (file:///home/capital/IdeaProjects/untitled/bin/main.dart:31:10)
  #2      main (file:///home/capital/IdeaProjects/untitled/bin/main.dart:13:3)
  #3      _startIsolate.&lt;anonymous closure&gt; (dart:isolate/runtime/libisolate_patch.dart:287:32)
  #4      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
  </code></pre>
  
  <p><strong>UPDATE-2:</strong> If I use Future than I cannot see any data but it runs without error.</p>
  
  <p>// TODO: REMOTE SERVER  CONNECT</p>
  
  <pre><code>Future _remoteServerConnect() async {
    await Socket.connect('***.***.***.***', ******).then((Socket sock) {
      socket = sock;
      socket.listen(dataHandler,
          onError: errorHandler,
          onDone: doneHandler,
          cancelOnError: false);
    }).catchError((AsyncError e) {
      print("Unable to connect: $e");
      exit(1);
    });
  
    print(_dataToBeSent);
    await socket.write(_dataToBeSent);
  }
  
  void dataHandler(data){
    print(new String.fromCharCodes(data).trim());
  }
  
  void errorHandler(error, StackTrace trace){
    print(error);
  }
  
  void doneHandler(){
    socket.destroy();
    exit(0);
  }
  </code></pre>
  
  <p><strong>The Result:</strong></p>
  
  <pre><code>00802020N02018092108262302030000000000005494490012001629   00000000
  Process finished with exit code 0
  </code></pre>