# Using the net module

When your transaction test script needs to establish a TCP connection with a network target, you'll use the `net` module from ThousandEyes. The ThousandEyes runtime environment for transaction scripts includes the `net` module for creating raw TCP client sockets to servers on arbitrary ports.

## Importing the Module

To use the `net` module, make sure to first import it within your transaction script:

`import net from 'thousandeyes';`

## Creating a Connection

To create a client connection to a remote server, use the `net.connect` function:

`await net.connect(<port>, '<host.ip.address>');`

## Sending Data

To send data over the socket connection, first assign the result of `net.connect` to a variable:

`let socket = await net.connect(80, '1.1.1.1');`

Then, use the `write` or `writeAll` methods to send data over the socket:

`await socket.writeAll('GET / HTTP/1.1\r\n\r\n');`

To read from the socket, use the `read` or `readAll` methods:

`const response = await socket.readAll();`

#### Example GET Request

In the following example, notice that first the `net` module is imported. Then, after `net.connect` is called, data is sent and received over the socket, using `write` and `read`:

```
import {net} from 'thousandeyes';

runScript();

async function runScript() {
   let host = 'newyork.wan.the-acme-corporation.net';
   let port = 80;
   let requestBody =
`GET / HTTP/1.1
Host: ${host}
User-Agent: thousandeyes-transaction
`

  let client = await net.connect(port, host);
  await client.write(requestBody);
  let response = await client.read();

  console.log(response.toString());
  
};
```

## Advanced Examples

For additional examples, see the [public repository of ThousandEyes transaction scripts](https://github.com/thousandeyes/transaction-scripting-examples/tree/master/API-transaction-scripts).

#### Example: IMAP Login

In the following example, you establish a TCP connection; then send IMAP credentials over the connection you've created:

```
let host = 'box.the-acme-corporation.net';
   let port = 993;
   let imap_password = credentials.get('tonystark@the-acme-corporation.net');

   const sock = await net.connectTls(port, host, {
       minVersion: 'TLSv1.2',
   });

   sock.setEncoding('utf8');

   let imap_commands = [
       `? LOGIN tonystark@the-acme-corporation.net ${imap_password}\n`,
       `? SELECT Inbox\n`,
       `? FETCH 1:1 RFC822\n`,
   ]

   for(var i=0; i < imap_commands.length; i++){
       let command = imap_commands[i];
       await sock.writeAll(command);
     
       // Make driver sleep for .5 seconds
       await driver.sleep(500);
       console.log(command);
       let response = await sock.read();
       console.log(response);
   }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.thousandeyes.com/product-documentation/browser-synthetics/transaction-tests/use-cases/api-monitoring/net-module.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
