Advertisement
FlyFar

abWinsock.pas

Dec 19th, 2023
1,371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.31 KB | Cybersecurity | 0 0
  1. unit abWinsock;
  2.  
  3. interface
  4.   uses Windows, Winsock;
  5.  
  6. type
  7.   TClientSocket = class(TObject)
  8.   private
  9.     FAddress: PChar;
  10.     FData: Pointer;
  11.     FTag: Integer;
  12.     FConnected: Boolean;
  13.   protected
  14.     FSocket: TSocket;
  15.   public
  16.     property Connected: Boolean read FConnected;
  17.     property Data: Pointer read FData write FData;
  18.     property Socket: TSocket read FSocket;
  19.     property Tag: integer read FTag write FTag;
  20.    
  21.     procedure Connect(Address: string; Port: Integer);
  22.     destructor Destroy; override;
  23.     procedure Disconnect();
  24.     function ReceiveBuffer(var Buffer; BufferSize: Integer): Integer;
  25.     function ReceiveLength(): Integer;
  26.     function ReceiveString(): String;
  27.     function SendBuffer(var Buffer; BufferSize: Integer): Integer;
  28.     function SendString(const Buffer: String): Integer;
  29.   end;
  30.  
  31. var
  32.   WSAData: TWSAData;
  33.  
  34. implementation
  35.  
  36. procedure TClientSocket.Connect(Address: String; Port: Integer);
  37. var
  38.   SockAddrIn: TSockAddrIn;
  39.   HostEnt: PHostEnt;
  40. begin
  41.   Disconnect;
  42.   FAddress := pchar(Address);
  43.   FSocket := Winsock.socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  44.   SockAddrIn.sin_family := AF_INET;
  45.   SockAddrIn.sin_port := htons(Port);
  46.   SockAddrIn.sin_addr.s_addr := inet_addr(FAddress);
  47.   if SockAddrIn.sin_addr.s_addr = INADDR_NONE then
  48.   begin
  49.     HostEnt := gethostbyname(FAddress);
  50.     if HostEnt = nil then
  51.     begin
  52.       Exit;
  53.     end;
  54.     SockAddrIn.sin_addr.s_addr := Longint(PLongint(HostEnt^.h_addr_list^)^);
  55.   end;
  56.   Winsock.Connect(FSocket, SockAddrIn, SizeOf(SockAddrIn));
  57.   FConnected := True;
  58. end;
  59.  
  60. procedure TClientSocket.Disconnect();
  61. begin
  62.   closesocket(FSocket);
  63.   FConnected := False;
  64. end;
  65.  
  66. function TClientSocket.ReceiveLength(): Integer;
  67. begin
  68.   Result := ReceiveBuffer(Pointer(nil)^, -1);
  69. end;
  70.  
  71. function TClientSocket.ReceiveBuffer(var Buffer; BufferSize: Integer): Integer;
  72. begin
  73.   if BufferSize = -1 then
  74.   begin
  75.     if ioctlsocket(FSocket, FIONREAD, Longint(Result)) = SOCKET_ERROR then
  76.     begin
  77.       Result := SOCKET_ERROR;
  78.       Disconnect;
  79.     end;
  80.   end
  81.   else
  82.   begin
  83.      Result := recv(FSocket, Buffer, BufferSize, 0);
  84.      if Result = 0 then
  85.      begin
  86.        Disconnect;
  87.      end;
  88.      if Result = SOCKET_ERROR then
  89.      begin
  90.        Result := WSAGetLastError;
  91.        if Result = WSAEWOULDBLOCK then
  92.        begin
  93.          Result := 0;
  94.        end
  95.        else
  96.        begin
  97.          Disconnect;
  98.        end;
  99.      end;
  100.   end;
  101. end;
  102.  
  103. function TClientSocket.ReceiveString(): String;
  104. begin
  105.   SetLength(Result, ReceiveBuffer(Pointer(nil)^, -1));
  106.   SetLength(Result, ReceiveBuffer(Pointer(Result)^, Length(Result)));
  107. end;
  108.  
  109. function TClientSocket.SendBuffer(var Buffer; BufferSize: Integer): Integer;
  110. var
  111.   ErrorCode: Integer;
  112. begin
  113.   Result := send(FSocket, Buffer, BufferSize, 0);
  114.   if Result = SOCKET_ERROR then
  115.   begin
  116.     ErrorCode := WSAGetLastError;
  117.     if (ErrorCode = WSAEWOULDBLOCK) then
  118.     begin
  119.       Result := -1;
  120.     end
  121.     else
  122.     begin
  123.       Disconnect;
  124.     end;
  125.   end;
  126. end;
  127.  
  128. function TClientSocket.SendString(const Buffer: String): Integer;
  129. begin
  130.   Result := SendBuffer(Pointer(Buffer)^, Length(Buffer));
  131. end;
  132.  
  133. destructor TClientSocket.Destroy;
  134. begin
  135.   inherited Destroy;
  136.   Disconnect;
  137. end;
  138.  
  139. initialization
  140.   WSAStartUp(257, WSAData);
  141.  
  142. finalization
  143.   WSACleanup;
  144.  
  145. end.
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement