cpp_redis::subscriber -> connect caused exception: connect() failure - windows

When I call "connect" to a Redis publisher, client gets exception "connect() failure" for some of my client/server combinations.
Other clients/servers run smoothly.
I am running Redis version 3.2.100 on windows (this is the newest windows Redis server available).
Using cpp_redis as client.
VisualStudio 2015 is my environment.
cpp_redis::redis_subscriber *subscriber = new cpp_redis::redis_subscriber();
std::string hostIP = 12.0.0.1; // Redis server IP address
int port = 6379;
std::string password = "my_password";
bool isConnected = false;
try
{
subscriber->connect(hostIP, port, nullptr);
if (password != "")
{
subscriber->auth(password,
[this](const cpp_redis::reply& reply)
{
if (!reply.is_error()
{
isConnected = true;
}
}
);
}
else
{
isConnected = true;
}
}
catch (const std::exception& exs)
{
std::cout << exs.what()) << std::endl;
}
works - and for other systems, I get exception on command
-- subscriber->connect(... etc. ?
the exception text is connect() failure
Any insights?
thank you!

mystery solved.
I had a bug in sending the Redis host ip to the clients which could not connect.
this was a really silly bug.

Related

is the extensive health check for connection essential in okhttp3?

In okhttp, it makes a 1 ms connection health check for every request. It has obvious influence in my application.
What's this check for?
Is it possible to add a config to disable the check?
if (doExtensiveChecks) {
try {
int readTimeout = socket.getSoTimeout();
try {
socket.setSoTimeout(1);
if (source.exhausted()) {
return false; // Stream is exhausted; socket is closed.
}
return true;
} finally {
socket.setSoTimeout(readTimeout);
}
} catch (SocketTimeoutException ignored) {
// Read timed out; socket is good.
} catch (IOException e) {
return false; // Couldn't read; socket is closed.
}
}
The most obvious fix would be to ensure you are using HTTP/2, which would skip this work on the socket and rely on pings etc.
FWIW extensive checks are based on the HTTP method, so should not be an issue with GET operations.
// We need the network to satisfy this request. Possibly for validating a conditional GET.
val doExtensiveHealthChecks = request.method != "GET"
val exchange = transmitter.newExchange(chain, doExtensiveHealthChecks)
Is this an option?

How to check internet connection?

I use ConnectivityPlugin (CrossConnectivity.Current.IsConnected field) to check the Internet connection. The problem is that it only checks if the internet connection button is pressed. That is, if I am connected to the mobile network, but there is no Internet itself (for example, problems from the operator), then the CrossConnectivity.Current.IsConnected field for such a situation returns true (although there is no connection). Question: how to check whether there is access to the Internet? Thanks
You can use the Device.StartTimer(TimeSpan, Func) method.
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
//insert checking of internet connection
}
Assuming "google.com" is always up and running:
if (CrossConnectivity.Current.IsConnected)
{
try {
Ping ping = new Ping();
String host = "google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();
PingReply reply = ping.Send(host, timeout, buffer, pingOptions);
if (reply.Status == IPStatus.Success){
// Your code here...
}
}
catch (Exception) {
return false;
}
}
I recommend to add the following code to your App.xaml.cs class OnStart method and that take care of it.
CrossConnectivity.Current.ConnectivityChanged += (sender, e) =>
{
if (!CrossConnectivity.Current.IsConnected)
{
Console.WriteLine("Internet connectivity lost");
}
};

Breaking on exception: String expected

When I run my code I get:
Breaking on exception: String expected
What I am trying to do is connect to my server using a websocket. However, it seems that no matter if my server is online or not the client still crashes.
My code:
import 'dart:html';
WebSocket serverConn;
int connectionAttempts;
TextAreaElement inputField = querySelector("#inputField");
String key;
void submitMessage(Event e) {
if (serverConn.readyState == WebSocket.OPEN) {
querySelector("#chatLog").text = inputField.value;
inputField.value = "";
}
}
void recreateConnection(Event e) {
connectionAttempts++;
if (connectionAttempts <= 5) {
inputField.value = "Connection failed, reconnecting. Attempt" + connectionAttempts.toString() + "out of 5";
serverConn = new WebSocket("ws://127.0.0.1:8887");
serverConn.onClose.listen(recreateConnection);
serverConn.onError.listen(recreateConnection);
} else {
inputField.value = "Connections ran out, please refresh site";
}
}
void connected(Event e) {
serverConn.sendString(key);
if (serverConn.readyState == WebSocket.OPEN) {
inputField.value = "CONNECTED!";
inputField.readOnly = false;
}
}
void main() {
serverConn = new WebSocket("ws://127.0.0.1:8887");
serverConn.onClose.listen(recreateConnection);
serverConn.onError.listen(recreateConnection);
serverConn.onOpen.listen(connected);
//querySelector("#inputField").onInput.listen(submitMessage);
querySelector("#sendInput").onClick.listen(submitMessage);
}
My Dart Editor says nothing about where the problem comes from nor does it give any warning until run-time.
You need to initialize int connectionAttempts; with a valid value;
connectionAttempts++; fails with an exception on null.
You also need an onMessage handler to receive messages.
serverConn.onMessage.listen((MessageEvent e) {
recreateConnection should register an onOpen handler as well.
After serverConn = new WebSocket the listener registered in main() will not work
If you register a listener where only one single event is expected you can use first instead of listen
serverConn.onOpen.first.then(connected);
According to #JAre s comment.
Try to use a hardcoded string
querySelector("#chatLog").text = 'someValue';
to ensure this is not the culprit.

Poco c++ Websocket server connection reset by peer

I am writing a kind of chat server app where a message received from one websocket client is sent out to all other websocket clients. To do this, I keep the connected clients in a list. When a client disconnects, I need to remove it from the list (so that future "sends" do not fail).
However, sometimes when a client disconnects, the server just gets an exception "connection reset by peer", and the code does not get chance to remove from the client list. Is there a way to guarantee a "nice" notification that the connection has been reset?
My code is:
void WsRequestHandler::handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp)
{
int n;
Poco::Timespan timeOut(5,0);
try
{
req.set("Connection","Upgrade"); // knock out any extra tokens firefox may send such as "keep-alive"
ws = new WebSocket(req, resp);
ws->setKeepAlive(false);
connectedSockets->push_back(this);
do
{
flags = 0;
if (!ws->poll(timeOut,Poco::Net::Socket::SELECT_READ || Poco::Net::Socket::SELECT_ERROR))
{
// cout << ".";
}
else
{
n = ws->receiveFrame(buffer, sizeof(buffer), flags);
if (n > 0)
{
if ((flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_BINARY)
{
// process and send out to all other clients
DoReceived(ws, buffer, n);
}
}
}
}
while ((flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
// client has closed, so remove from list
for (vector<WsRequestHandler *>::iterator it = connectedSockets->begin() ; it != connectedSockets->end(); ++it)
{
if (*it == this)
{
connectedSockets->erase(it);
logger->information("Connection closed %s", ws->peerAddress().toString());
break;
}
}
delete(ws);
ws = NULL;
}
catch (WebSocketException& exc)
{
//never gets called
}
}
See receiveFrame() documentation:
Returns the number of bytes received. A return value of 0 means that the peer has shut down or closed the connection.
So if receiveFrame() call returns zero, you can act acordingly.
I do not know if this is an answer to the question, but the implementation you have done does not deal with PING frames. This is currently (as of my POCO version: 1.7.5) not done automatically by the POCO framework. I put up a question about that recently. According to the RFC (6465), the ping and pong frames are used (among others) as a keep-alive function. This may therefore be critical to get right in order to get your connection stable over time. Much of this is guess-work from my side as I am experimenting with this now myself.
#Alex, you are a main developer of POCO I believe, a comment on my answer would be much appreciated.
I extended the catch, to do some exception handling for "Connection reset by peer".
catch (Poco::Net::WebSocketException& exc)
{
// Do something
}
catch (Poco::Exception& e)
{
// This is where the "Connection reset by peer" lands
}
A bit late to the party here... but I am using Poco and Websockets as well - and properly handling disconnects was tricky.
I ended up implementing a simple ping functionality myself where the client side sends an ACK message for every WS Frame it receives. A separate thread on the server side tries to read the ACK messages - and it will now detect when the client has disconnected by looking at flags | WebSocket::FRAME_OP_CLOSE.
//Serverside - POCO. Start thread for receiving ACK packages. Needed in order to detect when websocket is closed!
thread t0([&]()->void{
while((!KillFlag && ws!= nullptr && flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE && machineConnection != nullptr){
try{
if(ws == nullptr){
return;
}
if(ws->available() > 0){
int len = ws->receiveFrame(buffer, sizeof(buffer), flags);
}
else{
Util::Sleep(10);
}
}
catch(Poco::Exception &pex){
flags = flags | WebSocket::FRAME_OP_CLOSE;
return;
}
catch(...){
//log::info(string("Unknown exception in ACK Thread drained"));
return;
}
}
log::debug("OperatorWebHandler::HttpRequestHandler() Websocket Acking thread DONE");
});
on the client side I just send a dummy "ACK" message back to the server (JS) every time I receive a WS frame from the server (POCO).
websocket.onmessage = (evt) => {
_this.receivedData = JSON.parse(evt.data);
websocket.send("ACK");
};
It is not about disconnect handling, rather about the stability of the connection.
Had some issues with POCO Websocket server in StreamSocket mode and C# client. Sometimes the client sends Pong messages with zero length payload and disconnect occurs so I added Ping and Pong handling code.
int WebSocketImpl::receiveBytes(void* buffer, int length, int)
{
char mask[4];
bool useMask;
_frameFlags = 0;
for (;;) {
int payloadLength = receiveHeader(mask, useMask);
int frameOp = _frameFlags & WebSocket::FRAME_OP_BITMASK;
if (frameOp == WebSocket::FRAME_OP_PONG || frameOp ==
WebSocket::FRAME_OP_PING) {
std::vector<char> tmp(payloadLength);
if (payloadLength != 0) {
receivePayload(tmp.data(), payloadLength, mask, useMask);
}
if (frameOp == WebSocket::FRAME_OP_PING) {
sendBytes(tmp.data(), payloadLength, WebSocket::FRAME_OP_PONG);
}
continue;
}
if (payloadLength <= 0)
return payloadLength;
if (payloadLength > length)
throw WebSocketException(Poco::format("Insufficient buffer for
payload size %d", payloadLength),
WebSocket::WS_ERR_PAYLOAD_TOO_BIG);
return receivePayload(reinterpret_cast<char*>(buffer), payloadLength,
mask, useMask);
}
}

Multicast UDP packets to Multiple Interfaces (Multiple network adapters) in the system using boost asio

I want to multicast data to the all the network cards present in the system using boost asio(udp).
Can anyone help me in understanding how this can be done.I have created following example but its not working.The stream data send to each socket is getting mixed and the output is a mix of all sockets data on a single interface.
//e.g
Assume
data send to Interface1 is "Abcd"(Note: each interface has a separate socket)
data send to Interface2 is "xyz"
Then the output is only received from Interface1 ,the out stream is mixed(e.g "abxycdz" or "abxcdzy" etc)
Please help me in understanding the issue.
for(int i=0;i<NoOfInterfcaes;i++)
{
Open("229.1.1.1",1000,sNetInterfcaeAddList[i],false);
}
....................................
for(int i=0;i<NoOfInterfcaes;i++)
{
send(dataBuffer,len);
}
....................................
void Open(std::string &multicastIp,int nPort, std::string& sNetInterfcaeIpAdd,bool broadcast)
{
m_sNetInterfcaeIpAdd=sNetInterfcaeIpAdd;
m_sMulticastIp=multicastIp;
m_nport = nPort;
m_broadcast = broadcast ;
// try and open socket
const ip::udp::resolver::query queryIF( ip::udp::v4(),multicastIp.c_str(), nPort );
///resolve the connection
m_resolver.async_resolve(queryIF,
boost::bind(&handle_resolve, this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
}
void handle_resolve(const boost::system::error_code& err,
boost::asio::ip::udp::resolver::iterator endpoint_iterator)
{
if (!err)
{
//make a connection
m_socket.async_connect(*endpoint_iterator,
boost::bind(&handle_connect, this,
boost::asio::placeholders::error, endpoint_iterator));
}
else
{
//error message
}
}
void handle_connect(const boost::system::error_code& error,
boost::asio::ip::udp::resolver::iterator endpoint_iterator)
{
if (!error)
{
//Select the network adaptor
m_socket.set_option( boost::asio::ip::multicast::outbound_interface( boost::asio::ip::address_v4::from_string(m_sNetInterfcaeIpAdd)));
m_socket.set_option( boost::asio::ip::multicast::enable_loopback(false));
if(m_broadcast)
{
boost::asio::socket_base::broadcast option(true);
m_socket.set_option(option);
}
}
else if (endpoint_iterator != boost::asio::ip::udp::resolver::iterator())
{
// The connection failed. Try the next endpoint in the list.
m_socket.close();
//try to connect
m_socket.async_connect(*endpoint_iterator,
boost::bind(&handle_connect, this,
boost::asio::placeholders::error, endpoint_iterator));
}
else
{
//
}
}
You use this one socket per adapter, binding each to its adapter using the code from this question: Boost asio socket multicast to a specific ethernet interface

Resources