procedure TServer.rSendMessage(pIdx: Integer; pMsg: string); var I: Integer; begin if pMsg <> '' then begin if pIdx = -1 then begin for I := 0 to FServer.Socket.ActiveConnections - 1 do begin Application.ProcessMessages; // If ActiveConnections so large... FServer.Socket.Connections[I].SendText(#0 + pMsg + #0 + 'END'); end; end else begin if pIdx <= FServer.Socket.ActiveConnections - 1 then begin FServer.Socket.Connections[pIdx].SendText(#0 + pMsg + #0 + 'END'); end; end; end; end;
procedure TServer.rSendMessage(pData: Pointer; pMsg: string); begin rSendMessage(FConIds.IndexOf(pData), pMsg); end;
procedure TServer.rOnBefRead(Sender: TObject; Socket: TCustomWinSocket); var lvAllRec: Boolean; lvLength: Integer; lvRecive: string; lvAllMsg: string; I: Integer; begin lvRecive := Socket.ReceiveText; lvLength := Length(lvRecive); lvAllRec := Copy(lvRecive, lvLength - 2, 3) = 'END'; if lvAllRec then begin lvAllMsg := FLastMsg + Copy(lvRecive, 1, lvLength - 3); rSeparateMsg(FMsgLst, lvAllMsg); for I := 0 to FMsgLst.Count - 1 do onExecute(FMsgLst.Strings[I], Socket.Data); end else begin FLastMsg := FLastMsg + lvRecive; end; end;
procedure TServer.rOnBefLogin(Sender: TObject; Socket: TCustomWinSocket); var lvP: ^integer; begin New(lvP); Socket.Data := lvP; FConIds.Add(Socket.Data); onLogin(Socket); end;
procedure TServer.rOnBefLogout(Sender: TObject; Socket: TCustomWinSocket); begin onLogout(Socket); FConIds.Remove(Socket.Data); Dispose(Socket.Data); end;
procedure rSeparateMsg(var pLst: TStrings; pMsg: string); var I: Integer; lvTmp: String; begin lvTmp := ''; I := 1; pLst.Clear; while (I <= Length(pMsg)) do begin if pMsg[I] = #0 then begin Inc(I); while ((pMsg[I] <> #0) and (I <= Length(pMsg))) do begin lvTmp := lvTmp + pMsg[I]; Inc(I); end; end; Inc(I); if lvTmp <> '' then pLst.Add(lvTmp); lvTmp := ''; end; end;
procedure TServer.rOnBefError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); var lvEt: TErrorType; begin case ErrorCode of 10060: lvEt := etTimeOut; 10061: lvEt := etRefused; 10065: lvEt := etNoHost; 10064: lvEt := etHostDown; 10040: lvEt := etTooLong; else lvEt := etUnknow; end; onError(Socket, lvEt, ErrorCode); end;
procedure TServer.rChangeActive(pValor: Boolean); begin if not pValor then begin FLastMsg := ''; FMsgLst.Clear; FConIds.Clear; FServer.Close; end else begin if not FServer.Active then FServer.Open; end; end;
destructor TServer.Destroy(); begin FMsgLst.Free; FConIds.Free; end; //------------------------------------------------------------------------------ constructor TClient.Create(pIp: string; pPort: Integer; const pActive: Boolean = False); begin FLastMsg := ''; FMsgLst := TStringList .Create; FClient := TClientSocket.Create(nil); FClient.Port := pPort; if (LowerCase(pIp) = 'localhost') or (Trim(pIp) = '') then FClient.Address := '127.0.0.1' else FClient.Address := pIp; FClient.OnRead := rOnBefRead; FClient.OnError := rOnBefError; if pActive then if not FClient.Active then FClient.Open; end;
procedure TClient.rSendMessage(pMsg: string); begin if pMsg <> '' then if FClient.Socket.Connected then FClient.Socket.SendText(#0 + pMsg + #0+'END'); end;
procedure TClient.rOnBefRead(Sender: TObject; Socket: TCustomWinSocket); var lvAllRec: Boolean; lvLength: Integer; lvRecive: string; lvAllMsg: string; I: Integer; begin lvRecive := Socket.ReceiveText; lvLength := Length(lvRecive); lvAllRec := Copy(lvRecive, lvLength - 2, 3) = 'END'; if lvAllRec then begin lvAllMsg := FLastMsg + Copy(lvRecive, 1, lvLength - 3); rSeparateMsg(FMsgLst, lvAllMsg); for I := 0 to FMsgLst.Count - 1 do onRead(Socket.Data, FMsgLst.Strings[I]); end else begin FLastMsg := FLastMsg + lvRecive; end; end;
procedure TClient.rOnBefError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); var lvEt: TErrorType; begin case ErrorCode of 10060: lvEt := etTimeOut; 10061: lvEt := etRefused; 10065: lvEt := etNoHost; 10064: lvEt := etHostDown; 10040: lvEt := etTooLong; else lvEt := etUnknow; end; onError(Socket,lvEt, ErrorCode); ErrorCode := ErrorCode; end;
procedure TClient.rOnBefCon(Sender: TObject; Socket: TCustomWinSocket); begin onConnect(Socket); end;
destructor TClient.Destroy(); begin FClient.Close; FMsgLst.Free; end;
Pergunta
Rento
Após eu passar por dores de cabeças com o TServerSocket e o TClientSocket, fiz está classes.
Ela corrige os principais erros que enfrentei e mais alguns que podem estar acontecendo por ai:
- MensagensComProblamaDeJuntaTudoAndBugarTudo
- Mensagens grandes 100%
- Address com "localhost" auto para 127.0.0.1
Junto com o arquivo unSocket.pas vem um projeto de exemplo.Você pode modificar quaisquer arquivo, mantendo por favor o cabeçalho.
Link de tudo:
http://www.myfileupload.net/download.php?f...58d91c2146ebf79
unSocket.pas:
Editado por RentoLink para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.