Showing results for 
Search instead for 
Do you mean 
Reply
Solved! Go to solution

Transferring data with the Socket interface (to a Java application)

Hi!

I am trying to connect PlantSim to a Java application but can't seem to get the Socket interface to work.

 

I am using PlantSim's Socket as client with the following configuration:

http://i.imgur.com/sTLxAHE.png

 

I am using this method to send a message:

http://i.imgur.com/ggBG5Ca.png

 

This is my Java code:

import java.io.*;
import java.net.*;

public class NetworkingServer {
	public static void main(String[] args) {
ServerSocket myServer = null; try { myServer = new ServerSocket(30000); } catch (IOException e) { e.printStackTrace(); } Socket clientSocket = null; BufferedReader incoming; String incomingText; try { clientSocket = myServer.accept(); incoming = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); while (true) { incomingText = incoming.readLine(); if (incomingText != null) { System.out.println(incomingText); } incomingText = null; } } catch (IOException e) { e.printStackTrace(); } } }

Now to my problem:

I launch my Java application (which is the server), then switch to PlantSim and activate my Socket.

When I run the method which is supposed to send the message nothing happens, but as soon as I deactivate the Socket the message is being delivered.

Why is that? I've read on this forum that there might be a problem with the Socket's buffersize. I tried to lower it and even waited for a long time to give PlantSim the chance to flush its output - but nothing happened.

 

If you know something about this problem, please enlighten me! Smiley Happy

Thanks in advance and kind regards,

Stefan

 

3 REPLIES

Re: Transferring data with the Socket interface (to a Java application)

Hello Stefan,

It should work as you expect. I don't see a reason why the transfer is delayed until the socket is closed.

In the attached model you can use two Plant Simulation processes to communicate with each other. For me the data is sent immediately after it is written to the socket. Please check if this is the case for you as well.

Regards,

Ralf

Re: Transferring data with the Socket interface (to a Java application)

Hi Ralf,

your example is the same as the one from PlantSim's documentation, which works just fine!

 

I modified my Java-code to this:

import java.io.*;
import java.net.*;

public class NetworkingServer {
	public static void main(String[] args) {
		
		ServerSocket myServer = null;		
		try {
			myServer = new ServerSocket(30000);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Socket clientSocket = null;		
		BufferedReader incoming;
		char incomingChar;
		final char lineSeparator = '^';
		String incomingText = "";
		try {			
			clientSocket = myServer.accept();
			incoming = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));			
			
			while (true) {
				incomingChar = (char) incoming.read();
				if (incomingChar != lineSeparator) {
					incomingText += incomingChar;
				} else {					
					System.out.println(incomingText);
					incomingText = "";
				}
			}					
		} catch (IOException e) {
			e.printStackTrace();
		}			
	}
}

Message transportation is instantaneous when I use this workaround... I'm afraid PlantSim doesn't attach a proper lineseparator to the end of a message? Do you have any experience with sending and receiving strings via the TCP Socket to and from another program/application?

I can't be the first person to try to communicate with PlantSim using the TCP Socket Smiley Very Happy

Solution
Solution
Accepted by topic author stefan_o
‎11-08-2016 06:19 AM

Re: Transferring data with the Socket interface (to a Java application)

Hello Stefan,

Plant Simulation just sends the data you specify. If you want to have a seperator you have to add it yourself.

Since the communication between different Plant Simulation processes works fine, I guess the problem is in the Java code. Unfortunately I cannot help you here, since I'm not a Java expert.

Regards,

Ralf