0

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

这是这里的一些示例程序:

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()whichfd作为文件描述符message并由MallocMessageBuilder.

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

我想发送message到远程服务器,该服务器将使用类似的 Cap'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),但没有工作。顺便说一句,我不理解这个概念,因为套接字不“知道”我想使用哪个 IP 和端口。我应该在使用该connect()功能时指定它们。

我试图跳过该writePackedMessageToFd()功能。连接到服务器connect()并使用 Windows 的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 连接发送和接收 Cap'n Proto 打包消息到指定的 IP:端口。

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

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

4

1 回答 1

1

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

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

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

和:

kj::HandleInputStream in((HANDLE)sock);
capnp::InputStreamMessageReader message(in);
于 2017-11-01T04:11:00.187 回答