Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
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.

 

 

 
3 Comments
Labels in this area