Друзья! Помогите!
Путем недолгих рассуждений понял, что мне будут вполне приятны компоненты Indy. А вот понять как правильно сделать я никак не могу.
Как мог, сделал клиент-серверную связь на Indy: Клиент:
| Код | function TForm1.TCP_Connect(msg: string): boolean; begin result:=false;
idTcp.Host:=edtHost.Text; idTCP.Port:=StrToInt(edtPort.Text);
try idTCP.Connect; finally try idTCP.GetResponse(200);
idTCP.SendCmd('Preved '+ msg,201); idTCP.IOHandler.Capture(memo1.Lines);
idTCP.SendCmd('Quit', 202); result:=true; finally idTCP.Disconnect; end; end; end;
procedure TForm1.Button1Click(Sender: TObject); begin if not TCP_Connect('mycommand') then ShowMessage('Bad connection!'); end;
|
Сервер:
| Код | procedure TfrmMain.btnConnectClick(Sender: TObject); begin tcpServer.DefaultPort:=StrToInt(edtPort.Text); tcpServer.Active:=True; end;
procedure TfrmMain.tcpServerConnect(AContext: TIdContext); begin AContext.Connection.IOHandler.WriteLn('200 My Server ready.'); end;
procedure TfrmMain.tcpServerExecute(AContext: TIdContext); var LCmd: string; LLine: string; begin try LLine := AContext.Connection.IOHandler.ReadLn; LCmd := Fetch(LLine);
if AnsiSameText(LCmd, 'QUIT') then begin AContext.Connection.IOHandler.WriteLn('202-Bye!'); AContext.Connection.Disconnect; end else if AnsiSameText(LCmd, 'PREVED') then begin AContext.Connection.IOHandler.WriteLn('Medved.'); Memo1.Lines.Insert(0,LLine); end else begin AContext.Connection.IOHandler.WriteLn('400 Unknown Command'); end; finally
end; end;
|
В настоящее время существует две проблемы: - Клиент подвисает при работе с сетью. Антифриз стоит. - Функция подключения в клиенте не может отловить отсутствие подключения |