> [TCP class reference](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.tcp.html)
> [TCP API Reference](/reference/websocket/tcp)

With TCP, data can be sent and received from a device to a specified IP address or domain using TCP. It can be used to communicate with devices in the same network where the device exists, and can be used to display the router's configuration screen and acquire video footage from network monitoring cameras.

## Connection.

One or more TCPs are available and you can get a free one with [obniz.getFreeTcp()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.obniz.html#getfreetcp). If it doesn't exist, it will be thrown.
You can use [connectWait()](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.tcp.html#connectwait) to make a connection by specifying the port number and IP or domain name. Do it. If it fails, an error is thrown.

```javascript
// JavaScript Example
var tcp = obniz.getFreeTcp();
tcp.connectWait(80,"google.com");
```

You can also use end() to disconnect.

```javascript
// JavaScript Example
var tcp = obniz.getFreeTcp();
tcp.connectWait(80,"google.com");
await obniz.wait(1000);
tcp.end();
```

## Reading and writing

Use [write](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.tcp.html#write) for writing and [onreceive](https:// obniz.github.io/obniz/obnizjs/classes/obnizcore.components.tcp.html#onreceive) to specify the function. The function specified in [onerror](https://obniz.github.io/obniz/obnizjs/classes/obnizcore.components.tcp.html#onerror) will be called when some error occurs.

```javascript
// JavaScript Example
var tcp = obniz.getFreeTcp();
tcp.onerror = state => {
  console.log(state);
};
tcp.onreceive = data => {
  console.log(data);
};
tcp.connectWait(80,"google.com");
tcp.write('hello');
```