我正在尝试使用 VMCI 套接字在虚拟机与其主机之间建立面向流的连接。我成功启动服务器,绑定地址,进入监听模式,调用accept等待客户端。但是,每当我connect(...)
从客户那里打电话时,我都会收到WSAECONNRESET
错误消息。
我的客户代码是:
int sockfd;
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
// initialize sockets for win32
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
perror("Could not register with Winsock DLL.\n");
exit(-1);
}
// get VMCI socket file descriptor
int afVMCI = VMCISock_GetAFValue();
if ((sockfd = socket(afVMCI, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(-1);
}
// initialize server address
struct sockaddr_vm their_addr = {0};
their_addr.svm_family = afVMCI;
their_addr.svm_cid = 2;
their_addr.svm_port = 1234;
// connect to server
if ((connect(sockfd, (struct sockaddr *) &their_addr, sizeof(their_addr))) == -1) {
int e = WSAGetLastError();
printf("Error: %d\n", e);
exit(-1);
}
printf("Connected!\n");
每当我使用数据报套接字时,问题都不存在。(当然,无需调用listen、accept 和connect。在这种情况下,我只需使用sendto(...)
which 就可以了。)