comp.lang.ada
 help / color / mirror / Atom feed
* gnat win sockets WSAGetLastError tasking
@ 2005-01-04 21:30 Björn Lundin
  0 siblings, 0 replies; only message in thread
From: Björn Lundin @ 2005-01-04 21:30 UTC (permalink / raw)
  To: comp.lang.ada

Hello all!
I'm writing a console socket application, with several tasks, and I run into
som strange behaviour when tasks are involved. Below is output from a test
program (also below). The first output is when the tasking part was commented 
out, the second output is from when tasking is part of the program. compiled 
with gnat 3.15p on a w2k box. (just gnatmake socket_connect.adb)

What I wonder is why I can get the correct errorcode by calling 
WSAGetLasterror (which is what Gnat.Sockets.Thin.Socket_Errno does, just 
pragma import) when having no tasks, but not when having tasks.

It gets even more strange (to me at least) when i realize that the associated 
error string to the exception has the correct error code in both cases.
By looking in the sources I see, that gnat is using 
Gnat.Sockets.Thin.Socket_Errno to create that error-string. 

Testing with Object Ada 7.2.2 (well basically the same) will get the 
correct errorcode no matter of tasking or not


regards
Björn Lundin






Without tasking
--------------------------------------------
Startup sockets
Get a socket
Try to connect!
GNAT.SOCKETS.SOCKET_ERROR: [10061] Connection refused
 10061 - Connection refused
Resolve_Exception - CONNECTION_REFUSED



With tasking
--------------------------------------------
Task running, Execute!
Startup sockets
Get a socket
Try to connect!
GNAT.SOCKETS.SOCKET_ERROR: [10061] Connection refused
 0 - Unknown system error
Resolve_Exception - CONNECTION_REFUSED


---------------------------------------------------------------------------
with Text_Io;
with Gnat.Sockets;
with Gnat.Sockets.Thin;
with Ada.Exceptions;

procedure Socket_Connect is
  Socket   : Gnat.Sockets.Socket_Type;
  Address  : Gnat.Sockets.Sock_Addr_Type;
  Error    : Integer := 0;
  --------------------------------------------------
  task type Run_Once is
    entry Execute;
  end Run_Once;
  --------------------------------------------------
  task body Run_Once is
  begin
    select
      accept Execute do
        text_io.put_line("Task running, Execute!");
      end Execute;
    or
      terminate;
    end select;
  end Run_Once;
  --------------------------------------------------


begin
  declare
    TmpTask : Run_Once;
  begin
    TmpTask.Execute;
  end;

  text_io.put_line("Startup sockets");
  Gnat.Sockets.Initialize;

  text_io.put_line("Get a socket");
  Gnat.Sockets.Create_Socket (Socket);

  text_io.put_line("Try to connect!");
  Address.Addr := Gnat.Sockets.Inet_Addr("127.0.0.1"); 
  Address.Port := 8000;
  Gnat.Sockets.Connect_Socket (Socket, Address);
  text_io.put_line("Connected!");

  text_io.put_line("Close socket!");
  Gnat.Sockets.Close_Socket (Socket);

  text_io.put_line("Shutdown sockets!");
  Gnat.Sockets.Finalize;

  text_io.put_line("Done!");
exception
  when E: others =>
   Text_IO.Put_Line(Ada.Exceptions.Exception_Name (E) & ": " & 
                    Ada.Exceptions.Exception_Message (E));
   
   Error := Gnat.Sockets.Thin.Socket_Errno;
   Text_IO.Put_Line(Integer'Image(Error) & " - " & 
            Gnat.Sockets.Thin.Socket_Error_Message (Error));
 
   Text_IO.Put_Line("Resolve_Exception" & " - " & 
     Gnat.Sockets.Error_Type'Image(Gnat.Sockets.Resolve_Exception(E)));

   Gnat.Sockets.Finalize;

end socket_connect;
---------------------------------------------------------------------------









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

only message in thread, other threads:[~2005-01-04 21:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-04 21:30 gnat win sockets WSAGetLastError tasking Björn Lundin

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