首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

WSAAsyncSelect函数的BCB有关问题

2012-02-12 
WSAAsyncSelect函数的BCB问题TForm1头文件C/C++ code//---------------------------------------#ifndef s

WSAAsyncSelect函数的BCB问题
TForm1头文件

C/C++ code
//---------------------------------------#ifndef serverH#define serverH//---------------------------------------#include <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>//---------------------------------------class TForm1 : public TForm{__published:    // IDE-managed Components        TButton *Button1;        void __fastcall FormCreate(TObject *Sender);        void __fastcall Button1Click(TObject *Sender);private:    // User declarationspublic:        // User declarations        __fastcall TForm1(TComponent* Owner);        void Onrecvmessage(WPARAM wParam, LPARAM lParam);        void Init();};//---------------------------------------extern PACKAGE TForm1 *Form1;//---------------------------------------#endif

TForm1源代码
C/C++ code
//---------------------------------------#include <vcl.h>#pragma hdrstop#include <Winsock2.h>#include "server.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"#define WM_SOCKET WM_USER+1TForm1 *Form1;SOCKET serv;struct sockaddr_in servaddr,cliaddr;WSADATA wsaData;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)        : TForm(Owner){}//---------------------------------------void __fastcall TForm1::FormCreate(TObject *Sender){        this->Init();}//---------------------------------------void TForm1::Init(){        int err = WSAStartup(MAKEWORD(2,2),&wsaData);    if(err !=0){                Application->MessageBoxA("初始化winsock失败","测试",MB_OK);                return;    }    if((serv = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1){                Application->MessageBoxA("初始化winsock失败","测试",MB_OK);        return;    }    /* 初始化servaddr */    memset(&servaddr,0,sizeof(servaddr));    servaddr.sin_family = AF_INET;    servaddr.sin_port = htons(20000);    servaddr.sin_addr.s_addr = htons(INADDR_ANY);    if (bind(serv,(struct sockaddr*)&servaddr,sizeof(servaddr))<0){        Application->MessageBoxA("无法绑定端口","测试",MB_OK);        return;    }        if(SOCKET_ERROR == WSAAsyncSelect(serv, this->Handle, WM_SOCKET, FD_ACCEPT|FD_CLOSE))        {                Application->MessageBoxA("异步模式失败","测试",MB_OK);                closesocket(serv);                WSACleanup();                return;        }    if (listen(serv,255) < 0){        Application->MessageBoxA("调用监听失败","测试",MB_OK);        return;    }}void TForm1::Onrecvmessage(WPARAM wParam, LPARAM lParam){    if(WSAGETSELECTERROR(lParam))     {        Application->MessageBoxA("接受消息错误","测试",MB_OK);         return;     }         switch(WSAGETSELECTEVENT(lParam))    {        case FD_ACCEPT:                        Application->MessageBoxA("接受连接","测试",MB_OK);            //acceptclient(wParam, m_hWnd);            break;                case FD_WRITE:                        Application->MessageBoxA("读数据","测试",MB_OK);                        break;                case FD_READ:                        Application->MessageBoxA("发数据","测试",MB_OK);                        break;                case FD_CLOSE:                        closesocket(wParam);                        break;                default:                        break;        }        return;} void __fastcall TForm1::Button1Click(TObject *Sender){        this->Close();}//--------------------------------------- 



主程序代码
C/C++ code
//---------------------------------------#include <vcl.h>#pragma hdrstop//---------------------------------------USEFORM("server.cpp", Form1);//---------------------------------------WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int){        try        {                 Application->Initialize();                 Application->CreateForm(__classid(TForm1), &Form1);                 Application->Run();        }        catch (Exception &exception)        {                 Application->ShowException(&exception);        }        catch (...)        {                 try                 {                         throw Exception("");                 }                 catch (Exception &exception)                 {                         Application->ShowException(&exception);                 }        }        return 0;}//---------------------------------------


我按照网上的说法把WSAAsyncSelect模型建了这段程序代码,主要是在有客户端连接的时候用MessageBox来提示有连接到,貌似很简单,但不知道为什么这段代码没有反应,是不是我的做法有问题?应该怎么修改才行?

[解决办法]
没加消息映射。收到消息了也不会去调Onrecvmessage

在.h里加上
void __fastcall OnSocket(TMessage& Message);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_SOCKET,TMessage,OnSocket);
END_MESSAGE_MAP(TForm);

.cpp自己把OnSocket函数实现下 类似你的Onrecvmessage 只是参数变成了一个TMessage结构

热点排行