access Violation 에러가 발생합니다. 발생하는 곳은
m_NewEvent := WSACreateEvent;
에서 나옵니다. 아무리 봐도... 맞는것 같은데요.....
아래 소스 첨부했습니다. 00000000 번지를 못
읽으니... 뭔가 생성이 안된것 같은데... 흐미...
<!--CodeS-->
unit pas_Socket;
interface
uses IdWinSock2, Windows;
type
TAsyncSocket = class
public
constructor Create(Host : string; Port : Word);
procedure Listen;
procedure Close;
private
m_WSADATA : TWSADATA;
m_hServSock : TSOCKET;
m_ServAddr : SOCKADDR_IN;
m_hSockArray : array [0..wsa_maximum_wait_events - 1] of TSOCKET;
m_hClntSock : TSOCKET;
m_ClntLen : integer;
m_ClntAddr : SOCKADDR_IN;
m_hEventArray : array [0..wsa_maximum_wait_events - 1] of WSAEVENT;
m_NewEvent : WSAEVENT;
m_NetEvents : TWSANETWORKEVENTS;
m_SockTotal : integer;
m_Index : integer;
procedure Bind;
end;
implementation
{ TAsyncSocket }
procedure TAsyncSocket.Bind;
begin
if IdWinSock2.bind(m_hServSock, @m_ServAddr, SizeOf(m_ServAddr)) = SOCKET_ERROR then
begin
Assert(True, 'Bind Error');
exit;
end;
end;
procedure TAsyncSocket.Close;
var i : integer;
begin
for i := 0 to m_SockTotal - 1 do
begin
WSACloseEvent(m_hEventArray[i]);
CloseSocket(m_hSockArray[i]);
end;
end;
constructor TAsyncSocket.Create(Host : string; Port : Word) ;
begin
m_SockTotal := 0;
if (WSAStartup(MAKEWORD(2, 2), m_WsaData) <> 0) then
begin
Assert(True, 'WSAStartUp Error');
exit;
end;
m_hServSock := Socket(PF_INET, SOCK_STREAM, 0);
if (m_hServSock = INVALID_SOCKET) then
begin
Assert(True, 'Socket Error');
exit;
end;
m_ServAddr.sin_family := AF_INET;
m_ServAddr.sin_addr.S_addr := Inet_Addr(pChar(Host));
m_ServAddr.sin_port := HtoNs(Port);
Bind;
m_NewEvent := WSACreateEvent;
if (WSAEventSelect(m_hServSock, m_NewEvent, FD_ACCEPT) = SOCKET_ERROR) then
begin
Assert(True, 'WSACeateEvent Error');
exit;
end;
end;
procedure TAsyncSocket.Listen;
begin
if (IdWinSock2.listen(m_hServSock, 5) = SOCKET_ERROR) then
begin
Assert(True, 'Listen Error');
exit;
end;
m_hSockArray[m_SockTotal] := m_hServSock;
m_hEventArray[m_SockTotal] := m_NewEvent;
Inc(m_SockTotal);
end;
end.
<!--CodeE-->
코드는 딱히 에러날건 없는거 같은데... 에러가 난다면....
Create 함수 때문이예요..
Create 는 아직 클래스가 생성되기 전에 발생해요..
따라서 아직 채 생성되지 않은 클래스에서 메세지나 이벤트를 주고 받을수 없어요..
무슨말인지 알겠죠?