views:

41

answers:

2

Hi all, I'm writing a file transfer application in VC++(Server) using UDP. I came to know in winsock2, there are some functions which are useful in file transfer. Can anybody help me. I'm also looking for a sample application of TransmitPackets of winsock2 but not gettng. Please help me. Thank you.

A: 

http://www.mycplus.com/source-code/c-source-code/udp-sender-and-receiver/

http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedcode1e.html

http://msdn.microsoft.com/en-us/library/ms740566%28VS.85%29.aspx

Here is the sample application and source code which will help you.

edited:

Following is the sender function which take string, size of string, IP and Port and send packets over UDP.

int sender(char cSendBuffer[], int iBufferSize, char cIP[], int iPort)
{
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2), &wsaData);

    char cBroadcast = '1';
    int iNumBytes = 0;

    struct sockaddr_in their_addr;
    struct hostent *he;

    SOCKET sock;
    sock = socket(AF_INET,SOCK_DGRAM,0);

    if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&cBroadcast,sizeof(cBroadcast)) < 0)
    {
        printf("\n ----------------------------------------- \n");
        printf("Error in setting UDP option");
        printf("\n ----------------------------------------- \n");
        return 0;
    }//End if

    their_addr.sin_family      = AF_INET;
    their_addr.sin_port        = htons(iPort);
    //Target IP
    their_addr.sin_addr.s_addr = inet_addr (cIP);                       

    int iSentBytes =0;

    //Send bytes through socket
    iSentBytes = sendto(sock,cSendBuffer, iBufferSize,0,(sockaddr*)&their_addr,sizeof(their_addr));

    if( iSentBytes < 0)
    {
        printf("\n ----------------------------------------- \n");
        printf("Data Sending Error");
        printf("\n ----------------------------------------- \n");
        closesocket(sock);
        return 0;
    }//End if

    else
    {
        printf("\n ----------------------------------------- \n");
        printf("\n Data sent successfully to AT PORT:%d AND IP:%s \n",iPort,cIP);
        printf("\n ----------------------------------------- \n");
    }//End else

        closesocket(sock);
        WSACleanup();
        return 1;

}//End Fuction For Sending Packet

Following is the receiver function of UDP.

    int UDPReceiver( void )
    {
        char cRecievedBuffer[TRACK_BUFFER_SIZE];

        WSADATA wsaData;
        WSAStartup(MAKEWORD(2,2), &wsaData);
        SOCKET sock;
        sock = socket(AF_INET,SOCK_DGRAM,0);

        // my address information
        struct sockaddr_in  my_addr;
        // connector's address information
        struct sockaddr_in  their_addr;

        //sizeof (ANSI C function)
        int len = sizeof(struct sockaddr_in);

        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(MYPORT);

        //Automatically fill with my IP
        my_addr.sin_addr.s_addr = INADDR_ANY;

        if (bind(sock,(sockaddr*)&my_addr, sizeof (my_addr)) < 0)
        {
            printf("\n ----------------------------------------- \n");
            printf(" Error in BINDING ");
            printf("\n ----------------------------------------- \n");
            return 0;
        }//End if

        while(recvfrom(sock,cRecievedBuffer,TRACK_BUFFER_SIZE,0,(sockaddr *)&their_addr,&len))
        {

//Your Decoder code

       }

     }
Arman
Thanks for reply Arman...but here I dint get the function I wanted. I'm looking for TransmitPackets function sample application. I'm not getting how to use this function. If possible give more information. Thanks again.
Kishor Kumar
@Kishor, I have edited my post, now check it, I hope this will helping for you.
Arman
@Arman you did not use the TransmitPackets function, so once again the example is irrelevant. @Kishor - do not mark replies as answers if they do not answer your question.
bowenl2
A: 

This explains it: http://msdn.microsoft.com/en-us/library/ms740566%28VS.85%29.aspx

bowenl2