⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winudp.c

📁 Socket 编程的DOS下和Windows开发包 Microsoft TCP/IP SDK for DOS/Windows
💻 C
📖 第 1 页 / 共 2 页
字号:
            
        case WM_PAINT:
            /* This program has a simplistic output method.  It repaints
            * the entire screen for every WM_PAINT message.  Each time
            * it adds a line of output it calls inc_nI() to increment the
            * line number in the output buffer, and it calls InvalidateRect()
            * so the screen will be repainted.
            */
            if (nI==0) {
                return (DefWindowProc(hWnd, message, wParam, lParam));
            }
            BeginPaint(hWnd,&ps);
            for (nJ=1;nJ<=nI;nJ++) {
                TextOut(ps.hdc,xChar,yChar * nJ,&sztextarr[nJ-1][0],ntextlarr[nJ-1]);
            }
            EndPaint (hWnd, &ps);
            break;

        case WM_COMMAND:
            switch (wParam) {
                case IDM_UDP:
                    nI=0;
                    udp_cmds(hWnd);
                    break;

                case IDM_ABOUT:
                    lpProcAbout = MakeProcInstance(About, hInst);
                    DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
                    FreeProcInstance(lpProcAbout);
                    break;
                }
                break;

        case WM_DESTROY:
            PostQuitMessage(NULL);
            break;

        default:
            return (DefWindowProc(hWnd, message, wParam, lParam));
    }
    return (NULL);
}

/****************************************************************************

    FUNCTION: udp_cmds(HWND)

    PURPOSE:  Creates 2 sockets, binds address and send and receives a 
    packet.  When complete, or if and error occurs cleanup is called.

****************************************************************************/

VOID PASCAL udp_cmds(hWnd)
HWND hWnd;

{
    int nJ;
    int nFromLen;
    int nSd1,nSd2; 
    int nRc, nLen;
    struct hostent far *hp;
    struct sockaddr_in nSd1_addr, nSd2_addr, from;
    char far * szIP;

    ntextlarr[nI]=sprintf(&sztextarr[nI][0],"Starting UDP sample application");
                    inc_nI();
    InvalidateRect(hWnd,NULL,TRUE);

    /* create nSd1 socket */
    nSd1= socket( AF_INET, SOCK_DGRAM, 0);
    if (nSd1 < 0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"1st socket call failed, error=%d ",errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
        return;
    }

    /* create nSd2 socket */
    nSd2= socket( AF_INET, SOCK_DGRAM, 0);
    if (nSd2 < 0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"2nd socket call failed, error=%d",errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
        cleanup(hWnd, nSd1, nSd2); 
        return;
    }

    /* get IP address of local node */
    hp = gethostbyname( "local_node");
    if (hp ==NULL) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"can't continue, 'local_node' not found in hosts table");
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
        cleanup(hWnd, nSd1, nSd2); 
        return;
    }

    /* bind both sockets to hard coded ports */
    nSd1_addr.sin_family=AF_INET;
    nSd1_addr.sin_port=htons(PORT1);       
    nSd1_addr.sin_addr.s_addr=
                  *( (unsigned long far *) hp->h_addr_list[0]);
    nRc=bind( nSd1, (struct sockaddr far *) &nSd1_addr,
                sizeof(nSd1_addr));
    if (nRc <0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"bind of socket nSd1=%d failed, error=%d",nSd1,errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
        cleanup(hWnd, nSd1, nSd2);
        return;
    }

    nSd2_addr.sin_family=AF_INET;
    nSd2_addr.sin_port=htons(PORT2);       
    nSd2_addr.sin_addr.s_addr =
                  *( (unsigned long far *) hp->h_addr_list[0]);
    nRc=bind( nSd2, (struct sockaddr far *) &nSd2_addr,
                sizeof(nSd2_addr));
    if (nRc <0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"bind of socket nSd2=%d failed, error=%d",nSd1,errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
        cleanup(hWnd, nSd1, nSd2); 
        return;
    }

    /* initialize buffer */
    for (nJ=0; nJ<BUFFER_SIZE; nJ++)
        ssendbuff[nJ] =nJ;

    /* send data from nSd1 to nSd2 */
    nLen=sendto( nSd1, ssendbuff, BUFFER_SIZE, 0, 
                  (struct sockaddr * far) &nSd2_addr, 
                  sizeof(struct sockaddr_in));
    if (nLen <0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"Winsendto failed, error=%d",errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
        cleanup(hWnd, nSd1, nSd2);
        return;
    }
    else {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"sent packet");
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
    }

    /* receive at nSd2 from nSd1 */
    nFromLen=sizeof(from);
    nLen =recvfrom( nSd2, srecvbuff, BUFFER_SIZE, 0,
                      (struct sockaddr * far) &from, &nFromLen);
    if (nLen <0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"Winrecvfrom failed, error=%d",errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
        cleanup(hWnd, nSd1, nSd2); 
        return;
    }
    else {
        szIP = inet_ntoa(from.sin_addr);
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"received packet from IP addr=%s", szIP);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
    }
    ntextlarr[nI]=sprintf(&sztextarr[nI][0],"End of UDP sample program.");
    inc_nI();
    InvalidateRect(hWnd,NULL,TRUE);
    cleanup(hWnd, nSd1, nSd2);
}

/****************************************************************************

    FUNCTION: cleanup(HWND)

    PURPOSE:  Closes sockets and free the windows sockets DLL 

****************************************************************************/

VOID PASCAL cleanup(hWnd, nSd1, nSd2)
HWND  hWnd;
int	nSd1, nSd2;

{
    int nRc; 
    short errno;

    nRc= close_socket( nSd1);
    if (nRc < 0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"Error on socket close.  socket=%d, error=%d",nSd1,errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
    }

    nRc= close_socket( nSd2);
    if (nRc < 0) {
        ntextlarr[nI]=sprintf(&sztextarr[nI][0],"Error on socket close.  socket=%d, error=%d",nSd2,errno);
        inc_nI();
        InvalidateRect(hWnd,NULL,TRUE);
    }

}

/****************************************************************************

    FUNCTION: About(HWND, unsigned, WORD, LONG)

    PURPOSE:  Processes messages for "About" dialog box

    MESSAGES:

        WM_INITDIALOG - initialize dialog box
        WM_COMMAND    - Input received

****************************************************************************/

BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
HWND hDlg;
unsigned message;
WORD wParam;
LONG lParam;
{
    switch (message) {
        case WM_INITDIALOG:
            return (TRUE);

        case WM_COMMAND:
            if (wParam == IDOK
                || wParam == IDCANCEL) {
                EndDialog(hDlg, TRUE);
                return (TRUE);
            }
            return (TRUE);
    }
    return (FALSE);
}

/****************************************************************************

    FUNCTION: inc_nI

    PURPOSE: Increments the line number in the screen output buffer.

****************************************************************************/

VOID PASCAL inc_nI() 
{
        if (nI <MAXLINES -1) {
                nI++;
        }
        else {
                nI++;
                nI =0;
                ntextlarr[nI]=sprintf(&sztextarr[nI][0],"--wrapping around--");
                nI = 1;
        }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -