わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

UDPサーバ

UDPサーバは、ソケットを生成してbindで待ち受けるだけです。 送信元の情報は、recvfromで受信する事で取得する事ができます。

UDPサーバ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <tchar.h>
#include <locale.h>
#include <string>
#include <winsock2.h>
#include <windows.h>
#include <wingdi.h>
 
 
 
 
// ライブラリ
#pragma comment( lib, "ws2_32.lib" )
 
 
 
// WinSockからのイベント
#define WM_WINSOCKEVENT ( WM_USER + 100 )
 
 
 
// グローバル
SOCKET oSocket;
 
 
 
/*
    メインウインドウイベント処理
*/
LRESULT CALLBACK eMainWindowProc
(
      HWND   hWnd   // handle to window
    , UINT   uMsg   // message identifier
    , WPARAM wParam // first message parameter
    , LPARAM lParam // second message parameter
)
{
    switch( uMsg ) {
    case WM_CREATE:
    //--------------------------------------------
    // WM_CREATE
    //--------------------------------------------
        {
            CREATESTRUCT* tpCreateSt = (CREATESTRUCT*)lParam;
        }
        break;
 
 
    case WM_WINSOCKEVENT:
    //--------------------------------------------
    // WM_WINSOCKEVENT
    //--------------------------------------------
        {
            switch( WSAGETSELECTEVENT( lParam ) ) {
            case FD_READ:
                {
                    struct sockaddr_in oFromAddr;
                    int sockaddr_in_size = sizeof( struct sockaddr_in );
 
                    CHAR szData[ 256 ];
 
                    // 受信
                    ::recvfrom(
                              oSocket
                            , (char*)szData
                            , sizeof( szData ) - 1
                            , 0
                            , (struct sockaddr*)&oFromAddr
                            , &sockaddr_in_size
                        );
 
                    // 受信元IPと内容を表示
                    printf( "%s, %s¥n", inet_ntoa( oFromAddr.sin_addr ), szData );
                }
                break;
            }
        }
        break;
 
 
    case WM_DESTROY:
    //--------------------------------------------
    // WM_DESTROY
    //--------------------------------------------
        {
            // 終了する( 引数はそのまま終了コードとなります )
            ::PostQuitMessage( 0 );
        }
        break;
    }
 
    // デフォルト処理呼び出し
    return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}
 
 
 
/*
    UDPサーバ
*/
int _tmain
(
      int argc
    , _TCHAR* argv[]
)
{
    // 標準出力にユニコードを表示できるようにする
    setlocale( LC_ALL, "Japanese" );
 
    // WinSockのスタートアップ
    WSAData tWsaData;
    WSAStartup( MAKEWORD(2,0), &tWsaData );
 
    WNDCLASSEX tWndClass;
    HINSTANCE  hInstance;
    TCHAR*     cpClassName;
    TCHAR*     cpWindowName;
    HWND       hWnd;
 
 
    // アプリケーションインスタンス
    hInstance    = ::GetModuleHandle( NULL );
 
    // クラス名称
    cpClassName  = _T("MainWindowClass");
 
    // ウインドウ名称
    cpWindowName = _T("UDPサーバ");
 
    // ウインドウクラスパラメータセット
    tWndClass.cbSize        = sizeof( WNDCLASSEX );
    tWndClass.style         = 0;
    tWndClass.lpfnWndProc   = eMainWindowProc;
    tWndClass.cbClsExtra    = 0;    // ::GetClassLong で取得可能なメモリ
    tWndClass.cbWndExtra    = 0;    // ::GetWindowLong で取得可能なメモリ
    tWndClass.hInstance     = hInstance;
    tWndClass.hIcon         = NULL;
    tWndClass.hCursor       = NULL;
    tWndClass.hbrBackground = NULL;
    tWndClass.lpszMenuName  = NULL;
    tWndClass.lpszClassName = cpClassName;
    tWndClass.hIconSm       = NULL;
 
    // ウインドウクラス生成
    if ( 0 == ::RegisterClassEx( &tWndClass ) ) {
 
        /* 失敗 */
        return( -1 );
    }
 
    // ウインドウを生成する(メッセージ専用)
    hWnd = ::CreateWindowEx (
          0                       // extended window style
        , tWndClass.lpszClassName // pointer to registered class name
        , cpWindowName            // pointer to window name
        , WS_OVERLAPPEDWINDOW     // window style
        , CW_USEDEFAULT           // horizontal position of window
        , CW_USEDEFAULT           // vertical position of window
        , 640                     // window width
        , 480                     // window height
        , HWND_MESSAGE            // handle to parent or owner window
        , NULL                    // handle to menu, or child-window identifier
        , hInstance               // handle to application instance
        , (VOID*)0x12345678       // pointer to window-creation data
    );
 
 
    // ソケットの生成
    oSocket = socket( AF_INET, SOCK_DGRAM, 0 );
 
    // ソケットを非同期にする
    WSAAsyncSelect( oSocket, hWnd, WM_WINSOCKEVENT, FD_READ | FD_CLOSE );
 
    struct sockaddr_in oSockAddr;
    oSockAddr.sin_family = AF_INET;
    oSockAddr.sin_port   = htons( 12345 );
    oSockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
 
    // バインド
    ::bind( oSocket, (struct sockaddr*)&oSockAddr, sizeof( oSockAddr ) );
 
    // メッセージループ
    ::MessageBox( NULL, L"受信待ち", L"受信待ち", MB_OK );
 
    // ソケットのクローズ
    ::closesocket( oSocket );
 
    // WinSockのクリーンナップ
    ::WSACleanup();
 
    // 正常終了
    return( 0 );
}

実行結果







わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。