4

iam new in websocket. I have created a console application in .net 4.5 framework and create a sample websocket client using library "WebSocketSharp". I have following code

using System;
using WebSocketSharp;


namespace WebsocketTest
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            { 
            using (var ws = new WebSocketSharp.WebSocket("ws://192.168.18.186:7884"))
            {
               Console.WriteLine("started");
                ws.Connect();
                ws.Send("START");
                Console.ReadLine();
                }
            }catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }

        }
    }
}

But i cant create a connection to my websocket server running on another machine. I got error message like this

09-05-2017 16:20:41|Fatal|WebSocket.connect:0|System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 192.168.18.186:7884
                             at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
                             at WebSocketSharp.WebSocket.setClientStream()
                             at WebSocketSharp.WebSocket.doHandshake()
                             at WebSocketSharp.WebSocket.connect()

What is the issue related with my code? is any good websocket libraries are available?

Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95

1 Answers1

1

The easiest way to find if your code is the problem would be to use any web-socket enabled browser to try to connect to the endpoint, and see what happens. If unsure, you could also just use telnet - since web-sockets starts out as a text protocol (http 1.1 headers) and you're not using SSL, you should be able to just open a connection and send some dummy headers. If telnet can't connect: your code won't be able to.

If telnet can connect, then it is possible that the client lib is problematic. There is actually a web-socket client built into .NET that you should be able to use (ClientWebSocket)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900