我写了一个函数TransmitFile
API
在recv
函数中使用,程序如下:
#include "stdafx.h"
#include "tcp.h"
#include<windows.h>
#include<Mswsock.h>
bool CTcpClient::Start() {
WSADATA wsaData;
// Initialize Winsock
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return false;
}
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(szServerName,Port, &hints, &result);
if (iResult != 0)
{
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return false;
}
ptr = result;
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET)
{
printf("Error at socket(): %d\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return false;
}
// Connect to server
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET)
{
printf("Unable to connect to server!\n");
WSACleanup();
return false;
}
return true;
}
char* CTcpClient::Recv()
{
LPCTSTR file;
HANDLE hFile = CreateFile( file,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(TransmitFile(ConnectSocket,hFile,65536, 65536, NULL, NULL, TF_USE_KERNEL_APC & TF_WRITE_BEHIND) == true)
{
printf("transmitfile function worked correctly\n");
}
else
printf("TransmitFile error\n");
return false;
}
但我收到如下错误:
Error3 error LNK2019: unresolved external symbol _TransmitFile@28 referenced in function "public: char * __thiscall CTcpClient::Recv(void)" (?Recv@CTcpClient@@QAEPADXZ) c:\Users\damyant\documents\visual studio 2010\Projects\qttest\qttest\tcp.obj
是因为库链接失败还是其他原因?是否有任何 API 可以从客户端套接字接收数据到文件并且它也实现零复制方法?