comp.lang.ada
 help / color / mirror / Atom feed
* Winsock using Aonix ObjectAda - client/server
@ 2004-08-31 20:31 mla154
  0 siblings, 0 replies; only message in thread
From: mla154 @ 2004-08-31 20:31 UTC (permalink / raw)


My goal is to convert C code to Ada code of a client/server application. 
The Ada compiler I need to use to write the code is Aonix ObjectAda
version 7.2 Enterprise Edition for Windows NT.  The client code for C
works properly:
    #include <stdio.h>
    #include <winsock2.h>

    int main()
    {
	DWORD e;
	WSADATA ws;
        int sockfd;
        int len;
        struct sockaddr_in address;
        int result;
        char ch = 'A';

        /*  Create a socket for the client.  */
	e=WSAStartup(0x0101,&ws);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);

        /*  Name the socket, as agreed with the server.  */

        address.sin_family = AF_INET;
        address.sin_addr.s_addr = inet_addr("127.0.0.1");
        address.sin_port = htons(53);
        len = sizeof(address);

        /*  Now connect our socket to the server's socket.*/

        result = connect(sockfd, (struct sockaddr *) &address, len);

        if(result == -1) {
            perror("oops: client3");
            exit(1);
        }

        /*  We can now read/write via sockfd.  */

        send(sockfd, &ch, 1, 0);
        recv(sockfd, &ch, 1, 0);
        printf("char from server = %c\n", ch);
        closesocket(sockfd);
        exit(0);
    }

I came up with the following Ada code (does not compile without errors):
    with Ada.Text_Io;
    with Ada.Unchecked_Conversion;
    with Win32;
    with Win32.Winsock;

    procedure Client is
	type Pcch is access constant Win32.Char;
        type String9 is new String(1..9);
        package Tio renames Ada.Text_Io;
        package Ws  renames Win32.Winsock;
        function Sai_To_Sa is new Ada.Unchecked_Conversion(
            Ws.Sockaddr_In, Ws.Sockaddr);
        function String9_To_Pcstr is new  
Ada.Unchecked_Conversion(String9, Win32.Pcstr);
        function Ul_To_Sa1T is new Ada.Unchecked_Conversion(Ws.U_Long,
Ws.Struct_Anonymous1_T);
        Ch                  : aliased Win32.Char := 'A';
        Could_Not_Connect   : exception;
	Ch_Access           : Pcch;
        E, L1, Len, Result  : Win32.INT;
        Ws1                 : Ws.LPWSADATA := new Ws.WSADATA;
        Address             : Ws.Sockaddr_In;
        Sockfd              : Ws.SOCKET;
    begin
	Ch_Access := Ch'access;
        E := Ws.Wsastartup(16#0101#, Ws1);
        Sockfd := Ws.Socket_Func(Ws.AF_INET, Ws.SOCK_STREAM, 0);

        Address.Sin_Family := Ws.AF_INET;
        Address.Sin_Addr.S_Un.S_Un_B := (127,0,0,1);
        Address.Sin_Port := Ws.Htons(53);
        Address.Sin_Zero := (others => Win32.Nul);
        Len := Win32.INT(Address'Size);
        Result := Ws.Connect(Sockfd, new Ws.Sockaddr'(Sai_To_Sa(Address)),
Len);

        if Integer(Result)=-1 then
            Tio.Put_Line("error:  connect");
            raise Could_Not_Connect;
        end if;

        L1 := Ws.Send(Sockfd, Win32.Pcstr(Ch_Access), 1, 0);
        L1 := Ws.Recv(Sockfd, Win32.Pstr(Ch_Access), 1, 0);
        Tio.Put_Line("char from server = "&Character(Ch));
        L1 := Ws.Closesocket(Sockfd);
        exception
            when others =>
                null;
    end Client;

The compiler error I've been seeing is regarding the second parameter of
the ws.send function.  I need to choose something different for the second
parameter (I think).  All help is appreciated.




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-08-31 20:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-31 20:31 Winsock using Aonix ObjectAda - client/server mla154

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