comp.lang.ada
 help / color / mirror / Atom feed
From: file13@qlippoth.zzn.com (file13)
Subject: GNAT.Sockets Problems
Date: 9 May 2003 12:46:48 -0700
Date: 2003-05-09T19:46:48+00:00	[thread overview]
Message-ID: <28d8936a.0305091146.18a4dd57@posting.google.com> (raw)

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        <sys/types.h>   /* basic system data types */
#include        <sys/socket.h>  /* basic socket definitions */
#include        <netinet/in.h>  /* sockaddr_in{} and other Internet
defns */
#include        <arpa/inet.h>   /* inet(3) functions */
#include        <unistd.h>
#include        <stdio.h>
#include        <stdlib.h>
#include        <string.h>
#include        <time.h>

#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        <sys/types.h>   /* basic system data types */
#include        <sys/socket.h>  /* basic socket definitions */
#include        <netinet/in.h>  /* sockaddr_in{} and other Internet
defns */
#include        <arpa/inet.h>   /* inet(3) functions */
#include        <unistd.h>
#include        <stdio.h>
#include        <stdlib.h>
#include        <string.h>

#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 <IPaddress>");

  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/



             reply	other threads:[~2003-05-09 19:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-09 19:46 file13 [this message]
2003-05-11  0:51 ` GNAT.Sockets Problems Craig Carey
2003-05-12 11:38 ` Simon Clubley
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox