Visual C++ 6.0 を起動してプロジェクトを作成 |
|
「プロジェクト名」 : WINSOCK
1) Win32 Application を指定。
2) 標準的な "Hello World!" アプリケーションを選択
|
|
削除前
|
case WM_PAINT:
hdc = BeginPaint (hWnd, &ps);
// TODO: この位置に描画用のコードを追加してください...
RECT rt;
GetClientRect( hWnd, &rt );
DrawText( hdc, szHello, strlen(szHello), &rt, DT_CENTER );
EndPaint( hWnd, &ps );
break;
| |
|
削除後
|
case WM_PAINT:
hdc = BeginPaint (hWnd, &ps);
// TODO: この位置に描画用のコードを追加してください...
EndPaint( hWnd, &ps );
break;
| |
|
|
|
追加前
|
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if( !hWnd )
{
return FALSE;
}
| |
|
追加後
|
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if( !hWnd )
{
return FALSE;
}
DWORD style;
style = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
LBS_DISABLENOSCROLL | LBS_EXTENDEDSEL | LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | LBS_NOTIFY;
RECT Rect;
GetClientRect( hWnd, (LPRECT)&Rect);
hListBox = CreateWindow( "listbox",
NULL, style, 0, 0, Rect.right-Rect.left, Rect.bottom-Rect.top, hWnd, NULL, hInst, NULL);
if ( !hListBox ) {
DestroyWindow( hWnd );
return FALSE;
}
| |
|
[グローバル変数に追加 ]
|
HWND hListBox;
char szFile[MAX_PATH+1];
| |
|
|
|
|
case WM_SIZE:
MoveWindow( hListBox, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
| |
|
|
|
1) メニユー作成
2) 宣言追加
|
LRESULT CALLBACK HttpGet( HWND, UINT, WPARAM, LPARAM );
| |
|
3) 呼び出し部分追加
|
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_HTTPGET:
DialogBox(hInst, (LPCTSTR)IDD_DIALOG1, hWnd, (DLGPROC)HttpGet);
break;
| |
|
4) Call Back 追加
|
LRESULT CALLBACK HttpGet( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if( LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL )
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
| |
|
|
|
1) ヘッダファイル追加
2) 宣言追加
|
void HttpGetProc( HWND );
| |
|
3) HttpGet を変更
|
Case WM_COMMAND:
if( LOWORD(wParam) == IDOK )
{
HttpGetProc( hDlg );
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
if( LOWORD(wParam) == IDCANCEL )
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
| |
|
4) HttpGetProc を作成
|
void
HttpGetProc( HWND hDlg )
{
SOCKET curSocket;
int ret;
char szEdit[4096+1];
char szBuff[4096+1];
// リストボックスクリア
SendMessage( hListBox, LB_RESETCONTENT, 0, 0L );
WORD wVersion;
int err;
WSADATA wsaData;
// API 初期化
wVersion = MAKEWORD( (BYTE)2, (BYTE)2 );
err = WSAStartup( wVersion, &wsaData );
if ( err != 0 ) {
lstrcpy( szBuff, "WSAStartup ERROR" );
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szBuff );
return;
}
curSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); // SOCKET 作成
if ( curSocket == INVALID_SOCKET ) {
lstrcpy( szBuff, "Socket can't create" );
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szBuff );
WSACleanup();
return;
}
GetDlgItemText( hDlg, IDC_EDIT1, szBuff, 4096 );
hostent *hosts;
hosts = gethostbyname( szBuff );
if ( hosts == NULL ) {
lstrcpy( szBuff, "gethostbyname ERROR" );
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szBuff );
shutdown( curSocket, 2 );
WSACleanup();
return;
}
SOCKADDR_IN Sad;
Sad.sin_family = AF_INET;
Sad.sin_port = htons( 80 );
Sad.sin_addr = *((LPIN_ADDR)*(hosts->h_addr_list));
ret = connect( curSocket, (LPSOCKADDR)&Sad, sizeof( SOCKADDR_IN ) ); // 接続
if ( ret == SOCKET_ERROR ) {
lstrcpy( szBuff, "connect ERROR" );
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szBuff );
shutdown( curSocket, 2 );
WSACleanup();
return;
}
GetDlgItemText( hDlg, IDC_EDIT2, szEdit, 4096 );
wsprintf( szBuff, "GET %s HTTP/1.0\n\n", szEdit );
ret = send( curSocket, szBuff, lstrlen( szBuff ), 0 );
int timeout = 60000; // タイムアウト値をミリ秒単位で指定
setsockopt( curSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
int i,out;
out = 0;
while( 1 ) {
ret = recv( curSocket, szBuff, sizeof( szBuff )-1, 0 );
if ( ret < 0 ) {
lstrcpy( szBuff, "recv ERROR" );
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szBuff );
shutdown( curSocket, 2 );
closesocket( curSocket );
WSACleanup();
return;
}
if ( ret == 0 ) {
lstrcpy( szBuff, "-----------------------------end" );
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szBuff );
break;
}
for( i = 0; i < ret; i++ ) {
if ( szBuff[i] == 0x0d || szBuff[i] == 0x0a ) {
szEdit[out] = 0x00;
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szEdit );
out = 0;
if ( szBuff[i] == 0x0d && szBuff[i+1] == 0x0a ) {
i++;
}
}
else {
if ( szBuff[i] == 0x09 ) {
szEdit[out] = 0x20;
out++;
szEdit[out] = 0x20;
out++;
szEdit[out] = 0x20;
out++;
szEdit[out] = 0x20;
}
else {
szEdit[out] = szBuff[i];
}
out++;
}
}
}
if ( out != 0 ) {
szEdit[out] = 0x00;
SendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)szEdit );
}
shutdown( curSocket, 2 );
closesocket( curSocket );
WSACleanup(); // API END
}
| |
|
|
|
1) アクティブな構成を Release に変更
2) プロジェクトの構成の「リンク」で、オブジェクト/ライブラリ モジュールに ws2_32.lib を追加
3) ビルド
|
|