TCPIP (3)

zhaozj2021-02-17  55

(* @ /// 0000000D01 *) (* @ /// procedure t_tcpip.close_socket (var socket: TSocket); *) procedure t_tcpip.close_socket (var socket: TSocket); begin if socket <> INVALID_SOCKET then begin Winsock.CloseSocket (socket); if assigned (f_tracer) THEN F_Tracer ('Closed Socket ID' INTOSTR (Socket), TT_Socket; Socket: = Invalid_socket; End; End; (* @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ /// procedure t_tcpip.close_socket_linger (var socket: TSocket); *) procedure t_tcpip.close_socket_linger (var socket: TSocket); var linger: TLinger; begin if socket <> INVALID_SOCKET then begin linger.l_onoff: = 1; linger.l_linger: = fingerd_timeout Winsock.setsockopt (socket, sol_socket, so_linger, pchar (@Linger), sizeof (linger)); Winsock.shutdown (socket, 1); close_socket (socket); socket: = invalid_socket; end; end; (* @ / // 0000000842 *) (* @ /// function t_tcpip.Socket_by_name (const service: string): smallint; *) function t_tcpip.Socket_by_name (const service: string): smallint; var service_entry: PServEnt; s: string; begin s : = Service # 0; (* $ IFDEF VER80 *) service_entry: = Winsock.getServByname (Pchar (@S [1]), 'TCP'); (* $ else *) Service_Entry: = Winsock.getServbyName (PCHAR (@S [1]), 'TCP'); (* $ else *) service_entry: = Winsock.getServByname (Pchar (s), 'TCP'); (* $ ENDIF *) (* $ ENDIF *) IF service_entry = nil the result: = 0 else result: = Winsock.htons (service_entry ^ .s_port); end; (* @ @ @ @ @ @ 000000E02 *)

(* @ /// procedure t_tcpip.Login; *) procedure t_tcpip.Login; begin if f_logged_in then logout; ip_address: = lookup_hostname (f_hostname); if ip_address = INVALID_IP_ADDRESS then raise ETcpIpError.Create ( 'Couldn''t resolve hostname' f_hostname); open_socket_out (f_socket, f_Socket_number, ip_address); if f_socket = INVALID_SOCKET then raise ESocketError.Create (WSAGetLastError); f_eof: = false; f_logged_in: = true; end; (* @ /// 0000000315 *) (* @ /// procedure t_tcpip.LogOut; *) procedure t_tcpip.LogOut; begin close_socket (f_socket); f_socket: = invalid_socket; f_logged_in: = false; end; (* @ /// 0000000501 *) (* @ /// procedure t_tcpip .Sendcommand; *) procedure t_tcpip.sendcommand (const s: string); begin self.write_s (f_socket, s # 13 # 10); if Assigned (f_tracer) THEN F_Tracer (s, tt_proto_sent); end (* @ @ @ @)

(* @ /// function t_tcpip.eof (f_socket: tsocket): boolean; !!! *) Function T_tcpip.eof (f_socket: tsocket): boolean; begin EOF: = f_eof or (socket_state (f_socket) <> connection End; (* @ @ @ @ @ @ @ /// procedure t_tcpip.read_var (f_socket: tsocket; var buf; size: integer; var _ok: integer); *) procedure t_tcpip.read_var (f_socket: tisocket Var Buf; integer; var_ok: integer; var temp_buf: Pointer; error: integer; begin temp_buf: = nil; try @ buf = nil dam (TEMP_BUF, SIZE) (* alloc for the -> / DEV / NULL *) ELSE TEMP_BUF: = @ Buf; Repeat_ok: = Winsock.Recv (f_socket, temp_buf ^, size, 0); if _ok <= 0 THEN BEGIN Error: = Winsock.wsagetlasterror; (* listening socket is always non-blocking, but this causes problems with the recv command *) if error = wsaewouldblock then begin if f_async then begin f_newdata: = false; while not f_newdata do Application.ProcessMessages; end; end ; F_eof: = error <> wsaewouldblock; end else if assigned (f_tracer) then f_tracer ( 'Received' inttostr (_ok) 'bytes on socket ID' inttostr (f_socket), tt_socket); until f_eof or (_ok> 0 Finally if @ buf = nil dam (temp_buf, size) end; end; (* @ @ @ @ @ fc (f_socket: tsocket): string; *) function t_tcpip .read_line (f_socket: tsocket): string; var x: char; ok: integer; s: string; begin s: = '; repeat read_var (f_socket, x, 1, ok); if x =

# 13 death (* at Least NCSA 1.3 Does Send A # 10 Only *) Else if x = # 10 The begin Result: = S; EXIT; END ELSE BEGIN S: = S X; END; Until Eof (f_socket); End; (* @ /// *) (* @ /// procedure t_tcpip.write_buf (f_socket: tsocket; const buf; size: integer); *) procedure t_tcpip.write_buf (f_socket: tsocket; const buf; size: integer ); begin if Winsock.send (f_socket, pointer (@BUF) ^, size, 0) = Socket_ERROR THEN EXIT (* Error Writing *) Else if Assigned (f_tracer) THEN F_Tracer ('Sent' INTOSTR (SIZE) ' BYtes on socket ID ' INTOSTR (f_socket), tt_socket; end; (* @ /// 0000000801 *) (* @ /// procedure t_tcpip.write_s (f_socket: tsocket; const s: string); *) Procedure T_tcpip .write_s (f_socket: tsocket; const s: string); Begin (* $ ifdef Ver80 *) Write_buf (f_socket, pchar (@S [1]) ^, Length (s)); (* $ else *) (* $ Ifopt h- *) Write_buf (f_socket, pchar (@s [1]) ^, Length (s)); (* $ else *) Write_buf (f_socket, pchar (s) ^, length (s)); (* $ Endif *) (* $ ENDIF *) end; (* @ @ @ @ @ /// proce dure t_tcpip.SetStream (value: TStream); *) procedure t_tcpip.SetStream (value: TStream); begin TMemoryStream (f_stream) .LoadFromStream (value); end; (* @ /// 0000000301 *)

(* @ /// procedure t_tcpip.action; *) procedure t_tcpip.action; var p: pointer; ok, ok2: integer; begin login; TMemorystream (f_stream) .clear; while not eof (f_socket) do begin read_var (f_socket , f_buffer ^, buf_size, ok; while ok> 0 do begin (* Just to Be Sure Everything Goes Into the stream *) ok2: = f_stream.write (p ^, ok); DEC (OK, OK2); p: = POINTER (longint (p) ok2); end; end; f_stream.seek (0, 0); end; (* @ @ @ @ @ /// *)

{Finger client and demon} (* @ /// class t_finger (t_tcpip) *) (* @ /// constructor t_finger.Create (Aowner: TComponent); *) constructor t_finger.Create (Aowner: TComponent); begin inherited create (Aowner); f_socket_number: = ipport_finger; (* 79 *) end; (* @ @ @ @ @ @ 0000000403 *)

(* @ /// procedure t_finger.action; *) procedure t_finger.action; var P: Pointer; OK, OK2: Integer; s: string; begin login; s: = f_user # 13 # 10; Write_S (f_socket, s ); TMemorystream (f_stream) .clear; while not eof (f_socket) do begin read_var (f_socket, f_buffer ^, buf_size, ok); p: = f_buffer; while ok> 0 do begin (* just to be sure everything goes into the Stream *) ok2: = f_stream.write (p ^, ok); DEC (OK, OK2); P: = POINTER (longint (p) ok2); end; end; f_stream.seek (0, 0); * set the stream back to start *) logout; end; (* @ @ @ @ @ @ @ /// class t_fingerd (t_tcpip) *) (* @ /// constructor t_fingerd.Create (Aowner: TComponent); *) constructor t_fingerd.Create (Aowner: TComponent); begin inherited create (AOwner); f_Socket_number: = IPPORT_FINGER; (* 79 *) f_answer: = TStringList.Create; end;

转载请注明原文地址:https://www.9cbs.com/read-30406.html

New Post(0)