Implementing a Listener

A Listener opens and prepares the sockets to listen for incoming socket connection requests at the TCP layer. This section describes how to implement a Listener.

Required background

Before you start, you must understand:

  • Socket programming and configurations.

Procedure

  1. Create RCONNECTION().

    1. Create a session with the socket server:

      RSocketServ::Connect;
    2. Open a TCP socket on the new session:

      RSocket::Open(iSS, KAfInet, KSockStream, KProtocolInetTcp,iSubconn);

    Optionally, to open a new connection over a session, perform the following steps:

    1. Create a new session with the socket server:

      RSocketServ::Connect();
    2. Open a new connection over the session:

      RCONNECTION::Open(iSS);
    3. Modify the commsDB settings:

      TCommDbConnPref prefs;
              prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);

    4. Start the connection:

      RCONNECTION::Start(prefs);
    5. Open the default sub-connection over the new connection:

      RSubConnection::Open(iSS, RSubConnection::EAttachToDefault, iConn);
    6. Open a TCP socket on the sub-connection:

      RSocket::Open(iSS, KAfInet, KSockStream, KProtocolInetTcp,iSubconn);

      The socket type must be Stream Socket.

  2. Wait and accept new connection.

    1. Bind the socket to the appropriate IP address and port number. For example, the port number for PTP-IP is 1540.

      RSocket::Bind(anyAddrOnPort);
      RSocket::Listen(queuesize);
    2. Call the appropriate Listen() API on the socket.

  3. Acquire the socket.

    To get a new blank socket, use NewSocketL(), This method returns a reference to the new blank socket.

    RSocket& socket(MPTPIPController&::NewSocketL());

    Note: There is a limit to the number of sockets returned in each session.

    You must then call the Accept() API using the blank socket to accept and bind the remote connection request with the given socket.

    RSocket::Accept(socket, iStatus);