如何在窗口显示后检查套接字连接?

时间:2014-08-14 23:45:37

标签: c++ sockets winapi

我在win32套接字编程上遵循了关于youtube的教程。在我尝试实现win32 winapi应用程序之前它运行良好,现在当我点击Debug(运行应用程序)时,没有应用程序运行。

#include "Resource.h"
#include <tchar.h>
#include <Windows.h>
#pragma comment(lib, "Ws2_32.lib")

void AppendText(const HWND &hwnd, TCHAR *newText) {
    HWND hwndOutput = GetDlgItem(hwnd, IDC_OUTPUT);
    int outLength = GetWindowTextLength(hwndOutput) + lstrlen(newText) + 1;
    TCHAR * buf = (TCHAR *)GlobalAlloc(GPTR, outLength * sizeof(TCHAR));
    if (!buf) return;
    GetWindowText(hwndOutput, buf, outLength);
    _tcscat_s(buf, outLength, newText);
    SetWindowText(hwndOutput, buf);
    GlobalFree(buf);
}

LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_CREATE:
    {
                      HWND hServEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 10, 10, 370, 150, hwnd, (HMENU)MAKEINTRESOURCE(IDC_OUTPUT), NULL, NULL);

                      WSADATA wsa;
                      WORD Version = MAKEWORD(2, 1);
                      WSAStartup(Version, &wsa);

                      SOCKET Listen = socket(AF_INET, SOCK_STREAM, NULL);
                      SOCKET Connect = socket(AF_INET, SOCK_STREAM, NULL);
                      SOCKADDR_IN Server;

                      Server.sin_addr.s_addr = inet_addr("127.0.0.1");
                      Server.sin_family = AF_INET;
                      Server.sin_port = htons(100);

                      bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
                      listen(Listen, 1);

                      int size = sizeof(Server);
                      AppendText(hwnd, "Listening..\r\n");

                      for (;;) {
                          if (Connect = accept(Listen, (SOCKADDR*)&Server, &size)) {
                              AppendText(hwnd, "Connection was reached\r\n");
                              break;
                          }
                      }
                      WSACleanup();
                      return 0;
    }
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) {
    WNDCLASSEX wc;
    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = MsgProc;
    wc.lpszClassName = "Main_Window";
    wc.lpszMenuName = NULL;
    wc.style = 0;

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Failed to register window class.", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }

    HWND hwnd = CreateWindowEx(0, "Main_Window", "Server", WS_OVERLAPPEDWINDOW, 200, 100, 400, 200, NULL, NULL, hInstance, NULL);

    if (!hwnd) {
        MessageBox(NULL, "Failed to create window.", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

    MSG msg;
    ZeroMemory(&msg, sizeof(msg));

    while (msg.message != WM_QUIT) {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return 0;
}

编辑控件创建下面的WM_CREATE案例中的代码全部是从我观看的教程中复制和粘贴的,并且可以正常工作。但是,当我实施其他所有内容时,它无效。我正在使用MS Visual Studio 2013 Express。有谁知道为什么这不起作用?

0 个答案:

没有答案