Skip to Content
Author's profile photo Ashish Goyal

SAP PI Client Socket Connection to TCP/IP Socket Server

SAP PI Client Socket Connection to TCP/IP Socket Server

This blog post shows how to connect to a TCP/IP socket server from SAP PI.

High Level Flow:

  1. PI acts as a socket client in this scenario.
  2. SAP PI mapping invokes a UDF.
  3. UDF receives a input parameter from the source message.
  4. UDF invokes a socket client connection and connects to socket server.
  5. Socket server receives the input and returns a capitalized string.
  6. Return string is mapped to the target file in target message.

I have used the base code provided Dave Drager in his awesome blog https://systembash.com/a-simple-java-tcp-server-and-tcp-client/. Thanks Dave. Code for server was used as-is from Dave’s blog, however client code was modified a bit for usage in PI.

Here I created the socket server using NWDI and in my environment PI can connect to my laptop on the company network. In your case you may have to open firewalls to connect to the system wherever your test socket server is running.

Step1: Create source and Target Data Type, Message Type and Service Interfaces.

Step2: Create a UDF (SocClient in this example) with the following code. Make sure to include java.net.* in UDF libraries section.

 

Step3: Start the Socket server in NWDI

Step4: Test the mapping in PI

Server logs in NWDI will confirm the test message received:

 

Code for UDF:

public String SocClient(String var1, Container container) throws StreamTransformationException{

String sentence;

String modifiedSentence = “”;

try

{

InputStream isr = new ByteArrayInputStream(var1.getBytes());

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(isr));

Socket clientSocket = new Socket(“xxx.xxx.xxx.xxx”, 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + ‘\n’);

modifiedSentence = inFromServer.readLine();

clientSocket.close();

}

 

catch (UnknownHostException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

 

return    modifiedSentence;

}

 

This is my first blog and would appreciate feedback.

 

 

 

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Iñaki Vila
      Iñaki Vila

      Great blog Ashish Goyal , i miss the old feature to bookmark it.
      TCP/IP socket connectivity is a really question asked several times in this forum and you collaboration is very helpful. Have you thought to do an adapter instead of a java mapping?

      Regards.

      Author's profile photo Ashish Goyal
      Ashish Goyal
      Blog Post Author

      Thanks Inaki

      This was more of a POC and not the final solution in my case. I am open to writing a custom adapter module as well though this would be my first time.

      I also have a requirement to where PI will need to act as socket server and I believe adapter module is necessary in such case.

      Thanks,
      Ashish

      Author's profile photo Matheus Vinicius Munhoz Bruno
      Matheus Vinicius Munhoz Bruno

      How do you set up the receiver channel?