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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8fe5da0613b844b5,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-05-09 12:46:49 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: file13@qlippoth.zzn.com (file13) Newsgroups: comp.lang.ada Subject: GNAT.Sockets Problems Date: 9 May 2003 12:46:48 -0700 Organization: http://groups.google.com/ Message-ID: <28d8936a.0305091146.18a4dd57@posting.google.com> NNTP-Posting-Host: 208.188.29.61 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1052509608 3693 127.0.0.1 (9 May 2003 19:46:48 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 9 May 2003 19:46:48 GMT Xref: archiver1.google.com comp.lang.ada:37124 Date: 2003-05-09T19:46:48+00:00 List-Id: Howdy all, I've been tinkering with GNAT.Sockets on Mandrake 9.1 using the Gnat-3.15p linux binary and I'm simply trying to write the first day time client from Stevens UNIX Network Programming book with it and I can't seem to figure out what I'm doing wrong. The server I've tested on is the following one from Steven's book (it's in C): #include /* basic system data types */ #include /* basic socket definitions */ #include /* sockaddr_in{} and other Internet defns */ #include /* inet(3) functions */ #include #include #include #include #include #define MAXLINE 4096 /* max text line length */ #define SA struct sockaddr #define LISTENQ 1024 /* 2nd argument to listen() */ int main(int argc, char **argv) { int listenfd, connfd; struct sockaddr_in servaddr; char buff[MAXLINE]; time_t ticks; listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); /* daytime server */ bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); listen(listenfd, LISTENQ); for ( ; ; ) { connfd = accept(listenfd, (SA *) NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); write(connfd, buff, strlen(buff)); close(connfd); } } I've tested it with the following accompanying C client code and it works fine: #include /* basic system data types */ #include /* basic socket definitions */ #include /* sockaddr_in{} and other Internet defns */ #include /* inet(3) functions */ #include #include #include #include #define MAXLINE 4096 /* max text line length */ #define SA struct sockaddr int main(int argc, char **argv) { int sockfd, n; char recvline[MAXLINE + 1]; struct sockaddr_in servaddr; if (argc != 2) puts("usage: a.out "); if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) puts("socket error"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(13); /* daytime server */ if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) printf("inet_pton error for %s", argv[1]); if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) puts("connect error"); while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) puts("fputs error"); } if (n < 0) puts("read error"); exit(0); } $ ./daytimetcpcli 127.0.0.1 Fri May 9 14:37:41 2003 so far in Ada I have: with Ada.Exceptions; use Ada.Exceptions; with Ada.Text_IO; use Ada.Text_IO; with GNAT.Sockets; use GNAT.Sockets; procedure Daytime_Client is Target : Inet_Addr_Type := Inet_Addr ("127.0.0.1"); Socket : Socket_Type; Server : Sock_Addr_Type; Channel : Stream_Access; begin Initialize; Create_Socket (Socket); Server := (Family_Inet, Target, 13); Connect_Socket (Socket, Server); Channel := Stream (Socket, Server); Put_Line (String'Input (Channel)); Shutdown_Socket (Socket); Close_Socket (Socket); Finalize; exception when E : others => Put_Line (Exception_Name (E) & ": " & Exception_Message (E)); end Daytime_Client; It appears to connect just fine because if I turn off the server it gives me: $ ./daytime_client GNAT.SOCKETS.SOCKET_ERROR: [111] Connection refused but if it's on I'm getting the following error: $ ./daytime_client ADA.IO_EXCEPTIONS.END_ERROR: s-stratt.adb:188 Also if I change the port to something else open like 22 I get a segmentation fault: $ ./daytime_client Segmentation fault Why wouldn't it get a similar error if it's only ripping the banner off of the server port? Am I doing something wrong with the streams (this is the first time I've used Ada's streams) or do I need to GNAT.Sockets.Receive_Socket? If so can anyone provide me with an example? I know in C you have to use a loop, but I didn't know if that was necessary with Ada.Streams. I tried it with Receive_Socket also, but I could not figure out how to print the output to regular stdout.... BTW: I have looked at Samuel Tardieu's AdaSockets but I ran into compilation problems on Mac 10.2.6 (my home box) so I decided to stick with GNAT.Sockets since the ultimate goal is to eventually get some portable socket functions, the first of which I would like to get similar to a simple Python program: http://www.qlippoth.com/ripban.py Any help or guidance would be greatly appreciated. Thanks! file13 http://www.qlippoth.com/