1. Home
    1. Dashboard
    2. Search
  2. Forum
    1. Unresolved Threads
    2. Members
      1. Recent Activities
      2. Users Online
      3. Team Members
      4. Search Members
      5. Trophys
  3. Articles
  4. Blog
  5. Videos
  6. Jobs
  7. Shop
    1. Orders
  • Login or register
  • Search
This Thread
  • Everywhere
  • This Thread
  • This Forum
  • Articles
  • Pages
  • Forum
  • Blog Articles
  • Products
  • More Options
  1. Robotforum - Support and discussion community for industrial robots and cobots
  2. Forum
  3. Cobot Help and Discussion Center
  4. KUKA LBR IIWA
Your browser does not support videos RoboDK Software for simulation and programming
Visit our Mainsponsor
IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Sponsored Ads

LBR iiwa TCP/IP communication issue

  • Giuseppe Cappellacci
  • March 18, 2019 at 5:29 PM
  • Thread is Unresolved
  • Giuseppe Cappellacci
    Trophies
    3
    Posts
    1
    • March 18, 2019 at 5:29 PM
    • #1

    Hi everyone,

    I am working with KUKA LBR iiwa by using SunRise Workbench and I need to connect the robot with another device using a TCP/IP protocol. I am writing a TCP/IP server and a TCP/IP client using java sockets. Actually, when I use the robot as the server, I am able to establish a connection using my laptop as client. But when I try the opposite task (robot-client and laptop-server), it says "Connection timed out: connect".

    Is there anyone who can give me any advise?

    Thanks in advance,

    Here is the client code:

    public class LBR_Client {
    private PrintStream out = null;
    private BufferedReader in = null;
    @Override
    public void initialize() {
    Socket socket = null;
    try {
    socket = new Socket("172.31.1.14", 30001);

    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()), true);
    out.println("client message");

    System.out.print("server message: " + in);

    out.close();
    in.close();
    socket.close();

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

    @Override
    public void run() {
    }
    }

    And here's the server code:

    public class MyServer {
    public static void main(String args[]) throws IOException{
    Socket client;
    BufferedReader in = null;
    PrintStream out = null;
    try {
    ServerSocket server = new ServerSocket(30001);
    client = server.accept();
    server.close();
    in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    out = new PrintStream(new BufferedOutputStream(client.getOutputStream()), true);
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

    String userInput;
    userInput = stdIn.readLine();
    out.println(userInput);

    System.out.print("Client message: " + in);

    stdIn.close();
    out.close();
    in.close();

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

  • DrG
    Trophies
    3
    Posts
    39
    • March 25, 2019 at 5:54 PM
    • #2

    Hi Giuseppe Capellacci,

    Quote

    I am writing a TCP/IP server and a TCP/IP client using java sockets. Actually, when I use the robot as the server, I am able to establish a connection using my laptop as client. But when I try the opposite task (robot-client and laptop-server), it says "Connection timed out: connect".


    Hm.. the communication should be "symmetric" feasible.

    I would check the Network/Firewall Settings on the Laptop, since a lot of those Firewall-Systems distinguish between outbound and inbound traffic - aka: who is the Server and who is the client.
    Therefore, as you open the socket ports in your Firewall, you can configure for "incoming" or "outgoing" in separate views.

    For an simple test: Deactivate the Firewall of the Laptop for the Duration of your test, and rerun the Client/Server Programs.


    A quick glance on your code: It seems ok, unless:
    I personally would not immediately close (server.close()) the Server socket directly after first Connection of the Client ("Server.accept()"), but keep the Server running in an endless Loop reentering "accept()" as soon as the Client finishes - this saves a lot of Manual "restart" work.

    Therefore: In your program, the Server MUST be started before the clienat - and each time a Client connects...
    ... you could reduce this pain..

    Hope that helps,
    DrG

  • matteopantano
    Trophies
    3
    Posts
    13
    • April 16, 2019 at 6:29 PM
    • #3

    I have the same issue. Did you solve?

  • kiiwa
    Reactions Received
    9
    Trophies
    4
    Posts
    369
    • April 16, 2019 at 7:11 PM
    • #4

    You're closing the server socket, too early, before even reading from the input stream. And you're printing the object BufferedReader in, instead of reading from the stream. google some simple TCP server/client examples and build on them.

    Code
    System.out.print("server message: " + in);
  • kiiwa
    Reactions Received
    9
    Trophies
    4
    Posts
    369
    • April 16, 2019 at 7:22 PM
    • #5

    Here's a simple example that works.


    Client on Robot:

    Java
    package apps; 
     import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket; 
    import javax.inject.Inject; 
    import com.kuka.roboticsAPI.applicationModel.RoboticsAPIApplication; 
    import com.kuka.roboticsAPI.controllerModel.Controller;
    
    
    public class LBR_Client extends RoboticsAPIApplication {
    	@Inject
    	private Controller contoller;  
    	Socket clientSocket = null;
    
    
    	@Override
    	public void initialize() { 
    		contoller = (Controller) getContext().getControllers().toArray()[0]; 
    	}
    
    
    	@Override
    	public void run() 
    	{
    		try {       
    			String sentence;
    			String modifiedSentence;
    			BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    			clientSocket = new Socket("172.31.1.135", 30000);
    			DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    			sentence = "tcp message";
    			outToServer.writeBytes(sentence + '\n');
    			modifiedSentence = inFromServer.readLine();
    			System.out.println("FROM SERVER: " + modifiedSentence);
    			clientSocket.close(); 
    		} catch (IOException e) {  
    			e.printStackTrace();            
    		}  
    	}  
    	// To liberate the port 
    	@Override
    	public void dispose()
    	{
    		try { 
    			clientSocket.close();  
    			System.out.println("Socket closed"); 
    		}catch (Exception e)
    		{ e.printStackTrace(); } 
    		super.dispose(); 
    	} 
    
    
    }
    Display More

    Server on PC:

    Java
    package tcp;
    
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.*;
    
    
    class MyServer {
     public static void main(String argv[]) throws Exception {
      String clientSentence;
      String capitalizedSentence;
      ServerSocket welcomeSocket = new ServerSocket(30000);
    
    
      while (true) {
       Socket connectionSocket = welcomeSocket.accept();
       BufferedReader inFromClient =  new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
       DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
       clientSentence = inFromClient.readLine();
       System.out.println("Received: " + clientSentence);
       capitalizedSentence = clientSentence.toUpperCase() + '\n';
       outToClient.writeBytes(capitalizedSentence);
      }
     }
    }
    Display More
  • matteopantano
    Trophies
    3
    Posts
    13
    • April 17, 2019 at 8:45 AM
    • #6

    Thanks kiiwa. However, the problem is still not solved.

    My code is the following:

    Code
    public void socketConnection()  // Connecting to server at 172.31.1.50 Port:30001
    	{
    		System.out.println("Connecting to server at 172.31.1.50 Port:30001");
    
    		while (true){
    			try{
    			    skt = new Socket("172.31.1.50", 30001); 
    
    			    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    			    System.out.println("FROM SERVER: " + inFromServer.readLine());
    
    			    System.out.println("KUKA iiwa is connected to the server.");
    		    	    break;
    			}
    			catch(IOException e){
    		            System.out.println("ERROR connecting to the server!");
    		            e.printStackTrace();
    			}
    		}
    
    
    	    try{
    	    	outputStream = new PrintWriter(skt.getOutputStream(), true);
    	    	inputStream = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    	    	RUN = true;
    	    }
    	    catch(IOException e)
    	    {
    	    	System.out.println("Error creating inPort and outPort");
    	    	e.printStackTrace();
    	    }
    	}
    Display More

    The code then throws the first exception.

    In attachment a Wireshark capture of the network.

    KUKA IP: 172.31.1.147
    PC IP: 172.31.1.50

    BR,

    Matteo

    Images

    • connection_issue_iiwa.PNG
      • 37.85 kB
      • 1,917 × 250
      • 21

    Files

    connection_issue_iiwa.PNG_thumb 4.19 kB – 275 Downloads
  • matteopantano
    Trophies
    3
    Posts
    13
    • April 17, 2019 at 10:30 AM
    • #7

    Solved, it was a problem with firewall rules.

    For the sake of clarity someone should or disable firewall or create an inbound policy that allows communication from iiwa port to the port of the laptop.

  • kiiwa
    Reactions Received
    9
    Trophies
    4
    Posts
    369
    • April 17, 2019 at 11:36 AM
    • #8

    Good!
    Yes, the firewall is a pain in the neck sometimes.

  • rreype314
    Trophies
    3
    Posts
    28
    • August 6, 2019 at 2:06 AM
    • #9

    As a note on the same topic: port numbers are also a source of problems, you can only use port numbers between 30,000-30,010

  • JM212
    Reactions Received
    1
    Trophies
    2
    Posts
    43
    • December 4, 2020 at 8:25 PM
    • #10
    Quote from kiiwa

    Here's a simple example that works.


    Client on Robot:

    Java
    package apps; 
     import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.Socket; 
    import javax.inject.Inject; 
    import com.kuka.roboticsAPI.applicationModel.RoboticsAPIApplication; 
    import com.kuka.roboticsAPI.controllerModel.Controller;
    
    
    public class LBR_Client extends RoboticsAPIApplication {
    	@Inject
    	private Controller contoller;  
    	Socket clientSocket = null;
    
    
    	@Override
    	public void initialize() { 
    		contoller = (Controller) getContext().getControllers().toArray()[0]; 
    	}
    
    
    	@Override
    	public void run() 
    	{
    		try {       
    			String sentence;
    			String modifiedSentence;
    			BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    			clientSocket = new Socket("172.31.1.135", 30000);
    			DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    			sentence = "tcp message";
    			outToServer.writeBytes(sentence + '\n');
    			modifiedSentence = inFromServer.readLine();
    			System.out.println("FROM SERVER: " + modifiedSentence);
    			clientSocket.close(); 
    		} catch (IOException e) {  
    			e.printStackTrace();            
    		}  
    	}  
    	// To liberate the port 
    	@Override
    	public void dispose()
    	{
    		try { 
    			clientSocket.close();  
    			System.out.println("Socket closed"); 
    		}catch (Exception e)
    		{ e.printStackTrace(); } 
    		super.dispose(); 
    	} 
    
    
    }
    Display More

    Server on PC:

    Java
    package tcp;
    
    
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    import java.net.*;
    
    
    class MyServer {
     public static void main(String argv[]) throws Exception {
      String clientSentence;
      String capitalizedSentence;
      ServerSocket welcomeSocket = new ServerSocket(30000);
    
    
      while (true) {
       Socket connectionSocket = welcomeSocket.accept();
       BufferedReader inFromClient =  new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
       DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
       clientSentence = inFromClient.readLine();
       System.out.println("Received: " + clientSentence);
       capitalizedSentence = clientSentence.toUpperCase() + '\n';
       outToClient.writeBytes(capitalizedSentence);
      }
     }
    }
    Display More

    Is it a safe assumption that the Client and server are interchangable on pc and robot?

  • Seulki
    Reactions Received
    8
    Trophies
    3
    Posts
    84
    • December 8, 2020 at 8:44 AM
    • #11
    Quote from JM212

    Is it a safe assumption that the Client and server are interchangable on pc and robot?

    Yes, as long as you use right ports.

Advertising from our partners

IRBCAM
Robotics Channel
Robotics Training
Advertise in robotics
Advertise in Robotics
Advertise in Robotics

Job Postings

  • Anyware Robotics is hiring!

    yzhou377 February 23, 2025 at 4:54 AM
  • How to see your Job Posting (search or recruit) here in Robot-Forum.com

    Werner Hampel November 18, 2021 at 3:44 PM
Your browser does not support videos RoboDK Software for simulation and programming

Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000

Thread Tag Cloud

  • abb
  • Backup
  • calibration
  • Communication
  • CRX
  • DCS
  • dx100
  • dx200
  • error
  • Ethernet
  • Ethernet IP
  • external axis
  • Fanuc
  • help
  • hmi
  • I/O
  • irc5
  • IRVIsion
  • karel
  • kawasaki
  • KRC2
  • KRC4
  • KRC 4
  • KRL
  • KUKA
  • motoman
  • Offset
  • PLC
  • PROFINET
  • Program
  • Programming
  • RAPID
  • robodk
  • roboguide
  • robot
  • robotstudio
  • RSI
  • safety
  • Siemens
  • simulation
  • SPEED
  • staubli
  • tcp
  • TCP/IP
  • teach pendant
  • vision
  • Welding
  • workvisual
  • yaskawa
  • YRC1000
  1. Privacy Policy
  2. Legal Notice
Powered by WoltLab Suite™
As a registered Member:
* You will see no Google advertising
* You can translate posts into your local language
* You can ask questions or help the community with your knowledge
* You can thank the authors for their help
* You can receive notifications of replies or new topics on request
* We do not sell your data - we promise

JOIN OUR GREAT ROBOTICS COMMUNITY.
Don’t have an account yet? Register yourself now and be a part of our community!
Register Yourself Lost Password
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on Google Play
Robotforum - Support and discussion community for industrial robots and cobots in the WSC-Connect App on the App Store
Download