3

I am trying to figure out how to send data over UDP/TCP from my flutter application to my server which has .net applications which listen for UDP and TCP . I searched about it and I found that there is a package named web_socket_channel and I tried that it is working with the testing server ws://echo.websocket.org but when I replace the echo.websocket.org with my server IP address or domain name it doesn't work even i am not getting any errors back so I couldn't figure out what's going on. Is there something wrong? or am i doing something wrong? Can someone help me with my demo code :

  WebSocketChannel channel;
  String text = "";
  void sendSocket() {

      String message = "message_text"
       if(message.isNotEmpty)
      channel.sink.add(message);

   }  

  getStreamData() {
    channel.stream.asBroadcastStream().listen((event) {
      if (event != null)
          print(event);
    });
  }

  @override
  void dispose() {
    channel.sink.close();
    super.dispose();
  }

  @override
  void initState() {
    try {
      channel = IOWebSocketChannel.connect(

          'ws://127.0.0.1:8889');

      getStreamData();
      super.initState();
    } catch (e) {
      print(e.toString());
    }
  }


I appreciate your help. Thanks you so much.

Code Runner
  • 868
  • 16
  • 27
  • 1
    Yes, just use `dart:io` Socket or RawDatagramSocket. Since they are in the standard io package you can even just write a simple Dart command line app to test your implementation. – Richard Heap Mar 03 '20 at 03:09
  • There was a problem on my end. That's why it was not working. – Code Runner Mar 03 '20 at 14:57

1 Answers1

0

First, make sure that your server is running and available to connect.

For this problem go through all steps mentioned below:

1. Check your server is running and the app is listening to a specific port on which you are sending data
2. Check the server IP address and Port number
3. Make ping from the command prompt using "ping serverName/IP"
4. Make sure you can connect using telnet from command prompt "telnet serverName/IP portNumber"

(if telnet command doesn't work means your telnet client is not installed/enabled then check how to enable telnetClient)

1st Solution

if everything works well then try this plugin flutter_socket_plugin or you can write your own code for socket like @Richard Heap suggested.

2nd Solution

Excellent blog on Network Socket Programming for dart: Reference Link

You can also use RawDataGramSocket for the UDP sockets refer to this thread

Socket API for TCP sockets visit this thread

Code Runner
  • 868
  • 16
  • 27