eventstore socket receive error - eventstoredb

I am using the .Net EventStore and to append a basic event to a stream as follows:
IEventStoreConnection conn = EventStoreConnection.Create(
new IPEndPoint(IPAddress.Loopback, 1113));
conn.ConnectAsync().Wait();
int i = 100;
EventData e = new EventData(
Guid.NewGuid(),
"eventType",
true,
Encoding.ASCII.GetBytes("{\"somedata\" : " + i + "}"),
Encoding.ASCII.GetBytes("{\"metadata\" : " + i + "}")
);
conn.AppendToStreamAsync("test", ExpectedVersion.Any, e);
I am getting the following "socket receive error":
[16244,37,12:46:52.925] External TCP connection accepted: [Normal, 127.0.0.1:56476, L127.0.0.1:1113, {54565547-1dea-4ff5-acce-f2ebc0ff5d15}].
[16244,37,12:46:52.957] ES TcpConnection closed [12:46:52.957: N127.0.0.1:56476, L127.0.0.1:1113, {54565547-1dea-4ff5-acce-f2ebc0ff5d15}]:Received bytes: 0, Sent bytes: 0
[16244,37,12:46:52.957] ES TcpConnection closed [12:46:52.957: N127.0.0.1:56476, L127.0.0.1:1113, {54565547-1dea-4ff5-acce-f2ebc0ff5d15}]:Send calls: 0, callbacks: 0
[16244,37,12:46:52.957] ES TcpConnection closed [12:46:52.957: N127.0.0.1:56476, L127.0.0.1:1113, {54565547-1dea-4ff5-acce-f2ebc0ff5d15}]:Receive calls: 1, callbacks: 1
[16244,37,12:46:52.957] ES TcpConnection closed [12:46:52.957: N127.0.0.1:56476, L127.0.0.1:1113, {54565547-1dea-4ff5-acce-f2ebc0ff5d15}]:Close reason: [ConnectionReset] Socket receive error
[16244,37,12:46:52.957] Connection 'external-normal' [127.0.0.1:56476, {54565547-1dea-4ff5-acce-f2ebc0ff5d15}] closed: ConnectionReset.
Any idea why? THANKS :-)
What this means is the event written to the stream is not pushed to subscribing clients...

Related

Stream Read Error: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

I am working on a windows service to communicate with servers, In which I am using the TcpListener class.
In TcpListener class, after 3 successful socket connections, the 4th connection is returning an error.
class MyTcpListener
{
public static void Main()
{
TcpListener server=null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
// Enter the listening loop.
while(true)
{
Console.Write("Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also use server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
// Shutdown and end connection
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}
On this line on the 4th connection, it's returning an error.
// Send back a response.
stream.Write(msg, 0, msg.Length);
Error:
Stream Write Error: Unable to write data to the transport connection:
An existing connection was forcibly closed by the remote host. Stream
Write Error: at System.Net.Sockets.NetworkStream.Write(Byte[]
buffer, Int32 offset, Int32 size)

Exception decoder after connected with PubSubClient in my ESP8266

My code always gets exception decoder message and resets a few ms after MQTT connection is done successfully.
My connectMqtt function:
void MyClass::connectMqtt(PubSubClient client)
{
// Loop until we're reconnected
int attempts = 0;
bool connected = false;
while (!connected && attempts < 5)
{
std::string clientId = _my_device.getBarcode();
// Attempt to connect
if (client.connect(clientId.c_str(), _my_device.getMqttUser(), _my_device.getMqttPassword()))
{
Serial.println(F("MQTT connected"));
connected = true;
//Subscribe to config topic of the device
//TODO not done yet
}
else
{
Serial.print(F("failed, rc="));
Serial.print(client.state());
Serial.println(F(" try again in 5 seconds"));
attempts++;
// Wait 5 seconds before retrying
delay(5000);
}
}
Serial.println(F("Done."));
}
Exception decoder mesage starts by:
Exception (3):
epc1=0x40100691 epc2=0x00000000 epc3=0x00000000 excvaddr=0x40032468 depc=0x00000000
As #Juraj said:
The solution is passing a reference to the function, not a copy
void MyClass::connectMqtt(PubSubClient& client)

Qt connect function between slot and signal does not work into thread run function

I wroute a server program and a client program by qt.
client program create connection to local host port 60600 by tcp protocol and server program listen to that port.
after creating new connection by client, server accept connection and send packets to client.
In normal state, I create TCPClient class object and TCPServer class object in main function of client and server programs and it work properly.
but i need to create objects into thread and create and start thread in main function.
when i move code of creating client or server objects to run() function of Qthread it runs but server and clients does not work properly.
connect() function in constructor of server and client classes return true but connection does not work and slot function does not call.
can any help me?
server code is here:
main.cpp:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
......
PgThread* MyThread = new PgThread( d );
MyThread->start();
......
return app.exec();
}
PgThread.cpp
PgThread::PgThread( int packetsizein )
{
packetsize = packetsizein;
//connect( this, SIGNAL(finished()), this, SLOT(finishedReq() ));
}
PgThread::~PgThread
()
{
delete server;
}
void PgThread::run()
{
server = new tcpserver(packetsize);
}
tcpserver.cpp:
tcpserver::tcpserver(int packsize)
{
packetSize = packsize;
server = new QTcpServer(this);
server -> listen( QHostAddress::Any,60600 );
connect( server,SIGNAL(newConnection()),this,SLOT(newConnectionRequest()) );
}
void tcpserver::newConnectionRequest()//does not call when tcpserver object create in run comnmand of pgthread
{
i=i+1;
QTcpSocket *clientConnection = server->nextPendingConnection();
connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
QByteArray block;
char c;
c=(char)i;
block.append(c);
for (int k=1;k<packetSize;k++)
block.append('A');
clientConnection->write(block);
clientConnection->flush();
qint64 current = QDateTime::currentMSecsSinceEpoch();
if(forDebug)
cout << "start time: " <<current<<endl;
else
cout << i << "\n" << current << endl;
clientConnection->disconnectFromHost();
}
I found Problem:
I must add exec() function in end of run function!

WinSock UDP socket creation order in loopback.

I have a network application meant for a private LAN. I am doing my testing using loopback. When I test on the LAN the socket creation order does not matter. If I test using loop back 127.0.0.1 then there is a socket creation ordering issue. Why is it different on loop back?
Here are more details...
There is one server, and many client instances. The server is broad casting data over UDP. The clients receive the data and process it.
I need to have the network layer not care about the order in which either the server or clients start. It is hard to administer process creation for my case. The application instances should be able to start on the network in any order and just see the data broadcasted on the UDP port when it is sent.
But there is something in the way I setting up my UDP sockets which is forcing ordering to take place. I must start the clients, THEN start the server. If I start the clients AFTER the server doing the UDP broadcast, the client sockets do not receive the data. If I force a running server instance to tear down and rebuild its UDP socket, suddenly all the clients start receiving data.
There must be something wrong with how I creating the socket. The client and server code use a shared function library to make the UDP socket. So the server is sending on m_fdOut. Each instance of the client is receiving on m_fdIn.
What am I doing wrong here?
SOCKET m_fdIn;
SOCKET m_fdOut;
if ((m_fdIn = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
WARNF("socket failed, winsock error %d\n", WSAGetLastError());
exit(1);
}
if ((m_fdOut = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
WARNF("socket failed, winsock error %d\n", WSAGetLastError());
exit(1);
}
int sockopt = 1;
if (setsockopt(m_fdOut, SOL_SOCKET, SO_BROADCAST, (char *)&sockopt,
sizeof(sockopt)) < 0)
{
WARNF("setsockopt failed, winsock error %d\n", WSAGetLastError());
exit(1);
}
sockopt = readPreference<int>("SOL_RCVBUF", 512*1024);
if (setsockopt(m_fdIn, SOL_SOCKET, SO_RCVBUF, (char *)&sockopt, sizeof(sockopt)) < 0)
{
WARNF("setsockopt failed, winsock error %d\n", WSAGetLastError());
exit(1);
}
sockopt = 1;
if (setsockopt(m_fdIn, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt)) < 0)
{
WARNF("setsockopt failed, winsock error %d\n", WSAGetLastError());
exit(1);
}
sockopt = readPreference<int>("IP_MULTICAST_TTL", 32);
if (setsockopt(m_fdOut, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&sockopt, sizeof(sockopt)) < 0)
{
WARNF("setsockopt failed, winsock error %d\n", WSAGetLastError());
exit(1);
}
String destAdd = "255.255.255.255"
int portNumber = 1234;
int n1, n2, n3 ,n4;
if (sscanf(destAddr, "%d.%d.%d.%d", &n1, &n2, &n3, &n4) != 4)
{
n1 = n2 = n3 = n4 = 255;
}
u_long bcastAddr = (n1<<24) | (n2<<16) | (n3<<8) | n4;
outAddr.sin_family = AF_INET;
outAddr.sin_port = htons(portNumber);
outAddr.sin_addr.s_addr = htonl(bcastAddr);
struct sockaddr_in in_name;
in_name.sin_family = AF_INET;
in_name.sin_addr.s_addr = INADDR_ANY;
in_name.sin_port = htons(portNumber);
if (bind(m_fdIn, (struct sockaddr *)&in_name, sizeof(in_name)) < 0)
{
WARNF("bind failed, winsock error %d\n", WSAGetLastError());
exit(1);
}
So I did change the implementation from UDP broadcast to multicast. That seems to work in loopback so multiple processes can share the port.

result too large c++ sockets

I am getting an error an bind() with the value 34 (Result too large), can anyone help?
void Connect(string address, unsigned short port){
memset(&server2, 0, sizeof(server2));
server2.sin_family = AF_INET;
server2.sin_addr.s_addr = inet_addr(address.c_str());
server2.sin_port = htons(port);
desc2 = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(desc2 == -1) {
cout << "Error in Socket()" << endl;
}
if((::bind(desc2,(sockaddr*)&server2, sizeof(server2))) == -1) {
cout << "Error in Bind() " << errno << endl;
}
if((::connect(desc2, (sockaddr*)&server2, sizeof(server2))) > 0) {
cout << "Error in Connect()" << endl;
}
cout << "YOU ARE CONNECTED TO " << address << " ON PORT " << port << endl;
}
PS: I got this error 1 year ago too,the problem was simple i had write something bad when initializing the socket address, where to connect, but now again i have no clue where I made a mistake.
I don't know what caused your problem exactly to return that type of result.
You said you were a proxy server, so you are listening for incoming connections.
Try this:
server2.sin_addr.s_addr = 0;
server2.sin_family = AF_INET;
server2.sin_port = htons(port);
desc2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Puts the socket in listening mode, allowing 10 connection requests to queue.
bind(desc2, (sockaddr*)&server2, sizeof(server2));
listen(desc2, 10);
// Accepts the first connection request.
SOCKET accepted_socket = accept(desc2, NULL, NULL);
std::cout << "connection accepted!\n";
You will probably want to learn how to program asynchronously.
I don't know why you're getting 'name too long' -- without seeing how server2 was declared and defined, it's impossible to know for sure.
But I do know that calling bind() and then connect() on the same socket with the same address will fail -- bind() is assigning the local address to the socket, and connect() connects to the remote address. Giving the same address for both ends of the socket can only end badly.
Almost no protocols require bind() before connect(). (The exceptions would involve the "ports lower than 1024 can only be opened by root, so we can trust this connection" style of authentication, which hasn't been used in ages. Think rlogin.)
bind() makes most sense immediately before a listen() call. Clients will attempt to contact a server on a 'well-known port', and bind() is the mechanism you use to assign that name.

Resources