# TCP/IP (network) communication

Some of the newer Zaber devices (e.g. X-MCC) support ethernet and the TCP/IP protocol.
Alternatively, the serial port connection can be forwarded to TCP/IP using a 3rd party ethernet-serial converter or a simple program.
In this scenario, the Zaber device (or a hardware converter) acts as a TCP server and the library as a TCP client.

In order to communicate with a TCP server, you need to know its hostname or IP address
and on what TCP port the server is listening.

For Zaber devices, the hostname is in format `zaber-<serial number>.local`,
or you can obtain the IP address from the `comm.en.ipv4.address` setting.
The TCP port choice depends on your setup.
If you'd like to communicate with the entire device chain, choose port `55550` represented by the `TCP_PORT_CHAIN` constant.
If you'd like to communicate only with the ethernet device, choose port `55551` represented by the `TCP_PORT_DEVICE_ONLY` constant.
Using `TCP_PORT_DEVICE_ONLY` is typically faster as the device does not need to forward the commands to the rest of the chain over a slower serial connection.
For more details, see the [Protocol Manual](https://www.zaber.com/protocol-manual#topic_setting_comm).
For a 3rd party converter, the manufacturer specifies the hostname and the TCP port.

In the following example, the device has a hostname "zaber-12345.local".

**Python:**

```py
with Connection.open_tcp("zaber-12345.local", Connection.TCP_PORT_CHAIN) as connection:
    device_list = connection.detect_devices()
    print("Found {} devices".format(len(device_list)))

    # rest of your program goes here (indented)
```

**C#:**

```cs
using (var connection = Connection.OpenTcp("zaber-12345.local", Connection.TcpPortChain)) {
    var deviceList = connection.DetectDevices();
    Console.WriteLine($"Found {deviceList.Length} devices.");

    // rest of your program goes here
}
```

**JavaScript:**

```js
const connection = await Connection.openTcp('zaber-12345.local', Connection.TCP_PORT_CHAIN);
try {
    const deviceList = await connection.detectDevices();
    console.log(`Found ${deviceList.length} devices.`);

    // rest of your program goes here
} finally {
    // close the port to allow Node.js to exit
    await connection.close();
}
```

**Java:**

```java
try (Connection connection = Connection.openTcp("zaber-12345.local", Connection.TCP_PORT_CHAIN)) {
    Device[] deviceList = connection.detectDevices();
    System.out.println(String.format("Found %d devices.", deviceList.length));

    // rest of your program goes here
}
```

**MATLAB (legacy):**

```matlabjava
try
    connection = Connection.openTcp('zaber-12345.local', Connection.TCP_PORT_CHAIN);
    deviceList = connection.detectDevices();
    fprintf('Found %d devices.\n', deviceList.length);

    % The rest of your program goes here

catch exception
    disp(getReport(exception));
end
connection.close();
```

**MATLAB:**

```matlab
try
    connection = Connection.openTcp('zaber-12345.local', Connection.TCP_PORT_CHAIN);
    deviceList = connection.detectDevices();
    fprintf('Found %d devices.\n', length(deviceList));

    % The rest of your program goes here

catch exception
    disp(getReport(exception));
end
connection.close();
```

**C++:**

```cpp
Connection connection = Connection::openTcp("zaber-12345.local", TCP_PORT_CHAIN);
std::vector<Device> deviceList = connection.detectDevices();
std::cout << "Found " << deviceList.size() << " devices." << std::endl;

// The rest of your program goes here
```

The example program attempts to connect to a TCP server and detect present devices.
If you encounter a problem while connecting, make sure that the server listens on the specified port and is reachable on the network.

API Reference:

- Connection's [`open_tcp`](https://software.zaber.com/motion-library/api/py/ascii/connection.md#opentcp) method
