Open socket urcap send urscript

Hi,
I would like to send urscript to sendoary port (30002) and open a socket and send data to another port (eg 11111). The program is not running, when I would like to send.
My code is:

Socket s = new Socket(“127.0.0.1”, 30002);
if(s.isConnected())
System.outprintln(“ok”);
DataOutputStrem out = new DataOutputStream(s.getOutputStream());
String cmd = “def codea():\n”
+"\tsocket_open(“192.168.1.127”, 11111)\n"
+"\tsocket_send_string(“oksend”)\nend\n";
out.writeUTF(cmd);
out.flush();

out.close();
s.close();

But its only connected to 11111 when a robot program is running. I would like to connect, when the robot program is stopped.
Another problem is it isnt send oksend string, only connect.

Thanks to read.

Hello,

you can try using socket_send_line("…") instead of socket_send_string("…") or add LF & CR.

Hello,
I tried that, but unfortunately its not working. If I use it, the UR is not connected the server.

Having no connection (the code not being executed) using the other method could be an indication for some typo. Adding a popup or similar in the script code could help you exclude this possibility.

Using a while-loop to assure you, that the connection is established before the program continues, could also help out.

I added popup, and it is showing when I started the code. But the connection is since not open (add the popup row after the socket_open)

Could you post the script you are sending, which does not open the connection?
What Robot & SW Version are you using?

I use the official virtualbox simulation with ubuntu and ur3.12 software version (lastest version).
My code:

try {
	
	
	Socket sc = new Socket("127.0.0.1", 30002);
	if(sc.isConnected()) {
		System.out.println("OK");
	}
	DataOutputStream out;
	out = new DataOutputStream(sc.getOutputStream());
	

	String command = "def ezegyprogram():\n"
			+ "\twhile i_var_1 > 0:\n"
			+ "\t\tsocket_open(\"192.168.1.166\", 55555)\n"
			+ "\t\tsocket_send_line(\"ok\")\n"
			+ "\t\tpopup(\"ok\")\n"
			+ "\tend\n"
			+ "end\n";

	System.out.println(command);
	out.writeUTF(command);
	out.flush();

	out.close();
	sc.close();

	
	
} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();

}

The i_var_1 is 10, its a global variable in UR

I had problem trying to connect socket with the URSim. I think it Just works with the real robot.