GRC
HR
SCM
CRM
BI
Expand +


Article

 

CODE TEST

August 4, 2016

REPORT ZTCPCLIENT.
PARAMETERS: host type string,
            port type string,
            text type string DEFAULT 'Hello TCP, answer me!' LOWER CASE.
INITIALIZATION.
 host = cl_gui_frontend_services=>get_ip_address( ).
 concatenate '11' '88' into port.
DATA terminator type string.

CLASS apc_handler DEFINITION FINAL .
  PUBLIC SECTION.
    INTERFACES if_apc_wsp_event_handler.
    DATA message TYPE string.
ENDCLASS.

"Implement event handler to get events (on_start, on_message, on_close)
CLASS apc_handler IMPLEMENTATION.
  METHOD if_apc_wsp_event_handler~on_open.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler~on_message.
    TRY.
        message = i_message->get_text( ).
        "Cut of '0A' terminator
        data(len) = strlen( message ) - 1.
        message = message(len).
      CATCH cx_apc_error INTO DATA(apc_error).
        message = apc_error->get_text( ).
    ENDTRY.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler~on_close.
    message = 'Connection closed!'.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler~on_error.
  ENDMETHOD.
ENDCLASS.

CLASS apc_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS apc_demo IMPLEMENTATION.
  METHOD main.
    TRY.
        "Create event handler
        DATA(event_handler) = NEW apc_handler( ).

        "specify TCP frame information
        terminator = '0A'.

        DATA client type ref to if_apc_wsp_client.

        "create TCP client object and connect it to server
        client = cl_apc_tcp_client_manager=>create(
          i_host = host
          i_port = port
          i_frame = VALUE apc_tcp_frame(
            frame_type =
              if_apc_tcp_frame_types=>co_frame_type_terminator
            terminator =
              terminator )
          i_event_handler = event_handler ).

        client->connect( ).

        "Send initial 'hello' message from client
        DATA(message_manager) = CAST if_apc_wsp_message_manager(
          client->get_message_manager( ) ).
        DATA(message) = CAST if_apc_wsp_message(
          message_manager->create_message( ) ).
        DATA(binary_terminator) = CONV xstring( terminator ).
        DATA(binary_msg) = cl_abap_codepage=>convert_to( text ).
        CONCATENATE binary_msg binary_terminator
               INTO binary_msg IN BYTE MODE.
        message->set_binary( binary_msg ).
        message_manager->send( message ).

        "Wait for incoming messages from server
        CLEAR event_handler->message.
        WAIT FOR PUSH CHANNELS
             UNTIL event_handler->message IS NOT INITIAL UP TO 10 SECONDS.
        IF sy-subrc = 4.
          WRITE: 'No handler for APC messages registered!'.
        ELSEIF sy-subrc = 8.
          WRITE: 'Timeout occured!'.
        ELSE.
          WRITE: 'TCP client received: ', event_handler->message.
        ENDIF.
        client->close( i_reason = 'Application closed connection!' ).
      CATCH cx_apc_error INTO DATA(apc_error).
        WRITE: apc_error->get_text( ).
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  apc_demo=>main( ).

An email has been sent to:





 


COMMENTS

Please log in to post a comment.

No comments have been submitted on this article. Be the first to comment!


SAPinsider
FAQ