//启动
post:=StrtoInt(Listen_Port);
TimeServerSocket.Enabled :=True;
//Main.pas
procedure TMain.TimeServerSocketTimer(Sender: TObject);
var
ServerSocket, sockfd: TSocket;
wd: WSAData;
addr: TSockAddr;
wsstatus, rb, rl: integer;
SSocket: TSocket;
nsize: integer ;
logIp: string;
LocalRead, LocalWrite, Localexcept: TFDSet;
VTime: TTimeVal;
//RecvLen, SendLen: integer;
Hanlt,i:integer;
// Str:String;
Main: ListerSockets;
begin
TimeServerSocket.Enabled:=false;
wsstatus := WSAStartup(2, wd);//初始化winsock;
if wsstatus <> 0 then
begin
closesocket(ServerSocket);
TimeServerSocket.Enabled:=True;
exit;
end;
ServerSocket := Socket(AF_INET, SOCK_STREAM, 0);//建立服务器的socket
try
if not (ServerSocket > 0) then
begin
closesocket(ServerSocket);
TimeServerSocket.Enabled:=True;
// ShowMessage('服务器建立失败');
exit;
end;
addr.sin_family := AF_INET;
addr.sin_port := htons(post);
addr.sin_addr.S_addr := htonl(INADDR_ANY);
nsize := sizeof(addr );
rb := bind(ServerSocket, addr, nsize);//绑定,0表示成功
if rb <> 0 then
begin
closesocket(ServerSocket);
TimeServerSocket.Enabled:=True;
// ShowMessage('服务器绑定失败');
exit;
end;
rl := listen(ServerSocket, 100);//监听,0表示成功
if rl=0 then
begin
Main:=ListerSockets.Create(ServerSocket,DBConn);
end;
except
on E:exception do
begin
CloseSocket(ServerSocket);
TimeServerSocket.Enabled:=True;
end;
end;
WSACleanup;
end;
//ListerSocket.pas
procedure ListerSockets.Main;
var
addrc: PSockAddr;
addrclen: PInteger;
sockfd: TSocket;
LocalRead, LocalWrite, Localexcept: TFDSet;
Main:AcceptSocket;
begin
Try
begin
new(addrc);
new(addrclen);
addrclen^ := sizeof(addrc^);
while (true) do
begin
sockfd := accept(ServerSockets, addrc, addrclen);//等待接收
//logIp := inet_ntoa(addrc^.sin_addr); //Ip Addr Transport
if sockfd = INVALID_SOCKET then
begin
closesocket(sockfd);
exit;
end
else
begin
Main:=AcceptSocket.Create(sockfd,thedbcon);
end;
end;
end
except
on E:Exception do
begin
closesocket(sockfd);
exit;
end;
end;
end;