Emojicode Documentation 0.8
Guides Language Reference Package Index

Package sockets

The sockets package allows you to open TCP sockets to servers or to create a TCP server socket yourself.

The following is a very basic example of opening a TCP socket to make an HTTP request and print the first 140 characters of the response.

πŸ“¦ sockets 🏠

🏁 πŸ‡
  🍺 πŸ†•πŸ“žπŸ†• πŸ”€example.comπŸ”€ 80❗️ ➑️ socket
  πŸ’¬ socket πŸ“‡ πŸ”€GET / HTTP/1.1❌r❌nHost: example.com❌r❌n❌r❌nπŸ”€β—οΈβ—οΈ

  🍺 πŸ‘‚ socket 140❗️ ➑️ data
  πŸ˜€ 🍺 πŸ”‘ data❗️❗️
πŸ‰

At the time of writing this, the above code would return something similar to:

HTTP/1.1 200 OK
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Mon, 10 Sep 2018 08:36:28 GMT
Etag: "15410256

Here we’ve an example of a minimal echo-server that listens on port 8728. The server simply sends back a copy of the data it received.

πŸ“¦ sockets 🏠

πŸ’­ Simple echo server listening on port 8728
🏁 πŸ‡
  πŸΊπŸ†•πŸ„πŸ†• 8728❗️ ➑️ server

  πŸ” πŸ‘ πŸ‡
    🍺 πŸ™‹ server❗️ ➑️ clientSocket

    πŸ” πŸ‘ πŸ‡
      πŸ₯‘ data πŸ‘‚ clientSocket 50❗️ πŸ‡
        πŸ’­ We’ve read 50 bytes and send them back
        πŸ’¬ clientSocket data❗️
      πŸ‰
      πŸ™…β€β™€οΈ error πŸ‡πŸ‰
    πŸ‰
  πŸ‰
πŸ‰

Of course, the code above is minimal. For example, it can handle only one connection.

Types