From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,99ad16995c7745f2 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!k13g2000hse.googlegroups.com!not-for-mail From: "snoopysalive@googlemail.com" Newsgroups: comp.lang.ada Subject: Re: How to implement a server socket compatible to telnet? Date: Wed, 20 Aug 2008 14:25:04 -0700 (PDT) Organization: http://groups.google.com Message-ID: <0baa592d-1291-4298-90e3-88ca85a476a8@k13g2000hse.googlegroups.com> References: <8r_mk.151106$102.88358@bgtnsc05-news.ops.worldnet.att.net> <87bpzzcr88.fsf@willow.rfc1149.net> NNTP-Posting-Host: 217.227.52.234 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1219267505 15172 127.0.0.1 (20 Aug 2008 21:25:05 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 20 Aug 2008 21:25:05 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k13g2000hse.googlegroups.com; posting-host=217.227.52.234; posting-account=b7G67QoAAABjwLRYV73fYo_8iKCvB0IZ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; de; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:7424 Date: 2008-08-20T14:25:04-07:00 List-Id: Come on guys, stop arguing. In the end, I've found a solution: ----------------------------------------------------------------------- with Ada.Text_IO, Ada.Exceptions, Ada.Streams, Ada.Unchecked_Deallocation, GNAT.Sockets; use Ada.Text_IO, Ada.Exceptions, Ada.Streams, GNAT.Sockets; procedure IP_Server is BUFFER_SIZE : constant Positive := 1024; type String_Access is access all String; procedure Free is new Ada.Unchecked_Deallocation (String, String_Access); CRLF : constant String := ASCII.CR & ASCII.LF; Host : constant String := "localhost"; Port : Port_Type := 7777; Address : Sock_Addr_Type; Server : Socket_Type; Client : Socket_Type; Channel : Stream_Access; Data : Stream_Element_Array (1..1); Offset : Stream_Element_Count; Buffer : String_Access := new String (1..BUFFER_SIZE); Cnt : Natural := 0; Test : Float := 0.0; begin -- IP_Server Initialize; Address.Addr := Addresses (Get_Host_By_Name (Host), 1); Address.Port := Port; Create_Socket (Server); Set_Socket_Option (Server, Socket_Level, (Reuse_Address, True)); Bind_Socket (Server, Address); Listen_Socket (Server); Accept_Socket (Server, Client, Address); Channel := Stream (Client); Cnt := 0; loop Read (Channel.all, Data (1..1), Offset); if Character'Val (Data (1)) = ASCII.CR or Character'Val (Data (1)) = ASCII.LF or Character'Val (Data (1)) = ASCII.NUL or Offset = 0 then exit; else Cnt := Cnt + 1; Buffer.all (Cnt) := Character'Val (Data (1)); end if; end loop; -- Read values from client-stream character by character. -- Reading should be stopped when Windows-linefeed or -- NULL was found, because telnet seams to be sending -- strings in a Windows-like format including the -- terminating \0-character known from C-strings. declare Old : String_Access := Buffer; begin Buffer := new String'(Buffer (1..Cnt)); Free (Old); end; -- The buffer-size of Str is 1024 elements. It's necessary -- to create a new String containing only relevant characters -- for being able to process the message further. declare Pong : String := "pong" & CRLF; O : Stream_Element_Array (1..Pong'length); begin if Buffer.all = "ping" then for I in Pong'range loop O (Stream_Element_Offset (I)) := Character'Pos (Pong (I)); end loop; Write (Channel.all, O); end if; end; -- If Buffer's message equals "ping" the server will -- send "pong" to the client. "pong" must be casted -- from String to Stream_Element_Array first. Close_Socket (Client); Close_Socket (Server); Finalize; exception when E : Socket_Error => Put_Line (Standard_Error, "Socket_Error => " & Exception_Message (E)); end IP_Server; ----------------------------------------------------------------------------------- Thanks to the thread under http://groups.google.com/group/comp.lang.ada/browse_thread/thread/c58b7bd180ea81b2 I found out how to read character by character from the client. However, it's not very comfortable to cast the echo-string manually but it's better than not being able to communicate with telnet or other C-based clients. Bye, Matthias