how does h5 websocket work - c#

i have faced a problem about H5 websocket
this is my server code by c# , i have open a port 3030 to run socket
WebSocketServer server = new WebSocketServer("ws://127.0.0.1:3030");
then in my website,i connected to server
var ws = new WebSocket("ws://www.yummyonline.net:3030");
but,the error throwed out
WebSocket connection to 'ws://www.yummyonline.net:3030/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
while i define like this in my website
var ws = new WebSocket("ws://127.0.0.1:3030");
it will work.
could anyone can teach me why ?

You tell your server to listen only on 127.0.0.1, therefore it will not be accepting connections on any other address or interface.
Try using WS://0.0.0.0:3030 as the binding to listen on all interfaces and addresses.

Related

UDP UWP server + Mono2x client

I have a really weird issue with communication between server and client using UDP protocol. Client is written in Mono2x (I use Unity 3D as my client) and creates UdpClient class instance:
_udpClient = new UdpClient(9050);
_serverEP = new IPEndPoint(IPAddress.Parse(_serverIp), _serverPort);
My server is UWP application I want to run on Raspberry Pi that is using DatagramSocket:
_udpServer = new DatagramSocket();
_udpServer.MessageReceived += ClientCheck;
await _udpServer.BindServiceNameAsync(port.ToString());
I send data from client to server but with no luck. I checked with TCPView that data is send from my client application but never reaches the server. And now is the weird part. When I receive message from server first (I hardcode port to client), my client is able to send data with a success.
I am using same IPEndPoint to send data from client without any changes after receiving packet from server, it just starts working. Honestly, I have no idea what I could do wrong, so I will be thankful for any advice.
And now is the weird part. When I receive message from server first (I hardcode port to client), my client is able to send data with a success
This is a known issue which is filed based on this related question: https://stackoverflow.com/a/39767527/5254458
It includes the issue's description and temporary workaround.
The corresponding is team is investigating it and I can not guarantee that when the fix will be delivered.

How to access the websocket URL from C# which is in remote ip?

My Websocket URL is like this
"ws://someip:port/demo/"
i am trying to access the URL and it will return the Json. I am new to C#. I tried the below code
var client = new UdpClient();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("ipvalue"),portvalue );
client.Connect(ep);
var receivedData = client.Receive(ref ep);
Console.Write("receive data from " + ep.ToString());
Console.Read()
but it shows invalid Url. I have tried other codes also, but its also error. Please guide me.
Thanks
WebSocket != Socket
WebSocket is a protocol built on the top of TCP and requires HTTP for establishing the connection. Unfortunately you're not going to achieve anything with new UdpClient().
In order to connect to a websocket, you need to get a websocket library (alternatively, it looks like .NET already supports it) and use it instead of creating a TCP socket.

Tcp Server Only Allowing 1 Client

Server.cs - https://hastebin.com/enajinewij.cs
Client.cs - https://hastebin.com/iriperubur.cs
I have tried both running the Client on another PC and running it on one PC, but both result in the Client not being able to receive or send any messages.
I CANT portforward. I am using Hamachi for the IP Address. Both client and server are connected to my Network and are using the Hamachi IP Address. I am using PDA Net to connect to the internet from my PC.
The Server does not see them connect at all. Nor does the Server get any messages from them. Currently only the Server can send messages, and only it can get them.
I am not getting ANY errors at all, so I am not sure how I should handle solving this issue as it's my first time working with networking.
At first you create a TcpListener and you call StartLis() that does BeginAcceptTcpClient. However in AcceptTCPClient you create a new TcpListener and BeginAcceptTcpClient is not called.
You don't have to create a new listener for each connection, but you do have to call BeginAcceptTcpclient again:
private void AcceptTCPClient(IAsyncResult ar)
{
TcpListener Lis = (TcpListener)ar.AsyncState;
Clients.Add(new ServerClient(Lis.EndAcceptTcpClient(ar)));
StartLis(); // this will call BeginAcceptTcpClient again
}

No connection could be made because the target machine actively refused it [not similar to others]

I'm trying to learn Socket Programming, and I encountered this error while connecting to my server application.
Here's my declaration of the TcpListener in the server application:
TcpListener listener = new TcpListener(IPAddress.Loopback, 5152);
and here's my declaration of the TcpClient in my client application:
TcpClient client = new TcpClient(Dns.GetHostEntry(IPAddress.Loopback).HostName, 5152);
I have read several questions like this, and I always get the same answer: Either the server application isn't listening to the port or not running at all. But I've double-checked the Resource Monitor and cmd using netstat to see if the service is listening to the port, and it is. I've also included the service in the Firewall exceptions, so I'm not sure why I keep getting this error while trying to connect to the server app.
Any ideas?
Dns.GetHostEntry(IPAddress.Loopback).HostName returns the host name of your machine. When you pass a host name to TcpClient, it will resolve it to one or more IP addresses using Dns.GetHostAddresses(hostName). This includes the public and link-local IP addresses of your machine (e.g., 192.168.15.4), but not the loopback address (127.0.0.1).
So your client is trying to connect to any of the non-loopback addresses of your machine, while your server is listening only on the loopback address. Thus, no connection can be established.
Solution: Connect to the same end point your server is listening on.
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 5152);
TcpListener listener = new TcpListener(endPoint);
TcpClient client = new TcpClient();
client.Connect(endPoint);

Cannot get TcpClient to connect to gmail

I'm trying to make something that will read email, but I can't get anything to work. This code:
TcpClient c = new TcpClient();
c.Connect("imap.gmail.com", 993);
NetworkStream stream = c.GetStream();
stream.ReadTimeout = 1000;
stream.ReadByte();
Seams to be where any code I download breaks. The last line throws an IOException with the message: "Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."
I would settle for a third party program that automatically downloads email in a format I can read. I have gotten Thunderbird to connect to gmail so the problem is on my end for sure.
You are connecting on the SSL port. If you do not do a proper SSL-handshake, it will close the connection shortly after without sending you any data.
The message indicates that the remote server did not respond. It did not say "no connection allowed on this port". It did not say "I close the connection". It said nothing.
This means that you are not able to even create a connection on the TCP level. Try this:
telnet imap.gmail.com 993
It will fail. So it has nothing to do with you application.
use openssl to connect to gmail. its an easy way to get your things done.
Gmail IMAP uses SSL, so you need to wrap your network stream with SslStream and call AuthenticateAsClient method (so that it does the SSL handshake/server authentication magic). After that you interact with it as if it's normal (non-SSL) stream:
TcpClient c = new TcpClient();
c.Connect("imap.gmail.com", 993);
SslStream sslStream = new SslStream(c.GetStream());
sslStream.ReadTimeout = 1000;
sslStream.AuthenticateAsClient("imap.gmail.com");
sslStream.ReadByte();

Categories