📄 23. 领略internet.txt
字号:
return TRUE ;
}
// Call "connect" with IP address and time-server port
sa.sin_family = AF_INET ;
sa.sin_port = htons (IPPORT_TIMESERVER) ;
sa.sin_addr.S_un.S_addr = inet_addr (szIPAddr) ;
connect(sock, (SOCKADDR *) &sa, sizeof (sa)) ;
// "connect" will return SOCKET_ERROR because even if it
// succeeds, it will require blocking. The following only
// reports unexpected errors.
if (WSAEWOULDBLOCK != (iError = WSAGetLastError ()))
{
EditPrintf (hwndEdit, TEXT ("Connect error #%i.\r\n"), iError) ;
closesocket (sock) ;
WSACleanup () ;
return TRUE ;
}
EditPrintf (hwndEdit, TEXT ("Connecting to %hs..."), szIPAddr) ;
// The result of the "connect" call will be reported
// through the WM_SOCKET_NOTIFY message.
// Set timer and change the button to "Cancel"
SetTimer (hwnd, ID_TIMER, 1000, NULL) ;
GetWindowText (hwndButton, szOKLabel, sizeof (szOKLabel) /sizeof (TCHAR)) ;
SetWindowText (hwndButton, TEXT ("Cancel")) ;
SetWindowLong (hwndButton, GWL_ID, IDCANCEL) ;
return TRUE ;
case IDCANCEL:
closesocket (sock) ;
sock = 0 ;
WSACleanup () ;
SetWindowText (hwndButton, szOKLabel) ;
SetWindowLong (hwndButton, GWL_ID, IDOK) ;
KillTimer (hwnd, ID_TIMER) ;
EditPrintf (hwndEdit, TEXT ("\r\nSocket closed.\r\n")) ;
return TRUE ;
case IDC_CLOSE:
if (sock)
SendMessage (hwnd, WM_COMMAND, IDCANCEL, 0) ;
DestroyWindow (GetParent (hwnd)) ;
return TRUE ;
}
return FALSE ;
case WM_TIMER:
EditPrintf (hwndEdit, TEXT (".")) ;
return TRUE ;
case WM_SOCKET_NOTIFY:
wEvent = WSAGETSELECTEVENT (lParam) ; // ie, LOWORD
wError = WSAGETSELECTERROR (lParam) ; // ie, HIWORD
// Process two events specified in WSAAsyncSelect
switch (wEvent)
{
// This event occurs as a result of the "connect" call
case FD_CONNECT:
EditPrintf (hwndEdit, TEXT ("\r\n")) ;
if (wError)
{
EditPrintf (hwndEdit, TEXT ("Connect error #%i."), wError) ;
SendMessage (hwnd, WM_COMMAND, IDCANCEL, 0) ;
return TRUE ;
}
EditPrintf (hwndEdit, TEXT ("Connected to %hs.\r\n"), szIPAddr) ;
// Try to receive data. The call will generate an error
// of WSAEWOULDBLOCK and an event of FD_READ
recv (sock, (char *) &ulTime, 4, MSG_PEEK) ;
EditPrintf (hwndEdit, TEXT ("Waiting to receive...")) ;
return TRUE ;
// This even occurs when the "recv" call can be made
case FD_READ:
KillTimer (hwnd, ID_TIMER) ;
EditPrintf (hwndEdit, TEXT ("\r\n")) ;
if (wError)
{
EditPrintf (hwndEdit, TEXT ("FD_READ error #%i."), wError) ;
SendMessage (hwnd, WM_COMMAND, IDCANCEL, 0) ;
return TRUE ;
}
// Get the time and swap the bytes
iSize = recv (sock, (char *) &ulTime, 4, 0) ;
ulTime = ntohl (ulTime) ;
EditPrintf (hwndEdit,
TEXT ("Received current time of %u seconds ")
TEXT ("since Jan. 1 1900.\r\n"), ulTime) ;
// Change the system time
ChangeSystemTime (hwndEdit, ulTime) ;
SendMessage (hwnd, WM_COMMAND, IDCANCEL, 0) ;
return TRUE ;
}
return FALSE ;
}
return FALSE ;
}
BOOL CALLBACK ServerDlg ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static char * szServer ;
static WORD wServer = IDC_SERVER1 ;
char szLabel [64] ;
switch (message)
{
case WM_INITDIALOG:
szServer = (char *) lParam ;
CheckRadioButton (hwnd, IDC_SERVER1, IDC_SERVER10, wServer) ;
return TRUE ;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDC_SERVER1:
case IDC_SERVER2:
case IDC_SERVER3:
case IDC_SERVER4:
case IDC_SERVER5:
case IDC_SERVER6:
case IDC_SERVER7:
case IDC_SERVER8:
case IDC_SERVER9:
case IDC_SERVER10:
wServer = LOWORD (wParam) ;
return TRUE ;
case IDOK:
GetDlgItemTextA (hwnd, wServer, szLabel, sizeof (szLabel)) ;
strtok (szLabel, "(") ;
strcpy (szServer, strtok (NULL, ")")) ;
EndDialog (hwnd, TRUE) ;
return TRUE ;
case IDCANCEL:
EndDialog (hwnd, FALSE) ;
return TRUE ;
}
break ;
}
return FALSE ;
}
void ChangeSystemTime (HWND hwndEdit, ULONG ulTime)
{
FILETIME ftNew ;
LARGE_INTEGER li ;
SYSTEMTIME stOld, stNew ;
GetLocalTime (&stOld) ;
stNew.wYear = 1900 ;
stNew.wMonth = 1 ;
stNew.wDay = 1 ;
stNew.wHour = 0 ;
stNew.wMinute = 0 ;
stNew.wSecond = 0 ;
stNew.wMilliseconds = 0 ;
SystemTimeToFileTime (&stNew, &ftNew) ;
li = * (LARGE_INTEGER *) &ftNew ;
li.QuadPart += (LONGLONG) 10000000 * ulTime ;
ftNew = * (FILETIME *) &li ;
FileTimeToSystemTime (&ftNew, &stNew) ;
if (SetSystemTime (&stNew))
{
GetLocalTime (&stNew) ;
FormatUpdatedTime (hwndEdit, &stOld, &stNew) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -