0

I have written a function to use TransmitFile API in recv function,the program is as below:

#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;
    }

But I'm getting errors as follows:

 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

Is it because of library linking failure or something else? Is there any API to recieve data from a client socket to a file and it fulfill zero copy approach also?

Satya Kumar
  • 179
  • 1
  • 4
  • 19

1 Answers1

0

You have declared return type for CTcpClient::Recv as char *, but you are actually returning bool, did the compilation succeed before it started linking? Or do you have any other declaration of CTcpClient::Recv method?

  • no I was debugging it without any call to this function in main so it was not showing any error,but now I have corrected it.actually my primary concern was error in transmitfile API. – Satya Kumar Jun 27 '13 at 06:24