Click here to Skip to main content
15,795,402 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried using function capnp::writePackedMessageToFd(fd, builder) to serialize C++ message and send it as packet to the file descriptor. Using the above function in my code gave me runtime Exception.


#include "MapSchema.capnp.h"       // Include the generated schema code
#include <capnp/serialize-packed.h>
#include <iostream>
#include <winsock2.h>        // Include Winsock for UDP sockets
#include <kj/exception.h>

#pragma comment(lib, "ws2_32.lib") // Link against Winsock library

int main() {
   WSADATA wsaData;
   unsigned char* buff;
   if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
       std::cerr << "Failed to initialize Winsock." << std::endl;
       return 1;

   // Create a UDP socket
   SOCKET udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
   if (udpSocket == INVALID_SOCKET) {
       std::cerr << "Failed to create socket." << std::endl;
       return 1;
   std::cout << "WSA Success" << std::endl;

   // Define the destination UDP address
   sockaddr_in destAddr;
   destAddr.sin_family = AF_INET;
   destAddr.sin_port = htons(12345);         // Destination port
   destAddr.sin_addr.s_addr = 
                     inet_addr(""); // Destination IP address

   // Create a Cap'n Proto message
   ::capnp::MallocMessageBuilder message;
   KeyValueMap::Builder mapBuilder = message.initRoot<keyvaluemap>();

   std::cout << "ip Success" << std::endl;
   // Add entries to the map
   auto entries = mapBuilder.initEntries(2);

   kj::HandleOutputStream out((HANDLE)udpSocket);
    std::cout << "soc: " << &out;
    capnp::writePackedMessage(out, message);

   // Clean up Winsock

   return 0;

Unhandled exception at 0x76D5D9502 in file.exception Microsoft C++ exception: kj::ExceptionImp at memory location 0x0102D4D8

What I have tried:

Replacing the code snippet:
kj::HandleOutputStream out((HANDLE)udpSocket);
    std::cout << "soc: " << &out;
    capnp::writePackedMessage(out, message);

with this:
auto serializedMessage = capnp::messageToFlatArray(message);
const char* buffer = serializedMessage.asChars().begin();
int bufferLength = serializedMessage.asChars().size();

buff = const_cast<unsigned char*="">(reinterpret_cast<const unsigned="" char*="">(buffer));
int result = sendto(udpSocket, buffer, bufferLength, 0,
    (sockaddr*)&destAddr, sizeof(destAddr));
if (result == SOCKET_ERROR) {
    std::cerr << "Failed to send data: " << WSAGetLastError() << std::endl;

Solved the exception, so does that mean capnp::writePackedMessageToFd(fd, builder) cannot be used in Windows to send the serialized data directly to file descriptor?
Updated 29-Aug-23 1:36am
Richard MacCutchan 25-Aug-23 5:02am    
You first need to find out what caused the exception.
raddevus 25-Aug-23 14:01pm    
I found this, which could be similar and may just provide a bit more related info:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900