TCPIP is simplified by network connection (2)

zhaozj2021-02-17  56

(* @ /// PARSE A FTP DIRECTORY LINE INTO A FILEDATA RECORD (UNIX AND DOS STYLE ONLY) *) Const Month_String: Array [0..11] of string = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'SEP', 'OCT', 'NOV', 'DEC');

(* @ /// Function getMonth (const S: String): Integer; month -> integer *) Function getMonth: integer; var i: integer; begin result: = 0; for i: = 0 To 11 do if s = month_string [i] the beginning: = i 1; exit; end; end; (* @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 0000000301 *)

Const Empty_FileData: t_filedata = (filetype: ft_none; size: 0; name: ''; DateTime: 0);

(* @ /// function parse_line_unix (const s: string): t_filedata; *) function parse_line_unix (const v: string): t_filedata; (* known problems: filename with spaces (most unix's do not allow the anyway) *) (* Links aren't parse at all *) VAR T, DATE: STRING; Y, M, D, H, N, S: Word; Begin Try Case V [1] of 'd': Result.FileType: = ft_dir ; '-': Result.FileType: = ft_file; 'L': Result.FileType: = ft_link; end; result.name:=copy (v, posn ('', v, -1) 1, Length (V )); T: = COPY (v, 12, length (v) -length (result .Name) -12); Date: = COPY (T, Length (T) -11, 12); DECODEDATE (now, y, M, d); h: = 0; N: = 0; s: = 0; if Pos (':', date)> 0 Then Begin H: = STRTOINT (COPY (Date, 8, 2)); N: = STRTOINT (COPY (DATE, 11, 2)); END ELSE Y: = STRTOINT (COPY (DATE, 9, 4)); D: = STRTOINT (TRIM (Copy (Date, 5, 2))); M: = GETMONTH (COPY (DATE, 1, 3)); T: = COPY (T, 1, Length (T) -13); Result.Size: = StrtOINT (COPY (t, posn ('', t, -1 ) 1, Length (T)))); Result.DateTime: = Encodedate (Y, M, D) ENCOD ETIME (H, N, S, 0); ExcePt Result: = EMPTY_FILEDATA; END; END; (* @ /// 0000000201 *) (* @ /// Function PARSE_LINE_DOS (Const S: String): t_filedata; *) function PARSE_LINE_DOS: T_Filedata; (* KNown Problems: FileName with space;) VAR T: String; SD, ST: STRING; DS: CHAR; Begin DS: = DateSeparator; SD : = ShortdateFormat; St: = ShortTimeFormat; Try IF POS ('

', v) = 0 Then Result.FileType: = ft_file else result.filety: = ft_dir; result.name:=copy (v, POSN (' ', V, -1) 1, Length (V));

T: = COPY (V, 1, Length (V) -length (result.name) -1); Result.Size: = start ('0' Copy (T, POSN (', T, -1) 1, Length (t))); DateSeparator: = '-'; shortdateformat: = 'mm / dd / yy'; ShortTimeFormat: = 'hh: nnam / pm'; result.datetime: = start.datetime (Copy (t, 1 , 17)); ExcePT Result: = EMPTY_FILEDATA; END; DATESEPARATOR: = DS; shortdateformat: = SD; ShortTimeFormat: = st; end; (* @ @ @ @ @ @ @ @ /// Function PARSE_FTP_LINE (Const S) : string: t_filedata; *) Function Parse_ftp_LINE (const S: string): t_filedata; begin if copy (s, 1, 5) = 'Total' Then (* First Line for Some Unix FTP Server *) Result: = EMPTY_FILEDATA ELSE IF s [1] in ['D', 'L', '-', 's'] Then Result: = PARSE_LINE_UNIX (S) Else IF S [1] in ['0' .. '9'] Then Result : = PARSE_LINE_DOS (S); END; (* @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 0000000401 *)

(* @ /// procedure stream_write_s (h: TMemoryStream; const s: string); // string -> stream *) procedure stream_write_s (h: TMemoryStream; const s: string); var buf: pointer; begin buf: = @ s [1]; h.write (buf ^, length (s)); end; (* @ @ @ @ @ @ @ @ @)

Const back_log = 2; (* Possible Values ​​1..5 *) Fingerd_timeout = 5; buf_size = $ 7f00; (* size of the interfaceard buffer *)

(* @ /// class EProtocolError (ETcpIpError) *) constructor EProtocolError.Create (const proto, Msg: String; number: word); begin Inherited Create (Msg); protocoll: = proto; errornumber: = number; end; ( * @ /// 0000000301 *) (* @ /// class ESocketError (ETcpIpError) *) constructor ESocketError.Create (number: word); begin inherited create ( 'Error creating socket'); errornumber: = number; end; ( * @ /// *) (* @ /// Class EPROTOCOLBUSY (ETCPIPERROR) *) Constructor EPROTOCOLBUSY.CREATE; Begin inherited Create ('protocol busy'); end; (* @ @ @ @ @ / // Procedure Parse_url (Const URL: String; Var Proto, User, Pass, Host, Port, Path: String); *) Procedure Parse_URL (Const URL: String; Var Proto, User, Pass, Host, Port, Path: String );

(* Standard Syntax of an Url: protocol: // [user [: password] @] server [: port] / path *)

VAR P, Q: INTEGER; S: String; Begin Proto: = '; user: ='; pass: = '; host: ='; port: = ''; PATH: = ';

P: = POS (': //', url); if p = 0 Then Begin if LowerCase (Copy (URL, 1, 7)) = 'mailto:' Then Begin (* mailto: // NOT Common *) Proto : = 'Mailto'; P: = POS (':', URL); End; Else Begin Proto: = COPY (URL, 1, P-1); Inc (p, 2); end; s: = COPY (URL, P 1, Length (URL));

P: = POS ('/', s); if P = 0 THEN P: = Length (s) 1; Path: = COPY (S, P, Length (s)); s: = Copy (s, 1 , P-1);

p: = POSN (':', s, -1); if p> length (s) THEN P: = 0; Q: = POSN ('@', s, -1); if q> Length (s) THEN Q: = 0; IF (p = 0) and (q = 0) THEN BEGIN (* No User, Password or port *) host: = s; exit; end else if q

nil; if f_buffer <> nil Then FreeMem (f_buffer, buf_size); f_stream.free; if f_socket <> invalid_socket the logout; deallocatehwnd (f_handle); inherited destroy; end; (* @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 0000000301 *)

(* @ /// procedure t_tcpip.WndProc (var Msg: TMessage); *) procedure t_tcpip.WndProc (var Msg: TMessage); begin if msg.msg = uwm_socketevent then begin if msg.lparamhi = socket_error then else begin case msg .lparamlo of (* @ /// fd_read: *) fd_read: begin f_newdata: = true; end; (* @ @ @ @ @ @ @ @ @ @ e; END ELSE DISPATCH (MSG); END; (* @ // / 0000000701 *)

(* @ /// function t_tcpip.Create_Socket: TSocket; *) function t_tcpip.Create_Socket: TSocket; begin result: = Winsock.Socket (PF_INET, SOCK_STREAM, IPPROTO_IP); end; (* @ /// *) (* @ /// procedure t_tcpip.bind_socket (var socket: TSocket; out_port_min, out_port_max: word); *) procedure t_tcpip.bind_socket (var socket: TSocket; out_port_min, out_port_max: word); var LocalAddress: TSockAddr; i: word; begin with LocalAddress do begin sin_Family: = PF_INET; Sin_addr.S_addr: = INADDR_ANY; end; i: = out_port_min; while i <= out_port_max do begin LocalAddress.Sin_Port: = Winsock.htons (i); if Winsock.bind (socket, LocalAddress, SizeOf (LocalAddress)) <> SOCKET_ERROR then BREAK; inc (i); end; end; (* @ /// 0000000401 *) (* @ /// procedure t_tcpip.connect_socket (var socket: TSocket; socket_number: smallint; ip_address : longint); *) procedure t_tcpip.connect_socket (var socket: TSocket; socket_number: smallint; ip_address: longint); var RemoteAddress: TSockAddr; begin with RemoteAddress do begin sin_Family: = PF_INET; Sin_Port: = Winsock.htons (Socket_number); Sin_addr: = TInAddr (ip_address); end; if Winsock.Connect (Socket, RemoteAddress, SizeOf (RemoteAddress)) = SOCKET_ERROR then begin if winsock.WSAGetLastError <> wsaewouldblock then begin Close_Socket (socket ); if assigned (f_tracer) then f_tracer ( 'Failed to open output socket' inttostr (socket_number) 'to host' ip2string (ip_address), tt_socket); end end else if assigned (f_tracer) then f_tracer ( 'Opened output Socket ' INTOSTR (Socket_Number) ' to Host ' IP2String (IP_ADDRESS) ' SuccessFully

ID ' inttostr (socket), tt_socket); end; (* @ /// 000E00101C00101C00101C00101C *) (* @ /// procedure t_tcpip.open_socket_out (var socket: TSocket; Socket_number: smallint; ip_address: longint); *) procedure t_tcpip.open_socket_out (var socket: TSocket; socket_number: smallint; ip_address: longint); begin close_socket (socket); socket: = create_Socket; connect_socket (socket, socket_number, ip_address); end; (* @ /// 0000000501 *) ( * @ /// procedure t_tcpip.open_socket_in (var socket: TSocket; socket_number: smallint; ip_address: longint); *) procedure t_tcpip.open_socket_in (var socket: TSocket; socket_number: smallint; ip_address: longint); var LocalAddress: TSockAddr; begin close_socket (socket); f_Socket: = create_Socket; (* @ /// open the socket and let it listen *) with LocalAddress do begin sin_Family: = PF_INET; sin_Port: = Winsock.htons (socket_number); sin_addr: = TInAddr ( IP_address; end; if Winsock.bind (socket, localaddress, sizeof (localaddress)) = Socket_ERROR THEN BEGIN IF Assigned (f_tracer) THEN F_TRAC er ( 'Failed to bind socket' inttostr (Socket_number) 'for local ip' ip2string (ip_address), tt_socket); Close_Socket (socket); EXIT; endelse if assigned (f_tracer) then f_tracer ( 'Bound to socket' inttostr (socket_number) 'for local ip' ip2string (ip_address), tt_socket); if Winsock.Listen (Socket, back_log) = SOCKET_ERROR then begin Close_Socket (socket); if assigned (f_tracer) then f_tracer ( 'Failed to set input Socket ' INTOSTR (Socket_Number) ' To listening Mode ', TT_Socket; endelse if assigned (f_tracer) THEN F_Tracer (' set input socket '

inttostr (Socket_number) 'to listening mode sucessfully; ID' inttostr (socket), tt_socket); (* @ /// 0030000A18000A18001123 *) end; (* @ /// 0000000701 *) (* @ /// function t_tcpip .accept_socket_in (socket: TSocket; var SockInfo: TSockAddr): TSocket; *) function t_tcpip.accept_socket_in (socket: TSocket; var SockInfo: TSockAddr): TSocket; var x: u_int; LocalAddress: TSockAddr; temp_socket: TSocket; begin x: = SizeOf (LocalAddress); (* $ ifndef ver100 *) temp_socket: = Winsock.Accept (Socket, LocalAddress, x); (* $ else *) {Delphi 3 aRGH!} temp_socket: = Winsock.Accept (Socket, @ LocalAddress , @ x); (* $ endif *) if temp_socket = SOCKET_ERROR then begin (* no incoming call available *) temp_socket: = INVALID_SOCKET; if assigned (f_tracer) then f_tracer ( 'No incoming connection found on socket ID' inttostr ( Socket, tt_socket; end else if assigned (f_tracer) THEN F_TRACER ('incoming connection found on socket ID' INTSTOSTR (Socket) '; Generated Socket ID' INT tostr (temp_socket), tt_socket); accept_socket_in: = temp_socket; sockinfo: = LocalAddress; end; (* @ /// 0000001748 *) (* @ /// function t_tcpip.socket_state (socket: TSocket): T_Socket_State; *) function t_tcpip.socket_state (socket: TSocket): T_Socket_State; var peer_adr: TSockAddr; x: u_int; begin if socket = INVALID_SOCKET then socket_state: = invalid else begin x: = sizeof (TSockAddr); if winsock.getpeername (socket, peer_adr, x ) = 0 THEN SOCKET_STATE: = Connected Else Begin IF Winsock.wsagetlasterror <> wsaenotconn the socket_state: = state_unknown else socket_state: = VALID END;

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

New Post(0)