?? mysqurt.c
字號(hào):
closesocket (s_sock);
return -3;
}
// Wait for remote requests.
while (fContinue) {
// Block on accept.
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());
break;
}
Add2List (hWnd, TEXT ("sock accept..."));
hThread = CreateThread (NULL, 0, SenderThread,
(PVOID)t_sock, 0, &rc);
if (hThread)
CloseHandle (hThread);
}
closesocket (s_sock);
return rc;
}
//======================================================================
// SenderThread - Sends the file requested by the remote device
//
int SenderThread (PVOID pArg) {
SOCKET t_sock = (SOCKET)pArg;
int nCnt, nFileSize, rc;
TCHAR szFileName[MAX_PATH];
PBYTE pBuff, pPtr;
HWND hWnd = hMain;
HANDLE hFile;
Add2List (hWnd, TEXT ("sender thread entered"));
// Read the number of bytes in the filename.
rc = recv (t_sock, (PBYTE)&nCnt, sizeof (nCnt), 0);
if ((rc == SOCKET_ERROR) || (nCnt > MAX_PATH)) {
Add2List (hWnd, TEXT ("failed receiving name size"));
closesocket (t_sock);
return -1;
}
// Read the filename.
rc = recv (t_sock, (PBYTE)&szFileName, nCnt, 0);
if (rc == SOCKET_ERROR) {
Add2List (hWnd, TEXT ("failed receiving name"));
closesocket (t_sock);
return -2;
}
Add2List (hWnd, TEXT ("name: %s"), szFileName);
hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
Add2List (hWnd, TEXT ("file opened failed. rc %d"),
GetLastError());
rc = BAD_FILEOPEN;
} else {
rc = 0;
nFileSize = GetFileSize (hFile, NULL);
// Allocate buffer and read file.
pBuff = LocalAlloc (LPTR, nFileSize);
if (pBuff) {
ReadFile (hFile, pBuff, nFileSize, &nCnt, NULL);
if (nCnt != nFileSize)
rc = BAD_FILEREAD;
} else
rc = BAD_MEMORY;
}
// Start transfer. First send size and get acknowledgment.
if (!rc) {
// Send file size. Size will always be < 2 GB.
rc = send (t_sock, (PBYTE)&nFileSize, sizeof (nFileSize), 0);
if (rc == SOCKET_ERROR)
rc = BAD_SOCKET;
else
// Receive acknowledgment of file size.
recv (t_sock, (PBYTE)&rc, sizeof (rc), 0);
}
// Send the file.
pPtr = pBuff;
while ((!rc) && nFileSize) {
// Send up to the block size.
nCnt = min (BLKSIZE, nFileSize);
rc = send (t_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 acknowledgment.
recv (t_sock, (PBYTE)&rc, sizeof (rc), 0);
}
// Send close code.
if (rc != BAD_SOCKET)
send (t_sock, (PBYTE)&rc, sizeof (rc), 0);
closesocket (t_sock);
// Clean up.
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle (hFile);
LocalFree (pBuff);
Add2List (hWnd, TEXT ("sender thread exit"));
return 0;
}
//----------------------------------------------------------------------
// GetFile - Reads a file from the remote device
//
int GetFile (HWND hWnd, TCHAR *szFileName) {
SOCKET c_sock;
HANDLE hFile;
INT rc, nSize, i, nFileSize, nCnt;
char cDevice[256];
SOCKADDR_IRDA iraddr;
DEVICELIST *pDL;
STORE_INFORMATION si;
PBYTE pBuff;
// 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());
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)
break;
Sleep(500);
}
// If no device found, exit.
if (pDL->numDevice == 0) {
closesocket (c_sock);
return -1;
}
// 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're 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..."));
// Send name size.
nCnt = (lstrlen (szFileName) + 1) * sizeof (TCHAR);
rc = send (c_sock, (PBYTE)&nCnt, sizeof (nCnt), 0);
if (rc != SOCKET_ERROR) {
// Send filename.
rc = send (c_sock, (PBYTE)szFileName, nCnt, 0);
}
pBuff = LocalAlloc (LPTR, BLKSIZE); // Create buffer for file
// Receive file size.
rc = recv (c_sock, (PBYTE)&nFileSize, sizeof (nFileSize), 0);
Add2List (hWnd, TEXT ("received file size of %d bytes"), nFileSize);
if ((rc != SOCKET_ERROR) && (nFileSize > 0)) {
GetStoreInformation (&si);
Add2List (hWnd, TEXT ("free space of %d bytes"), si.dwFreeSize);
if ((INT)si.dwFreeSize < nFileSize + 1000)
rc = BAD_FILESIZE;
else
rc = GOOD_XFER;
if (rc == GOOD_XFER) {
// Create the file. Overwrite if user says so.
hFile = CreateFile (szFileName, GENERIC_WRITE, 0, NULL,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
if (GetLastError() == ERROR_FILE_EXISTS) {
i = MessageBox (hWnd,
TEXT ("File already exists. Replace?"),
szAppName, MB_YESNO);
if (i == IDYES)
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 acknowledgment code.
Add2List (hWnd, TEXT ("Sending size ack."));
send (c_sock, (PBYTE)&rc, sizeof (rc), 0);
//
// Receive file.
//
while ((!rc) && (nFileSize > 0)) {
nCnt = min (BLKSIZE, nFileSize);
for (nSize = 0; nSize < nCnt;) {
i = recv (c_sock, 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 %d bytes."), nSize);
if (i) {
if (!WriteFile (hFile, pBuff, nSize, &i, 0))
rc = BAD_FILEWRITE;
nFileSize -= i;
} else
Sleep(50);
// Send acknowledgment of packet.
send (c_sock, (PBYTE)&rc, sizeof (rc), 0);
}
} else if (rc == BAD_FILEOPEN)
Add2List (hWnd, TEXT ("File not found."));
Add2List (hWnd, TEXT ("receive finished"));
CloseHandle (hFile);
closesocket (c_sock);
LocalFree (pBuff);
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -