WINSOCK


  Visual C++ 6.0 を起動してプロジェクトを作成

「プロジェクト名」 : WINSOCK

1) Win32 Application を指定。
2) 標準的な "Hello World!" アプリケーションを選択

  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];
  

  WndProc に WM_SIZE を追加

  

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) ヘッダファイル追加
  

#include <winsock2.h>
  

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) ビルド





yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[sc]
CCBot/2.0 (https://commoncrawl.org/faq/)
24/10/05 14:25:35
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube