Einzelnen Beitrag anzeigen

strom

Registriert seit: 23. Aug 2008
Ort: Keine Ergebnisse gefunden
290 Beiträge
 
#6

AW: Socket- Fehler

  Alt 22. Nov 2015, 22:56
Hallo,

bekomme immer noch zwei Fehlermeldungen (siehe Anhang)!
Was mache ich falsch?

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, WinSock, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure InitSockets;
var
 Data: WSAData;
 Res: Integer;
 ErrorCode: Integer;
 S: String;
 Len: Integer;
begin
 res := WSAStartup( makeWord(1,1), Data);
  if res = SOCKET_ERROR then
   begin
     ErrorCode := WSAGetLastError;
     Setlength(s,260);
     Len := Formatmessage(Format_Message_from_System,
     nil, errorCode, 0, @s[1], length(s), nil);
     Setlength(s, len);
     MessageBox(0, PChar(s), 'Socket Error',
     MB_OK or MB_ICONERROR or MB_TASKMODAL);
   end;
end;

procedure HandleError;
var
 ErrorCode: Integer;
 S: String;
 Len: Integer;
begin
 ErrorCode := WSAGetLastError;
  Setlength(s,260);
     Len := Formatmessage(Format_Message_from_System,
     nil, errorCode, 0, @s[1], length(s), nil);
     Setlength(s, len);
     MessageBox(0, PChar(s), 'Socket Error',
     MB_OK or MB_ICONERROR or MB_TASKMODAL);
end;

function createSocket_UDP: TSocket;
begin
    result := socket(AF_INET,SOCK_STREAM,IPProto_UDP);
  if result = INVALID_SOCKET then HandleError;
end;

procedure sendbuf_via_UDP(IP:string; Port: Word; var buf; buflen: Integer);
var
 SockAddr: TSockAddrIn;
 AddrLen: Integer;
 FSocket: TSocket;
begin
 FSocket := createSocket_UDP;

 Addrlen := SizeOf(SockAddr);

 SockAddr.sin_family := AF_INET;
 SockAddr.sin_Port := htons(Port);
 SockAddr.sin_Addr.S_Addr := inet_addr(PAnsiChar(IP));

 if Sendto( FSocket, buf, buflen, 0, SockAddr, AddrLen) = Socket_ERROR then
  HandleError;

 CloseSocket( FSocket);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 MyString: String;
begin
 MyString := 'test';
 Sendbuf_via_UDP('192.168.0.251', 1234, MyString[1], length(MyString));
end;

initialization
 Initsockets;

finalization
WSACleanup;

end.
Miniaturansicht angehängter Grafiken
22-11-15-22-48-16-.png   22-11-15-22-48-01-.png  
  Mit Zitat antworten Zitat