在C ++中通过TCP发送Cap' n Proto消息

时间:2017-10-31 23:07:03

标签: windows sockets networking tcp capnproto

我对网络很陌生,刚开始使用Cap'n Proto

这是来自here的一些示例程序:

void writeAddressBook(int fd) {
  ::capnp::MallocMessageBuilder message;

  AddressBook::Builder addressBook = message.initRoot<AddressBook>();
  ::capnp::List<Person>::Builder people = addressBook.initPeople(2);

  Person::Builder alice = people[0];
  alice.setId(123);
  alice.setName("Alice");
  alice.setEmail("alice@example.com");
  // Type shown for explanation purposes; normally you'd use auto.
  ::capnp::List<Person::PhoneNumber>::Builder alicePhones =
      alice.initPhones(1);
  alicePhones[0].setNumber("555-1212");
  alicePhones[0].setType(Person::PhoneNumber::Type::MOBILE);
  alice.getEmployment().setSchool("MIT");

  Person::Builder bob = people[1];
  bob.setId(456);
  bob.setName("Bob");
  bob.setEmail("bob@example.com");
  auto bobPhones = bob.initPhones(2);
  bobPhones[0].setNumber("555-4567");
  bobPhones[0].setType(Person::PhoneNumber::Type::HOME);
  bobPhones[1].setNumber("555-7654");
  bobPhones[1].setType(Person::PhoneNumber::Type::WORK);
  bob.getEmployment().setUnemployed();

  writePackedMessageToFd(fd, message);
}

最后一行使用writePackedMessageToFd()fd作为文件描述符,message创建MallocMessageBuilder

我使用Visual Studio 2017在Windows上工作。

我想将message发送到远程服务器,该服务器将使用类似的Cap&nbsp; nP对象进行回答。

问题是如何发送并接收答案?

我尝试按以下方式初始化并创建套接字:

    //Initialize WinSock
    WSAData data;
    WORD ver = MAKEWORD(2, 2);
    int wsResult = WSAStartup(ver, &data);
    if (wsResult != 0) {
        cerr << "Can't start WinSock, Error #" << wsResult << endl;
        return;
    }
    else {
        cout << "Socket initialized!" << endl;
    }


    //Create socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET) {
        cerr << "Can't create socket, Error #" << WSAGetLastError() << endl;
        WSACleanup();
        return;
    }
    else {
        cout << "Socket created!" << endl;
    }

如果一切正常socket(),则返回文件描述符。 所以我刚才用过 writePackedMessageToFd(sock, message),但没有成功。 顺便说一下,我不理解这个概念,因为套接字不知道&#34;我想要使​​用哪个IP和端口。我应该在使用connect()函数时指定它们。

我试图跳过writePackedMessageToFd()功能。使用connect()连接到服务器并且只使用了Windows&#39; send()函数发送message。像这样:

    string ipAddress = "127.0.0.1";     //IP Address of server 
    int port = 58661;           //Listening port of server

    //Initialize WinSock
    WSAData data;
    WORD ver = MAKEWORD(2, 2);
    int wsResult = WSAStartup(ver, &data);
    if (wsResult != 0) {
        cerr << "Can't start WinSock, Error #" << wsResult << endl;
        return;
    }
    else {
        cout << "Socket initialized!" << endl;
    }


    //Create socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET) {
        cerr << "Can't create socket, Error #" << WSAGetLastError() << endl;
        WSACleanup();
        return;
    }
    else {
        cout << "Socket created!" << endl;
    }

    //Fill in a hint structure
    sockaddr_in hint;
    hint.sin_family = AF_INET;
    hint.sin_port = htons(port);
    inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);

    //Connect to server
    int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
    if (connResult == SOCKET_ERROR) {
        cerr << "Can't connect to server, Error #" << WSAGetLastError() << endl;
        closesocket(sock);
        WSACleanup();
        return;
    }
    else {
        cout << "Connected to " << ipAddress << " on port " << port << endl;
    }

    //Send and receive data
    char buf[4096];
    //string mess = "Hello";

    //Send message
    //int sendResult = send(sock, mess.c_str(), mess.size() + 1, 0);
    int sendResult = send(sock, (const char*)&message, sizeof(message) + 1, 0);


    //Wait for response
    ZeroMemory(buf, 4096);
    int bytesReceived = recv(sock, buf, 4096, 0);
    if (bytesReceived > 0) {
        cout << "SERVER>" << string(buf, 0, bytesReceived) << endl;
    }


    //Close down everything
    closesocket(sock);
    WSACleanup();
    cout << "Connection closed!" << endl;

这个发送了一些东西,但它肯定是错的,因为服务器没有回应。

简而言之:我想通过TCP连接向指定的IP:端口发送和接收Cap&#n; Proto打包消息。

我怎么能做到这一点?我真的需要一些帮助。

我真的非常感谢你的帮助!提前致谢!

1 个答案:

答案 0 :(得分:1)

在Windows上,socket()不返回文件描述符。它返回一个Windows SOCKET,实际上是HANDLE强制转换为整数。在Windows上,“文件描述符”在C运行时库中实现为兼容层;操作系统不直接支持它们。

您可以使用kj::HandleInputStreamkj::HandleOutputStream在Windows上的套接字上执行I / O.

kj::HandleOutputStream out((HANDLE)sock);
capnp::writeMessage(out, message);

kj::HandleInputStream in((HANDLE)sock);
capnp::InputStreamMessageReader message(in);