We are a team working on a remote module for Rotor. As such we need to
establish a connection with a remote server (although this server is
hosted at localhost at the moment). We decided to use the sockets library,
as it comes in the PAL specification. But no matter how we try, we get
a "connection refused" error. We are using a server implemented in C#,
using System.Net.Sockets and we can get a connection if we try it on a
standalone client (also in C#). This connection is established in a thread
we created to manage connections and replies assynchronously. We've tried
almost everything we came up to, but we get no results, and of course as
the project consists in a remote module, without a connection we have no
project.

We're posting the code we are using to establish the connection so far, if
anyone notices an error or knows something about this problem please let
us know.

This is the code (preliminary version of the helper function for the
thread start):

ULONG __stdcall GCHeap::RemoteSenderThreadStart(void *args) {
    WORD VersionRequested = MAKEWORD(2, 2);
    WSADATA WsaData;
    int SocketID, error;
    struct sockaddr_in server_addr;

    BOOL    ok = RemoteSenderThread->HasStarted();

    _ASSERTE(ok);
    _ASSERTE(GetThread() == RemoteSenderThread);

 /* initialize to use winsock2.dll */
 error = WSAStartup(VersionRequested, &WsaData);

 if(error != 0){
  ok = FALSE;
  _ASSERTE(!"Failed to find a usable WinSock DLL!");
 }

 /* Confirm that the WinSock DLL supports 2.2. */
 if(LOBYTE( WsaData.wVersion ) != 2 || HIBYTE( WsaData.wVersion ) !
= 2 ){
  ok = FALSE;
  error = WSACleanup();
  if(SOCKET_ERROR == error){
   ok = FALSE;
   _ASSERTE(!"Failed to call WSACleanup API!");
  }
 }

 SocketID = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

 if(SocketID == INVALID_SOCKET){
  ok = FALSE;
  error = WSACleanup();
  if(error == SOCKET_ERROR){
   ok = FALSE;
   _ASSERTE(!"Failed to call WSACleanup API!");
  }
 }

 struct hostent *hp;
 hp = gethostbyname("127.0.0.1");

 server_addr.sin_family = AF_INET;
 server_addr.sin_port = 8000;
 server_addr.sin_addr = *(struct in_addr*)hp->h_addr;
 memset( &(server_addr.sin_zero), 0, 8);

 error = connect(SocketID, (struct sockaddr *)&server_addr, sizeof
(server_addr));
 if(error == SOCKET_ERROR){
  ok = FALSE;
  fprintf(stdout, "Error Code: %d\n", GetLastError());
  error = closesocket(SocketID);
  if(error == SOCKET_ERROR){
                ok = FALSE;
  }
  error = WSACleanup();
  if(error == SOCKET_ERROR){
   ok = FALSE;
  }
 }

 if (ok){
        EE_TRY_FOR_FINALLY{
   RemoteSenderThread->SetBackground(TRUE);
   while (!fQuitSender){
    // Wait for work to do...

                _ASSERTE(RemoteSenderThread->PreemptiveGCDisabled());

    RemoteSenderThread->EnablePreemptiveGC();
                WaitForRemoteEvent (GCHeap::hEventToSender);
                RemoteSenderThread->DisablePreemptiveGC();


   }
  }
  EE_FINALLY{
   if (GOT_EXCEPTION())
                _ASSERTE(!"Exception in the RemoteSender thread!");
  }
  EE_END_FINALLY;
 }

 WSACleanup();
 RemoteSenderThread->EnablePreemptiveGC();

        return 0;
}

Thank you for your help. If anyone could come up with the problem, or even
give us an alternative to sockets to establish the connection, it'd be
very welcome.

R.S.V.P.

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to