Arkadaşlar merhaba,
.Net te çalıştırdığım bu alttaki kodun Delphi de karşılığını yazamadım, yardımcı olabilecek arkadaş var mı?
string messageBoxText;
try
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("192.168.1.145", 2222);
Console.WriteLine("Başarılı");
NetworkStream stream = tcpClient.GetStream();
byte[] bytes = Encoding.UTF8.GetBytes("{\"id\":\"getinfo\"}\r\n");
stream.Write(bytes, 0, bytes.Length);
byte[] numArray = new byte[102400];
int count = stream.Read(numArray, 0, numArray.Length);
string str = Encoding.UTF8.GetString(numArray, 0, count);
while (numArray[count - 1] != (byte)10)
{
count = stream.Read(numArray, 0, numArray.Length);
str += Encoding.UTF8.GetString(numArray, 0, count);
}
MessageBox.Show(str);
messageBoxText = str;
Console.WriteLine("Gelen Veri:{0}", (object)str);
stream.Close();
tcpClient.Close();
}
catch (Exception ex)
{
Console.WriteLine("Bağlantı Hatası:{0}", (object)ex.Message);
}
(25-07-2024, Saat: 20:06)nevzatc Adlı Kullanıcıdan Alıntı: [ -> ]Arkadaşlar merhaba,
.Net te çalıştırdığım bu alttaki kodun Delphi de karşılığını yazamadım, yardımcı olabilecek arkadaş var mı?
string messageBoxText;
try
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("192.168.1.145", 2222);
Console.WriteLine("Başarılı");
NetworkStream stream = tcpClient.GetStream();
byte[] bytes = Encoding.UTF8.GetBytes("{\"id\":\"getinfo\"}\r\n");
stream.Write(bytes, 0, bytes.Length);
byte[] numArray = new byte[102400];
int count = stream.Read(numArray, 0, numArray.Length);
string str = Encoding.UTF8.GetString(numArray, 0, count);
while (numArray[count - 1] != (byte)10)
{
count = stream.Read(numArray, 0, numArray.Length);
str += Encoding.UTF8.GetString(numArray, 0, count);
}
MessageBox.Show(str);
messageBoxText = str;
Console.WriteLine("Gelen Veri:{0}", (object)str);
stream.Close();
tcpClient.Close();
}
catch (Exception ex)
{
Console.WriteLine("Bağlantı Hatası:{0}", (object)ex.Message);
}
Böyle durumlarda chatgpt den yardım alıyorum. Aşağıdaki kodu üretti. Olası hataları ve algoritmayı kendinize göre düzenleyebilirsiniz. maalesef her zaman %100 sonuç alamasamda bana yol gösteriyor.
kaynak :
https://chatgpt.com/
Kod:
program ConsoleTCPClient;
{$APPTYPE CONSOLE}
uses
IdTCPClient, IdGlobal, SysUtils, Classes;
var
tcpClient: TIdTCPClient;
bytes: TIdBytes;
numArray: TIdBytes;
count: Integer;
str: string;
messageBoxText: string;
begin
try
tcpClient := TIdTCPClient.Create(nil);
try
tcpClient.Host := '192.168.1.145';
tcpClient.Port := 2222;
tcpClient.Connect;
Writeln('Başarılı');
bytes := ToBytes('{"id":"getinfo"}' + #13#10, IndyTextEncoding_UTF8);
tcpClient.IOHandler.Write(bytes);
SetLength(numArray, 102400);
count := tcpClient.IOHandler.ReadBytes(numArray, 102400);
str := BytesToString(numArray, 0, count, IndyTextEncoding_UTF8);
while (count > 0) and (numArray[count - 1] <> 10) do
begin
count := tcpClient.IOHandler.ReadBytes(numArray, 102400);
str := str + BytesToString(numArray, 0, count, IndyTextEncoding_UTF8);
end;
messageBoxText := str;
Writeln('Gelen Veri:' + str);
except
on E: Exception do
begin
Writeln('Bağlantı Hatası:' + E.Message);
end;
end;
finally
tcpClient.Free;
end;
end.
(25-07-2024, Saat: 21:14)SercanTEK Adlı Kullanıcıdan Alıntı: [ -> ] (25-07-2024, Saat: 20:06)nevzatc Adlı Kullanıcıdan Alıntı: [ -> ]Arkadaşlar merhaba,
.Net te çalıştırdığım bu alttaki kodun Delphi de karşılığını yazamadım, yardımcı olabilecek arkadaş var mı?
string messageBoxText;
try
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("192.168.1.145", 2222);
Console.WriteLine("Başarılı");
NetworkStream stream = tcpClient.GetStream();
byte[] bytes = Encoding.UTF8.GetBytes("{\"id\":\"getinfo\"}\r\n");
stream.Write(bytes, 0, bytes.Length);
byte[] numArray = new byte[102400];
int count = stream.Read(numArray, 0, numArray.Length);
string str = Encoding.UTF8.GetString(numArray, 0, count);
while (numArray[count - 1] != (byte)10)
{
count = stream.Read(numArray, 0, numArray.Length);
str += Encoding.UTF8.GetString(numArray, 0, count);
}
MessageBox.Show(str);
messageBoxText = str;
Console.WriteLine("Gelen Veri:{0}", (object)str);
stream.Close();
tcpClient.Close();
}
catch (Exception ex)
{
Console.WriteLine("Bağlantı Hatası:{0}", (object)ex.Message);
}
Böyle durumlarda chatgpt den yardım alıyorum. Aşağıdaki kodu üretti. Olası hataları ve algoritmayı kendinize göre düzenleyebilirsiniz. maalesef her zaman %100 sonuç alamasamda bana yol gösteriyor.
kaynak : https://chatgpt.com/
Kod:
program ConsoleTCPClient;
{$APPTYPE CONSOLE}
uses
IdTCPClient, IdGlobal, SysUtils, Classes;
var
tcpClient: TIdTCPClient;
bytes: TIdBytes;
numArray: TIdBytes;
count: Integer;
str: string;
messageBoxText: string;
begin
try
tcpClient := TIdTCPClient.Create(nil);
try
tcpClient.Host := '192.168.1.145';
tcpClient.Port := 2222;
tcpClient.Connect;
Writeln('Başarılı');
bytes := ToBytes('{"id":"getinfo"}' + #13#10, IndyTextEncoding_UTF8);
tcpClient.IOHandler.Write(bytes);
SetLength(numArray, 102400);
count := tcpClient.IOHandler.ReadBytes(numArray, 102400);
str := BytesToString(numArray, 0, count, IndyTextEncoding_UTF8);
while (count > 0) and (numArray[count - 1] <> 10) do
begin
count := tcpClient.IOHandler.ReadBytes(numArray, 102400);
str := str + BytesToString(numArray, 0, count, IndyTextEncoding_UTF8);
end;
messageBoxText := str;
Writeln('Gelen Veri:' + str);
except
on E: Exception do
begin
Writeln('Bağlantı Hatası:' + E.Message);
end;
end;
finally
tcpClient.Free;
end;
end.
Malesef işimi çözmedi

(26-07-2024, Saat: 00:07)nevzatc Adlı Kullanıcıdan Alıntı: [ -> ] (25-07-2024, Saat: 21:14)SercanTEK Adlı Kullanıcıdan Alıntı: [ -> ]Böyle durumlarda chatgpt den yardım alıyorum. Aşağıdaki kodu üretti. Olası hataları ve algoritmayı kendinize göre düzenleyebilirsiniz. maalesef her zaman %100 sonuç alamasamda bana yol gösteriyor.
kaynak : https://chatgpt.com/
Kod:
program ConsoleTCPClient;
{$APPTYPE CONSOLE}
uses
IdTCPClient, IdGlobal, SysUtils, Classes;
var
tcpClient: TIdTCPClient;
bytes: TIdBytes;
numArray: TIdBytes;
count: Integer;
str: string;
messageBoxText: string;
begin
try
tcpClient := TIdTCPClient.Create(nil);
try
tcpClient.Host := '192.168.1.145';
tcpClient.Port := 2222;
tcpClient.Connect;
Writeln('Başarılı');
bytes := ToBytes('{"id":"getinfo"}' + #13#10, IndyTextEncoding_UTF8);
tcpClient.IOHandler.Write(bytes);
SetLength(numArray, 102400);
count := tcpClient.IOHandler.ReadBytes(numArray, 102400);
str := BytesToString(numArray, 0, count, IndyTextEncoding_UTF8);
while (count > 0) and (numArray[count - 1] <> 10) do
begin
count := tcpClient.IOHandler.ReadBytes(numArray, 102400);
str := str + BytesToString(numArray, 0, count, IndyTextEncoding_UTF8);
end;
messageBoxText := str;
Writeln('Gelen Veri:' + str);
except
on E: Exception do
begin
Writeln('Bağlantı Hatası:' + E.Message);
end;
end;
finally
tcpClient.Free;
end;
end.
Malesef işimi çözmedi 
direkt çalışmasını beklemeyin zaten. yol göstermesini bekleyin. Siz kendi bilginize ve amacınıza göre değiştirip kullanabilirsiniz. yaptığı iş TCP Client bağlantı yapıp veri okumak. internette bir çok örneği var.
@
nevzatc,
Size daha hızlı, kolay yardımcı olmak ve daha çok DelphiCan'ın cevap veya yorumla konuya katılmasını sağlamak amacıyla;
ne yapmak istediğinizi yazarsanız yada bir yerden başlayarak, sorun yaşadığınız kısmı paylaşırsanız ve destek talep ederseniz iyi olur.
uses
IdTCPClient, IdGlobal;
procedure ConnectAndGetInfo;
var
tcpClient: TIdTCPClient;
messageBoxText, responseStr: string;
bytes: TIdBytes;
count, bytesRead: Integer;
begin
try
tcpClient := TIdTCPClient.Create(nil);
try
tcpClient.Host := '192.168.1.145';
tcpClient.Port := 2222;
tcpClient.Connect;
WriteLn('Başarılı');
bytes := ToBytes('{"id":"getinfo"}' + #13#10, IndyTextEncoding_UTF8);
tcpClient.IOHandler.Write(bytes);
responseStr := '';
repeat
SetLength(bytes, 0);
tcpClient.IOHandler.ReadBytes(bytes, -1, False);
bytesRead := Length(bytes);
if bytesRead > 0 then
responseStr := responseStr + BytesToString(bytes, IndyTextEncoding_UTF8);
until (bytesRead = 0) or (bytes[bytesRead - 1] = 10);
ShowMessage(responseStr);
messageBoxText := responseStr;
WriteLn(Format('Gelen Veri:%s', [responseStr]));
finally
tcpClient.Disconnect;
tcpClient.Free;
end;
except
on E: Exception do
WriteLn(Format('Bağlantı Hatası:%s', [E.Message]));
end;
end;
Herkese teşekkür ederim, bu kod ile istediğim şekilde veri alış işlemimi gerçekleştirdim.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, Vcl.ExtCtrls;
type
TTimerThread = Class(TThread)
Private
fIdTCPClient1: TIdTCPClient;
fVeri : TMemo;
Protected
Procedure Execute ; Override;
Public
Constructor Create(ASuspended : Boolean);
End;
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
btnStart: TButton;
Veri: TMemo;
procedure btnStartClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
timerThred: TTimerThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses IdGlobal;
procedure TForm1.btnStartClick(Sender: TObject);
Var
Command: String;
bytes: TIdBytes;
begin
Command := '{"id": "gettemp"}';
if IdTCPClient1.Connected then
idTCPClient1.Disconnect;
idTCPClient1.Host := '192.168.1.125';
idTCPClient1.Port := 2222;
idTCPClient1.Connect;
bytes := ToBytes(Command+#13#10, IndyTextEncoding_UTF8);
if IdTCPClient1.Connected then
IdTCPClient1.IOHandler.Write(bytes);
end;
{ TTimerThread }
constructor TTimerThread.Create(ASuspended : Boolean);
begin
inherited;
end;
procedure TTimerThread.Execute;
begin
while not Terminated do
begin
if fIdTCPClient1.Connected then
if not fIdTCPClient1.Socket.InputBufferIsEmpty then
begin
fVeri.Lines.Add(fIdTCPClient1.Socket.ReadLn);
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
timerThred.Terminate;
Action := caFree;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
timerThred := TTimerThread.Create(True);
timerThred.fVeri := Veri;
timerThred.fIdTCPClient1 := IdTCPClient1;
timerThred.Start;
end;
end.