어디에 문제가 있는지 잘 모르겠습니다. 따라서 기본 인디 (Indy) 서버 및 클라이언트 응용 프로그램을 만들기위한 전체 예제를 게시하고 있습니다.
먼저 서버 응용 프로그램에 IdTCPServer
구성 요소와 버튼이 있습니다. 관련 속성은 다음과 같습니다
object Button1: TButton
Text = 'Listen'
OnClick = Button1Click
end
object IdTCPServer1: TIdTCPServer
DefaultPort = 1234
OnExecute = IdTCPServer1Execute
end
및 서버의 IdTCPServer.OnExecute
및 Button.OnClick
방법은 다음과 같습니다
procedure TServerForm.Button1Click(Sender: TObject);
begin
IdTCPServer1.Active := not IdTCPServer1.Active;
if IdTCPServer1.Active then
Button1.Text := 'Close'
else
Button1.Text := 'Listen';
end;
procedure TServerForm.IdTCPServer1Execute(AContext: TIdContext);
var
Num: Integer;
begin
while (IdTCPServer1.Active) and (AContext.Connection.Connected) do
begin
Num := Random(MaxInt);
AContext.Connection.IOHandler.WriteLn(IntToStr(Num));
Sleep(1000);
end;
end;
당신이 볼 수 있듯이, 연결된 각 클라이언트에 대해, 우리는 루프를 입력 할 경우 각 초 의지 임의의 숫자 (문자열)를 소켓에 씁니다.
내가 서버를 실행하고 음악을 감상 할 수있는 버튼을 누르면, 열려있는 포트를 허용하도록 경고 방화벽을 받아들이고 나서 성공적으로 연결하고 텔넷을 통해이 서버에서 정보를 얻을 : 이제
클라이언트 응용 프로그램을 만들었습니다.
object Button1: TButton
Text = 'Connect'
OnClick = Button1Click
end
object Memo1: TMemo
end
object IdTCPClient1: TIdTCPClient
Host = 'localhost'
Port = 1234
end
과 코드는 다음과 같습니다 : 양식에
버튼, 메모 및 IdTCPClient, 관련 속성은
: 런타임시
procedure TClientForm.ReadResults;
var
S: string;
begin
while IdTCPClient1.Connected do
begin
S := IdTCPClient1.IOHandler.ReadLn;
Memo1.Lines.Add(S);
//don't repeat this approach in production code, it's just a test here
Application.ProcessMessages;
end;
end;
procedure TClientForm.Button1Click(Sender: TObject);
begin
if IdTCPClient1.Connected then
begin
IdTCPClient1.Disconnect;
Button1.Text := 'Connect';
end
else
begin
IdTCPClient1.Connect;
Button1.Text := 'Disconnect';
Button1.Repaint;
ReadResults;
end;
end;
그것은 다음과 같습니다
프로젝트는 Delphi X가 포함 된 FireMonkey로 제작되었습니다. E3이지만, VCL에서도 Indy 10을 지원하는 Delphi 버전에서 작동해야합니다.
우선,'ReadLn' 함수는 리턴하기 전에 개행을 기다릴 때까지 기다립니다. 일반 개행이나 캐리지 리턴 및 개행 조합은 잘 모르겠습니다. 또한이 숫자를 바이너리 데이터 또는 텍스트 문자열로 어떻게 보내나요? –
서버 응용 프로그램은 50 초 후에 정수 값만 전송합니다 (4 바이트 큰 블록에서 전보가 정수 값으로 전송 됨). 그게 내가 아는 전부 야. ReadLn 대신 무엇을 사용해야합니까? –
저는 집에 델파이가 없지만'ReadLn'이라는 함수는 바이너리 데이터가 아닌 텍스트 데이터를 기대한다고 생각합니다.임의의 데이터에 대해 사용해야하는'Read' 또는'Receive' 또는 유사한 함수가 있어야합니다. –