| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 488 人关注过本帖
标题:求助WSAAsyncselect函数的应用
只看楼主 加入收藏
zanzan1986
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:100
专家分:140
注 册:2011-2-22
结帖率:66.67%
收藏
 问题点数:0 回复次数:1 
求助WSAAsyncselect函数的应用
// WSAAsynSelect.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "WSAAsynSelect.h"

#define MAX_LOADSTRING 100
#define WM_SOCKET WM_USER + 1
typedef struct DATA
{
    HWND hDlg;
    SOCKET  s;
}DATA;

// 全局变量:
HINSTANCE hInst;                                // 当前实例
TCHAR szTitle[MAX_LOADSTRING];                    // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), NULL, About);                  //这里是一个模态对话框
    return 0;//(int) msg.wParam;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    static SOCKET s;
    struct sockaddr_in addr;
    static WSADATA   wsd;
    SOCKET c_s;
    int i;
    char xie[50];

    switch (message)
    {
    case WM_INITDIALOG:
        if(!WSAStartup(MAKEWORD(2,2),&wsd))                              //加载winsocket 动态库
        {
            
            s = socket(AF_INET,SOCK_STREAM,0);                          //创建套节字
            if(s == SOCKET_ERROR)
            {
                 MessageBox(hDlg,TEXT("创建套接字失败!!!!"),TEXT("提示"),MB_OK);
                 WSACleanup();
                 EndDialog(hDlg, LOWORD(wParam));
            }
            addr.sin_addr.S_un.S_addr = inet_addr("192.168.0.100");             //取得IP地址
            addr.sin_family = AF_INET;                                          //TCP/IP模式
            addr.sin_port = htons(5150);                                        //设置端口号
            if(bind(s,(PSOCKADDR)&addr,sizeof(sockaddr_in)) == SOCKET_ERROR)    //邦定套节字
            {
                 MessageBox(hDlg,TEXT("邦定套接字失败!!!!"),TEXT("提示"),MB_OK);
                 WSACleanup();
                 EndDialog(hDlg, LOWORD(wParam));
            }

            //以下是在对话框的静态文本中显的IP和端口
            i = wsprintfA(xie,"%s",inet_ntoa(addr.sin_addr));            
            xie[i] = 0;
            SetWindowTextA(GetDlgItem(hDlg,IDC_IP),xie);

            i = wsprintfA(xie,"%d",ntohs(addr.sin_port));
            xie[i] = 0;
            SetWindowTextA(GetDlgItem(hDlg,IDC_PORT),xie);


            WSAAsyncSelect(s,hDlg,WM_SOCKET,FD_ACCEPT | FD_CLOSE);        将套节字加入异步事件中并发送消息到hDlg窗口中
            listen(s,40);                                                 //开始监听
 
        }
        else
        {
            MessageBox(hDlg,TEXT("加载winsocket动态库失败!!!!"),TEXT("提示"),MB_OK);
            WSACleanup();
            EndDialog(hDlg, LOWORD(wParam));
        }
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            WSAAsyncSelect(wParam,hDlg,0,0);
            closesocket(s);
            WSACleanup();
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;

    case WM_SOCKET:                                                             //网络事件
        if(WSAGETSELECTERROR(lParam))
        {
            MessageBox(hDlg,TEXT("存在网络错误"),TEXT("提示"),MB_OK);
            closesocket(wParam);
            return (INT_PTR)TRUE;
        }

        switch(WSAGETSELECTEVENT(lParam))
        {
        case FD_ACCEPT:                        //在这里就有错误了,一旦有客户机连接上,就会无限的接收到该消息而且c_s套节字都是些SOCKET_ERROR
            c_s = accept(wParam,(struct sockaddr*)&addr,&i);              //请问大侠们能否帮我找出是什么原因么,书上的代码都行不通!!!!
            if(c_s == SOCKET_ERROR) return (INT_PTR)TRUE;
            
            i = wsprintfA(xie,"客户机地址:%s",inet_ntoa(addr.sin_addr));
            xie[i] = 0;
            SendMessageA(GetDlgItem(hDlg,IDC_LIST1),LB_ADDSTRING,0,(LPARAM)xie);
            WSAAsyncSelect(c_s,hDlg,WM_SOCKET,FD_READ | FD_WRITE | FD_CLOSE);
            closesocket(c_s);
            
            break;
        }
        return (INT_PTR)TRUE;
    }
    return (INT_PTR)FALSE;
}
搜索更多相关主题的帖子: 标题栏 include 应用程序 
2012-10-11 21:56
zanzan1986
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:100
专家分:140
注 册:2011-2-22
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

这是程序的界面,有客户机连接后就会有无限的WM_SOCKETI消息接收!!!!!!!!!!!!!!!!!!!!!!
2012-10-11 22:00
快速回复:求助WSAAsyncselect函数的应用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018528 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved