Hallo
versuche anhand des Internets Google mit Tutorials Delphi zu lernen.
Nun das ist das erstemal das ich mich überhaupt an ein Forum wende Sonst habe ich immer alles geschafft mit Google.
Google hat mir Z.b. php beigebracht.
Ich komme nun leider überhaupt nicht weiter weis nicht was falsch ist an dem Code schnipsel.
der Client funktioniert eben dieser Teil hier nicht.
Wäre echt geil wen sich das mal ein Profi anguggt und mir sagt was daran falsch ist und mir erklärt wie ich das Problem behebe. Brauche Hilfe ich probiere seit 3 Tagen dran rum lol ich gebe erstmal nicht auf mal sehen woran es liegt.
hier der Code:
Delphi-Quellcode:
program prjServer;
{$APPTYPE CONSOLE}
uses
Winsock,
Windows,
SysUtils;
type
TDynStringArray = array of AnsiString;
type
TServer = class(TObject)
Private
hSocket: TSocket;
Addr: TSockAddrIn;
wsaData: TWSAData;
Public
procedure Connect;
procedure SendData(Data: AnsiString);
procedure ReceiveData(Socket: TSocket; Buffer: PByte);
function UserName(): AnsiString;
function CompName(): AnsiString;
function GetIPFromHost(const HostName: AnsiString): AnsiString;
function Explode(const Separator, S :AnsiString; Limit :Integer = 0): TDynStringArray;
end;
var
Server: TServer;
Close: Boolean;
//Connection settings
hPort: Integer;
hHost: AnsiString;
procedure TServer.ReceiveData(Socket: TSocket; Buffer: PByte);
var
Data: AnsiString;
strArr: TDynStringArray;
begin
//Convert our data to a AnsiString and trim it
Data := AnsiString(Buffer);
Data := Trim(Data);
if Length(Data) > 0 then begin
//Setup our dynamic array structure
StrArr := Explode('|', Data);
//Display incoming message (if strArr[0] = 'MSGBOX')
if StrArr[0] = 'MSGBOX' then begin
MessageBox(0,PAnsiChar(StrArr[1]),'Winsock Example',MB_OK + MB_ICONINFORMATION)
end;
end;
end;
procedure TServer.Connect;
var
Buffer: Array[0..8192] Of AnsiChar;
iRecv: Integer;
Begin
if (WSAStartup($0202, wsaData) <> 0) then
begin
Writeln('Unable to start winsock, unable to continue');
Readln;
Exit;
end;
Close := False;
repeat
//Prepare a listen socket to be used for our connection
//and declare its host/port information
hSocket := Socket(AF_INET, SOCK_STREAM, 0);
Addr.sin_family := AF_INET;
Addr.sin_port := htons(hPort);
Addr.sin_addr.S_addr := INET_ADDR(PAnsiChar(GetIPFromHost(hHost)));
Writeln('Connecting to ' + GetIPFromHost(hHost));
//Attempt the connection using our socket
if (Winsock.Connect(hSocket, Addr, SizeOf(Addr)) = 0) then
begin
Writeln('Connected to host');
//Notify our connected client of the connection and send
//our information
SendData('ADDNEW|' + UserName + '|' + CompName + '|' + 'Awaiting commands');
//Attempt to receive data from our socket.
ZeroMemory(@Buffer, SizeOf(Buffer));
iRecv := Recv(hSocket, Buffer, SizeOf(Buffer), 0);
while ((iRecv > 0) and (iRecv <> INVALID_SOCKET)) do
begin
ReceiveData(hSocket, @Buffer);
ZeroMemory(@Buffer, SizeOf(Buffer));
iRecv := Recv(hSocket, Buffer, SizeOf(Buffer), 0);
end;
//We got disconnected if we reach this point
Writeln('Lost connection');
CloseSocket(hSocket);
end;
//Wait 3 seconds and repeat our connection attempt
Sleep(3000);
until (Close);
WSACleanup();
end;
function TServer.UserName(): AnsiString;
var
User : array[0..255] of AnsiChar;
SizeOfUser : Cardinal;
begin
SizeOfUser := SizeOf(User);
if GetUserName(User, SizeOfUser) = True then
Result := AnsiString(User)
else
Result := 'Unknown';
end;
function TServer.CompName(): AnsiString;
var
Comp : array[0..255] of AnsiChar;
SizeOfComp : Cardinal;
begin
SizeOfComp := SizeOf(Comp);
if GetComputerName(Comp, SizeOfComp) = True then
Result := AnsiString(Comp)
else
Result := 'Unknown';
end;
function TServer.GetIPFromHost(const HostName: AnsiString): AnsiString;
type
TaPInAddr = array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
i: Integer;
begin
Result := '';
phe := GetHostByName(PAnsiChar(HostName));
if phe = nil then Exit;
pPtr := PaPInAddr(phe^.h_addr_list);
i := 0;
while pPtr^[i] <> nil do
begin
Result := inet_ntoa(pptr^[i]^);
Inc(i);
end;
end;
//This function splits up a given AnsiString into an array
function TServer.Explode(const Separator, S :AnsiString; Limit :Integer = 0): TDynStringArray;
var
SepLen: Integer;
F, P: PAnsiChar;
begin
SetLength(Result, 0);
if (S = '') or (Limit < 0) then
Exit;
if Separator = '' then
begin
SetLength(Result, 1);
Result[0] := S;
Exit;
end;
SepLen := Length(Separator);
P := PAnsiChar(S);
while P^ <> #0 do
begin
F := P;
P := AnsiStrPos(P, PAnsiChar(Separator));
if (P = nil) or ((Limit > 0) and (Length(Result) = Limit - 1)) then
P := StrEnd(F);
SetLength(Result, Length(Result) + 1);
SetString(Result[High(Result)], F, P - F);
F := P;
if P = Separator then
SetLength(Result, Length(Result) + 1);
while (P^ <> #0) and (P - F < SepLen) do
Inc(P);
end;
end;
procedure TServer.SendData(Data: AnsiString);
var
Len: LongInt;
begin
Len := Length(Data);
Send(hSocket, Data[1], Len, 0);
end;
begin
Server := TServer.Create;
hHost := '127.0.0.1';
hPort := 12345;
Server.Connect;
end.