I am using System.Net.WebSockets to create a websocket client to a websocket server I am running. The following code is used to create the websocket
Debug.Log("Setting up websocket");
client = new ClientWebSocket();
await client.ConnectAsync(new Uri("ws://192.168.68.93:8001/ws"), System.Threading.CancellationToken.None);
Debug.Log($"Socket status: {client.State}");
The following error is produced:
System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Net.Sockets.SocketException (0x80004005): 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.
The confusing part to me, the following code in javascript works perfectly fine
const ws = new WebSocket("ws://192.168.68.93:8001/ws")
And if I log the messages I receive, they are what I expect/work perfectly fine.
What would cause a JS websocket to work, but not a C# one?
My current suspicion is the issue is actually related to the wsserver, I am running it on an esp32, using a library called ESPAsyncWebServer. But I need to know why C# would fail on it when JS works fine
Related
I am getting the following exception in cluster.Connect while performing load testing in my application.
All hosts tried for query failed (tried :: SocketException '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'; :: SocketException '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'; ...)
The exception is thrown only when I overwhelm my application with a large number of requests (1000 or more). However, it seems to work fine for small number of requests because of which I am eliminating the possibility of a firewall issue. For some reason, this call gets stuck which leads to a deadlock in my application. Any help is greatly appreciated. Thanking in advance.
Regards,
Sitakanta Mishra
So I implemented an interface to communicate with a rest web service using the HttpClient class to make requests.
The code works perfectly locally, but when I deploy to Azure my application can't fire the request, it crashes on this line:
using (var response = await HttpClient.PostAsync(uri, content)) { ... }
// uri = https://api-rest.zenvia360.com.br/services/send-sms
The exact exception message is this:
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 200.203.125.26:443
The web service provider states that "if you use firewall or proxy you must add and exception for our IPs. For HTTPS, use port 443:
200.203.125.24
200.203.125.25
200.203.125.26 (the ip of the exception message)
200.203.125.27
200.203.125.28
200.203.125.29"
I looked everywhere in Azure looking for a firewall or something and I got nothing. Also this exception message is pretty cryptc. I tested the same code with another url (fired a post to www.gooogle.com) and it worked.
Any ideas?
The problem turned out to be on the web service side. The service I'm using blocks international requests by default. I asked them to whitelist the azure outbound IPs and now it works.
I have made written a c# program which is successfully connecting to the remote host with and without proxy. We work in two different networks, home and office networks, which uses proxy. Here is the code snippet.
while(true) {
Thread.sleep(5000);
using (var client = new WebClient()) {
client.Headers[HttpRequestHeader.Accept] = "application/json";
client.Headers[HttpRequestHeader.ContentType] = "application/json";
string result = client.UploadString(Event.GetInsertURL(), "POST", json);
if (result.Contains("SUCCESS")) {
// Console.WriteLine("SUCCESS");
}
}
}
The above code runs in a loop to keep making the request to the same api. It is working in both the networks if the program is started in those networks. However, if I start the program in home and hibernate or sleep the computer and restart it in office, I'm getting the following exception.
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 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 74.125.130.141:443
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
The reason is that the connection made for first time is reused in subsequent requests. Is there any way for me to force creation of connection when I get this exception?
P.S:
Code of Event.GetInsertURL()
public static string GetInsertURL(){
return "https://my-app.appspot.com/_ah/api/"eventendpoint/v1/insertEvents";
}
The code already creates a new client session for each connection in new WebClient().
It may be that the sleep occurs during the course of one of the sessions and then triggers the fault.
In general, any of the network methods could throw under unexpected conditions. The only real solution is to wrap the code in a try/catch block and retry under the correct conditions a few times before reporting a permanent failure.
Based on experience more than knowledge:
use HttpWebRequest instead of webClient , it's just more robust.
my examples are too messy but that's the base:
var httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(URI);
I wrote a simple tcp/ip chat and it worked with the localhost IP(127.0.0.1) as well as connecting to another computer on the local network.
Through the Internet (v4 IP address) it didn't work so I tried the codeproject example:
I downloaded but it threw the same exception on connecting:
TcpClient Client = New TcpClient(IPAddr, 65535);
I tried with another port and turned off Windows-Firewall and Avira-Free.
Update:
If I connect to another Internet IP address, the following exception is thrown:
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 IP-Address: 65535
Probably this won't help, but perhaps try another port?
I am trying to connect to gateway.sandbox.push.apple.com through "apns-sharp" class library, but all I get is the following error message: "A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connect
ed host has failed to respond 17.172.238.209:2195"
Why Is That?
Your provider may not allow to go out using port 2195.