[omniORB] Connecting to a remote server

Armin Gerritsen armin.gerritsen@philips.com
Thu, 8 Feb 2001 11:46:51 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_01BE_01C091C4.D376CE10
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

>I am getting started with CORBA and I am writing a small app (echo type of
>thing) using omniORBpy. My question is, how can I connect to
>a remote server knowing the IP address of the remote host. (things are not
>as obvious as with socket !)
>
>BTW, a sample code would be of great help.


What I sometimes do is create a sort of init-function which opens a standard
socket connection and uses that to transfer the IOR-string.

I'm not trying to tell that that is the ideal way of working with CORBA,
since you do limit yourself, but if you are primairly interested in using
CORBA because it is easy to program network-stuff with it helps.

I attatched an example of a C++ Win32 server with a C++ Win32/MFC client. I
included the two functions that should give you the idea. I think it won't
be that the idea will be much different with omniORBpy (else please correct
me.)

Hope that helps,

Regards,

Armin
--
Philips Semiconductors B.V.
Systems Laboratory Eindhoven (PS-SLE)

Building BE235, Hurksestraat 19,
P.O.Box 218, 5600MD Eindhoven,
The Netherlands
E-mail: Armin.Gerritsen@philips.com


------=_NextPart_000_01BE_01C091C4.D376CE10
Content-Type: text/plain;
	name="Example.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="Example.txt"

I used a thread on the Win32 based server to listed for requests:

 DWORD dwThreadID ;
 CreateThread(NULL, 0, &SocketConnectThread, (LPVOID)szIOR , 0, =
&dwThreadID) ;


DWORD WINAPI SocketConnectThread(LPVOID szServerIOR)
{
 // Init WinSock
 WSADATA  WSAData ;
 if ( WSAStartup(MAKEWORD(1, 1), &WSAData) !=3D 0 )
 {
  printf("Error: Failed to initialise Winsock.\nErrorcode =3D %d.\n", =
WSAGetLastError() ) ;
  printf("Clients will not be able to get the IOR string via a =
socket-connection.\n") ;
  return -1 ;
 }
=20
 // Create server-socket
 SOCKET lSck ;
 if ( (lSck=3Dsocket(PF_INET,SOCK_STREAM,0)) =3D=3D INVALID_SOCKET )
 {
  printf("Error: Failed to create a listening socket.\nErrorcode =3D  =
%d.\n", WSAGetLastError() ) ;
  printf("Clients will not be able to get the IOR string via a =
socket-connection.\n") ;
  return -1 ;
 }
=20
 // Bind server-socket
 SOCKADDR_IN sin;
 memset(&sin, 0, sizeof(sin)) ;
 sin.sin_family =3D AF_INET ;
 sin.sin_port =3D htons(iPort) ;
 sin.sin_addr.s_addr =3D INADDR_ANY ;
 if ( bind(lSck, (const struct sockaddr *)&sin, sizeof(sockaddr)) !=3D 0 =
)
 {
  printf("Error: Failed to bind the listening socket.\nErrorcode =3D =
%d.\n", WSAGetLastError() ) ;
  printf("Clients will not be able to get the IOR string via a =
socket-connection.\n") ;
  return -1 ;
 }
=20
 // Listen to socket
 if ( listen(lSck, 0) !=3D 0 )
 {
  printf("Error: Failed to listen to the socket.\nErrorcode =3D %d.\n", =
WSAGetLastError());
  printf("Clients will not be able to get the IOR string via a =
socket-connection.\n") ;
  return -1 ;
 }
=20
 // Accept connections
 printf("Server is ready to accept IOR-request on port %d ...\n\n", =
iPort ) ;
 SOCKET cSck ;
 int nlen =3D 0 ;
 while ( TRUE )
 {
  begin:
 =20
  // Wait and accept
  nlen =3D sizeof(sockaddr) ;
  if ( (cSck=3Daccept(lSck, (struct sockaddr *)&sin, &nlen)) =3D=3D -1 )
  {
   printf("Oops!\nError: Failed to accept a call.\nErrorcode =3D %d.\n", =
WSAGetLastError() ) ;
   goto begin ;
  }
 =20
  nlen =3D sizeof(sockaddr) ;
  getpeername(cSck, (struct sockaddr *)&sin, &nlen);
  printf("A client connected from address %03d.%03d.%03d.%03d to get the =
IOR-string.\n",
         (unsigned)sin.sin_addr.S_un.S_un_b.s_b1,
  		 (unsigned)sin.sin_addr.S_un.S_un_b.s_b2,
  		 (unsigned)sin.sin_addr.S_un.S_un_b.s_b3,
  		 (unsigned)sin.sin_addr.S_un.S_un_b.s_b4 ) ;
 =20
  // Sending IOR to client
  int Bytes =3D 0 ;
  char buf[2048] ;
  strcpy( buf, (char*)szServerIOR ) ;
  while ( ( Bytes=3Dsend(cSck, buf, sizeof(buf), 0) ) > 0 )
  {
   if ( Bytes<1 )
   {
    printf("Error: Failed to send the whole IOR buffer to the =
client.\nErrorcode =3D %d.\n", WSAGetLastError() ) ;
    closesocket(cSck);
    goto begin ;
   }
  }
  closesocket(cSck);
 =20
 } //end WHILE(TRUE)
=20
 return 0 ;
}


And on the client side as MFC application:


BOOL SrvControl::GetIORFromSocket( CString& cstrIOR )
{
 // Init socket-stuff
 WSADATA WSAData ;
 if ( 0 !=3D WSAStartup(MAKEWORD(1, 1), &WSAData) )
 {
  CString cstrError ;
  cstrError.Format( "Error: Failed to initialise Winsock.\nErrorcode =3D =
%d.\n", WSAGetLastError() ) ;
  ::MessageBox(NULL, cstrError, "Oops!", MB_OK | MB_ICONSTOP ) ;
  return FALSE ;
 }


 // Create socket
 SOCKET cSck ;
 if ( ( cSck =3D socket(PF_INET,SOCK_STREAM,0)) =3D=3D INVALID_SOCKET )
 {
  CString cstrError ;
  cstrError.Format( "Error: Failed to create a client socket.\nErrorcode =
=3D %d.\n", WSAGetLastError() ) ;
  ::MessageBox(NULL, cstrError, "Oops!", MB_OK | MB_ICONSTOP ) ;
  return FALSE ;
 }
=20
 // Connect
 int Bytes =3D 0 ;
 SOCKADDR_IN sin ;
 memset(&sin, 0, sizeof(sin) ) ;
 sin.sin_family =3D AF_INET ;
 sin.sin_port =3D htons(m_iPort) ;
 sin.sin_addr.s_addr =3D GetAddr(m_cstrHost) ;
 if ( (sin.sin_addr.s_addr =3D=3D INADDR_ANY) || (sin.sin_addr.s_addr =
=3D=3D INADDR_NONE) )
 {
  CString cstrError ;
  cstrError.Format( "Error: Host lookup to \"%s\" failed.\nErrorcode =3D =
%d.\n", m_cstrHost, WSAGetLastError() ) ;
  ::MessageBox(NULL, cstrError, "Oops!", MB_OK | MB_ICONSTOP ) ;
  return FALSE ;
 }
=20
 // Connect     =20
 int nlen =3D sizeof(sockaddr) ;
 if ( connect(cSck, (struct sockaddr *)&sin, nlen) !=3D 0 )
 {
  CString cstrError ;
  cstrError.Format( "Error: Failed to connect to the server =
socket.\nErrorcode =3D %d.\n", WSAGetLastError() ) ;
  ::MessageBox(NULL, cstrError, "Oops!", MB_OK | MB_ICONSTOP ) ;
  return FALSE ;
 }
=20
 // Get IOR
 char buf[1024] ;
 while( (Bytes=3Drecv(cSck, buf, sizeof(buf), 0) ) > 0 )
 {
  if ( -1=3D=3DBytes )
  {
   CString cstrError ;
   cstrError.Format( "Error: Failed to receive a full IOR buffer from =
the server.\nErrorcode =3D %d.\n",
                     WSAGetLastError() ) ;
   ::MessageBox(NULL, cstrError, "Oops!", MB_OK | MB_ICONSTOP ) ;
   closesocket(cSck);
   return FALSE ;
  }
        =20
  closesocket(cSck);
 }
=20
 cstrIOR.Format("%s", buf ) ;
=20
 return TRUE ;
}

// The following code is taken from sample code at =
http://www.sockets.com
/*-----------------------------------------------------------
 * Function: GetAddr()
 *
 * Description: Given a string, it will return an IP address.
 *   - first it tries to convert the string directly
 *   - if that fails, it tries to resolve it as a hostname
 *
 * WARNING: gethostbyname() is a blocking function
 */
DWORD GetAddr( const char *szHost )
{
 LPHOSTENT lpstHost;
 u_long lAddr =3D INADDR_ANY;
=20
 /* check that we have a string */
 if (*szHost)
 {
  /* check for a dotted-IP address string */
  lAddr =3D inet_addr (szHost) ;
 =20
  /* If not an address, then try to resolve it as a hostname */
  if ( (lAddr=3D=3DINADDR_NONE) && (strcmp(szHost, "255.255.255.255")) )
  {
   lpstHost =3D gethostbyname(szHost) ;
   if (lpstHost)
   {
    /* success */
    lAddr =3D *( (u_long FAR *) (lpstHost->h_addr) ) ;
   }
   else
   { =20
    lAddr =3D INADDR_ANY ;  /* failure */
   }
  }
 }
=20
 return lAddr ;=20
}

------=_NextPart_000_01BE_01C091C4.D376CE10--