Home > other >  Unable to connect Python Websocket server using Dart Client
Unable to connect Python Websocket server using Dart Client

Time:04-29

Issue :

When I am trying to connect to server.py using client.py then it is working and I am getting the response like this enter image description here

But, when I am trying to connect to Python server using Dart then getting following error: enter image description here

My server.py file content:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('192.168.0.102', 59000))
server.listen()
client, address = server.accept()
client.send('You are connected'.encode('utf-8'))

My client.py file content

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('192.168.0.102', 59000))
message = client.recv(1024).decode('utf-8')
print(message)

My dart client

import 'package:web_socket_channel/web_socket_channel.dart';

void main(List<String> arguments) {
  final channel = WebSocketChannel.connect(
    Uri.parse('ws://192.168.0.102:59000'),
  );
  channel.stream.listen(
    (data) {
      print(data);
    },
    one rror: (error) {
      print('Error : $error');
    },
  );
}

CodePudding user response:

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('192.168.0.102', 59000))
server.listen()
client, address = server.accept()
client.send('You are connected'.encode('utf-8'))

This is not implementation of WebSocket server, as it is not compliant with RFC 6455. If you want to use with WebSocket client, either alter your code that it is compliant with all normative parts of RFC 6455 xor use ready WebSocket server implementation provided by external package for example websocket-server but before using please comprehend its' documentation.

  • Related