如何在winsock2中编写TransmitPackets函数?

发布于 2024-09-12 10:18:17 字数 137 浏览 9 评论 0原文

我正在使用 UDP 在 VC++(服务器)中编写文件传输应用程序。我在winsock2中了解到,有一些在文件传输中很有用的函数。任何人都可以帮助我吗?我也在寻找winsock2 的TransmitPackets 示例应用程序,但不是gettng。请帮我。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-09-19 10:18:17

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

这里是示例应用程序和源代码,可以帮助您。

编辑:

以下是发送方函数,它接受字符串、字符串大小、IP 和端口并通过 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 Function For Sending Packet

下面是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

       }

     }

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 Function 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

       }

     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文