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

📄 mysquirt.cpp

📁 《Windows CE 6.0开发者参考》(《Programming Windows Embedded CE 6.0 Developer Reference》)第四版书中的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    va_list args;
    va_start(args, lpszFormat);

    nBuf = StringCchVPrintf(szBuffer, dim (szBuffer), 
                            (wchar_t *)lpszFormat, args);

    SetWindowText (hWnd, (LPCWSTR)szBuffer);
    va_end(args);
}
//======================================================================
// MonitorThread - Monitors for connections; connects and notifies
// user when a connection occurs.
//
DWORD WINAPI MonitorThread (PVOID pArg) {
    HWND hWnd = (HWND)pArg;
    INT rc, nSize, i;
    SOCKADDR_IRDA iraddr, t_iraddr;
    SOCKET t_sock, s_sock;

    Add2List (hWnd, TEXT("Monitor thread entered"));

    // Open an infrared socket.
    s_sock = socket (AF_IRDA, SOCK_STREAM, 0);
    if (s_sock == INVALID_SOCKET) {
        Add2List (hWnd, TEXT("Socket failed. rc %d"), WSAGetLastError());
        return 0;
    }
    // Fill in irda socket address structure.
    iraddr.irdaAddressFamily = AF_IRDA;
    for (i = 0; i < dim (iraddr.irdaDeviceID); i++)
        iraddr.irdaDeviceID[i] = 0;
    memcpy (iraddr.irdaServiceName, chzAppName, sizeof (chzAppName) + 1);

    // Bind address to socket.
    rc = bind (s_sock, (struct sockaddr *)&iraddr, sizeof (iraddr));
    if (rc) {
        Add2List (hWnd, TEXT(" bind failed"));
        closesocket (s_sock);
        return 0;
    }
    // Set socket into listen mode.
    rc = listen (s_sock, SOMAXCONN);
    if (rc == SOCKET_ERROR) {
        Add2List (hWnd, TEXT(" listen failed %d"), GetLastError());
        closesocket (s_sock);
        return 0;
    }
    // Wait for remote requests.
    // Block on accept.
    while (fContinue) {
        nSize = sizeof (t_iraddr);
        t_sock = accept (s_sock, (struct sockaddr *)&t_iraddr, &nSize);
        if (t_sock == INVALID_SOCKET) {
            Add2List (hWnd, TEXT(" accept failed %d"), GetLastError());
        }
        Add2List (hWnd, TEXT("sock accept..."));
        HANDLE hTh = MyCreateThread (NULL, 0, ReceiveThread, 
                                     (PVOID)t_sock, 0, NULL);
        CloseHandle (hTh);
    }
    closesocket (s_sock);
    Add2List (hWnd, TEXT("Monitor thread exit"));
    return 0;
}
//======================================================================
// ReceiveThread - Receives the file requested by the remote device
//
DWORD WINAPI ReceiveThread (PVOID pArg) {
    SOCKET t_sock = (SOCKET)pArg;
    HWND hWnd = hMain; // I抦 cheating here.
    int nCnt, nFileSize, rc;
    TCHAR szFileName[MAX_PATH];
    PBYTE pBuff;
    int i, nSize, nTotal;
    DWORD dwBytes;
    HANDLE hFile;
    Add2List (hWnd, TEXT("receive thread entered"));
    SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_ABOVE_NORMAL);

    // Read the number of bytes in the filename.
    rc = recv (t_sock, (LPSTR)&nCnt, sizeof (nCnt), 0);
    if ((rc == SOCKET_ERROR) || (nCnt > MAX_PATH)) {
        Add2List (hWnd, TEXT("failed receiving name size"));
        closesocket (t_sock);
        return 0;
    }
    // Read the filename. If Win Mobile, put file in my documents.
#if defined(WIN32_PLATFORM_PSPC)
    StringCchCopy (szFileName, dim (szFileName), L"\\my documents\\"); 
#else
    StringCchCopy (szFileName, dim (szFileName), L"\\");   
#endif //defined(WIN32_PLATFORM_PSPC)
    i = (int) wcslen (szFileName);             
    rc = recv (t_sock, (LPSTR)&szFileName[i], nCnt, 0);
    if (rc == SOCKET_ERROR) {
        Add2List (hWnd, TEXT("failed receiving name"));
        closesocket (t_sock);
        return 0;
    }
    Add2List (hWnd, TEXT("name: %s"), szFileName);

    pBuff = (PBYTE)LocalAlloc (LPTR, BLKSIZE); //Create buff for file.
    //
    // Receive file size.
    //
    rc = recv (t_sock, (LPSTR)&nFileSize, sizeof (nFileSize), 0);
    Add2List (hWnd, TEXT("received file size of %d bytes"), nFileSize);

    if ((rc != SOCKET_ERROR) && (nFileSize > 0)) {
        // We should really check here to see if there is enough 
        // free space to receive the file.

        // Create the file. Overwrite if user says so.
        rc = 0;
        hFile = CreateFile (szFileName, GENERIC_WRITE, 0, NULL,
                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile == INVALID_HANDLE_VALUE) {
            Add2List (hWnd, TEXT("File Open failed. rc %d"), 
                      GetLastError());
            rc = BAD_FILEWRITE;
        }
        // Send ack code.
        Add2List (hWnd, TEXT("Sending size ack.")); 
        send (t_sock, (LPSTR)&rc, sizeof (rc), 0);
        //
        // Receive file.
        //
        nTotal = nFileSize;
        while ((!rc) && (nFileSize > 0)) {

            MySetWindowText (hWnd, TEXT ("%02d%% received"), 
                             (nTotal-nFileSize)*100/nTotal);
            nCnt = min (BLKSIZE, nFileSize);
            for (nSize = 0; nSize < nCnt;) {
                i = recv (t_sock, (LPSTR)pBuff+nSize, nCnt-nSize, 0);
                if (i == SOCKET_ERROR) {
                    Add2List (hWnd, TEXT("recv socket err %d"), 
                              GetLastError()); 
                    rc = BAD_SOCKETRECV;
                    break;
                } 
                nSize += i;
            }
            Add2List (hWnd, TEXT("recv抎 %d bytes."), nSize); 
            if (i) {
                if (!WriteFile (hFile, pBuff, nSize, &dwBytes, 0))
                    rc = BAD_FILEWRITE;
                nFileSize -= dwBytes;
            } else
                Sleep(50);
            // Send ack of packet.
            send (t_sock, (LPSTR)&rc, sizeof (rc), 0);
        }
    } else if (rc == BAD_FILEOPEN)
        Add2List (hWnd, TEXT("File not found.")); 
    Add2List (hWnd, TEXT("receive finished"));
    SetWindowText (hWnd, szTitle);
    LocalFree (pBuff);  
    CloseHandle (hFile);
    Add2List (hWnd, TEXT("receive thread exit"));
    return 0;
}
//----------------------------------------------------------------------
// SendFile - Sends a file to the remote device
//
DWORD WINAPI SendFileThread (PVOID pArg) {
    TCHAR *szFileName = (LPTSTR)pArg;
    HWND hWnd = hMain;
    SOCKET c_sock;
    HANDLE hFile;
    INT rc, nSize, i, nFileSize, nTotal, nCnt;
    char cDevice[256];
    SOCKADDR_IRDA iraddr;
    DEVICELIST *pDL;
    LPSTR pPtr;
    PBYTE pBuff;

    // Open the file.
    hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ,
                        NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        Add2List (hWnd, TEXT("File open failed. rc %d"), 
                  GetLastError());
        return -1;
    }

    // Open an infrared socket.
    c_sock = socket (AF_IRDA, SOCK_STREAM, 0);
    if (c_sock == INVALID_SOCKET) {
        Add2List (hWnd, TEXT("Sock failed. rc %d"), WSAGetLastError());
        CloseHandle (hFile);
        return 0;
    }
    // Search for someone to talk to.
    for (i = 0; i < 5; i++) {
        memset (cDevice, 0, sizeof (cDevice));
        nSize = sizeof (cDevice);
        rc = getsockopt (c_sock, SOL_IRLMP, IRLMP_ENUMDEVICES, 
                         cDevice, &nSize);
        if (rc)
            Add2List (hWnd, TEXT("Getsockopt failed. rc %d"), 
                      WSAGetLastError());

        pDL = (DEVICELIST *) cDevice;
        if (pDL->numDevice) {
            Add2List (hWnd, TEXT("%d devices found."), pDL->numDevice); 
            break;
        }
        Sleep(500);
    }
    // If no device found, exit.
    if (pDL->numDevice == 0) {
        closesocket (c_sock);
        CloseHandle (hFile);
        Add2List (hWnd, TEXT("No infrared devices found in range."));
        return -2;
    }

    //
    // Copy address of found device.
    //
    memset (&iraddr, 0, sizeof (iraddr));
    iraddr.irdaAddressFamily = AF_IRDA;
    memcpy (iraddr.irdaDeviceID, pDL->Device[0].irdaDeviceID, 4);
    //
    // Now initialize the specific socket we抮e interested in.
    //
    memcpy (iraddr.irdaServiceName, chzAppName, sizeof (chzAppName)+1);
    Add2List (hWnd, TEXT("Found: %hs"), pDL->Device[0].irdaDeviceName);

    //
    // Connect to remote socket.
    //
    rc = connect (c_sock, (struct sockaddr *)&iraddr, sizeof (iraddr));
    if (rc) {
        Add2List (hWnd, TEXT("Connect failed. rc %d"), WSAGetLastError());
        closesocket (c_sock);
        return -4;
    }
    Add2List (hWnd, TEXT("connected..."));

    rc = 0;
    nFileSize = GetFileSize (hFile, NULL);

    // Allocate buffer and read file.
    pBuff = (LPBYTE)LocalAlloc (LPTR, nFileSize);
    if (pBuff) {
        ReadFile (hFile, pBuff, nFileSize, (DWORD *)&nCnt, NULL);
        if (nCnt != nFileSize)
            rc = BAD_FILEREAD;
    } else
        rc = BAD_MEMORY;

    if (rc) {
        closesocket (c_sock);
        CloseHandle (hFile);
        Add2List (hWnd, TEXT("Error allocating buffer or reading file."));
        return rc;
    }
    // Start transfer. First send size and get ack.
    
    // Strip off any leading path, assume len > 1 since we抳e opened file.
    for (i = lstrlen (szFileName)-1; (i > 0) && 
                                     (szFileName[i] != TEXT ('\\')) ; i--);
    if (szFileName[i] == TEXT ('\\')) i++;
    // Send name size.
    nCnt = ((lstrlen (&szFileName[i]) + 1) * sizeof (WCHAR));
    rc = send (c_sock, (LPSTR)&nCnt, sizeof (nCnt), 0);

    // Send filename.
    if (rc != SOCKET_ERROR) 
        rc = send (c_sock, (LPSTR)szFileName, nCnt, 0);

    if (rc != SOCKET_ERROR)  {
        // Send file size. Size will always be < 2 gig.
        rc = send (c_sock, (LPSTR)&nFileSize, sizeof (nFileSize), 0);
        if (rc == SOCKET_ERROR) 
            rc = BAD_SOCKET;
        else
            // Recv ack of file size.
            recv (c_sock, (LPSTR)&rc, sizeof (rc), 0);

        // Send the file.
        nTotal = nFileSize;
        pPtr = (LPSTR)pBuff;
        while ((!rc) && nFileSize) {

            MySetWindowText (hWnd, TEXT ("%02d%% sent"), 
                             (nTotal-nFileSize)*100/nTotal);
            // Send up to the block size.
            nCnt = min (BLKSIZE, nFileSize);
            rc = send (c_sock, pPtr, nCnt, 0);
            if (rc == SOCKET_ERROR) {
                Add2List (hWnd, TEXT("send error %d "), GetLastError());
                rc = BAD_SOCKET;
            } else
                Add2List (hWnd, TEXT("sent %d bytes"), rc);
            pPtr += rc;
            nFileSize -= rc;

            // Receive ack.
            recv (c_sock, (LPSTR)&rc, sizeof (rc), 0);
        }
        SetWindowText (hWnd, szTitle);
    }
    // Send close code.
    if (rc != BAD_SOCKET) 
        send (c_sock, (LPSTR)&rc, sizeof (rc), 0);

    closesocket (c_sock);
    // Clean up.
    CloseHandle (hFile);
    LocalFree (pBuff);  
    if (rc)
        Add2List (hWnd, TEXT("SendFile Exit rc = %d"), rc);
    else
        Add2List (hWnd, TEXT("File sent successfully."));
    return 0;
}  

⌨️ 快捷键说明

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