WSAAsyncSelect函数的BCB问题
TForm1头文件
//---------------------------------------#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
//---------------------------------------#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();}//---------------------------------------
//---------------------------------------#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;}//---------------------------------------