Skip to content

Commit

Permalink
Mojo🔥TCP client-server
Browse files Browse the repository at this point in the history
  • Loading branch information
ego committed May 28, 2023
1 parent 5cd5dee commit ff2c3de
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ MLIR is designed to be language-and-target-agnostic, and its primary focus is on
To perform operating system syscalls in MLIR, we need to use a target-specific **backend**.

But with these `execution backends`, basically, we have access to OS syscalls.
And we have the whole world of C/LLVM/Python staff under the hood.
And we have the whole world of C/LLVM/Python stuff under the hood.

Lets have same quick look on it in practice:

Expand Down Expand Up @@ -1286,7 +1286,7 @@ from Pointer import DTypePointer, Pointer
# We can use `from String import String` but for clarification we will use a full form.
# DynamicVector[SIMD[DType.si8, 1]] == DynamicVector[SI8] == String

# Compile time staff.
# Compile time stuff.
alias cArrayOfStrings = DynamicVector[SIMD[DType.si8, 1]]
alias capacity = 1024

Expand All @@ -1308,6 +1308,22 @@ fn star_hostname(hostname: String) -> String:
<img src="img/gethostname.png" height="200" />


## Mojo TCP Socket Server with PythonInterface

Let's do some things for a WEB with Mojo🔥.
We do not have Internet access at playground.modular.com
But we can steal do some interesting stuff like TCP on one machine.

Let's write the first TCP client-server code in Mojo🔥 with [PythonInterface](https://docs.modular.com/mojo/MojoPython/PythonInterface.html)

<img src="img/TCPSocketServer.png" width="250" />
<img src="img/TCPSocketClient.png" width="250" />

* [Mojo TCP Socket Server](algorithm/TCPSocketServer.mojo)
* [Mojo TCP Client Server](algorithm/TCPSocketClient.mojo)

Preatty nice

# Code implementation

## Radiative transfer
Expand Down Expand Up @@ -1398,6 +1414,7 @@ A very good domain name that accurately describes the meaning of the Company.
* [LLVM](https://llvm.org)
* [MLIR](https://mlir.llvm.org)
* [Circuit IR Compilers and Tools](https://circt.llvm.org)
* [Cross Compile Compiler-rt](https://releases.llvm.org/8.0.1/docs/HowToCrossCompileBuiltinsOnArm.html)
* [The Golden Age of Compiler Design in an Era of HW/SW Co-design by Dr. Chris Lattner](https://youtu.be/4HgShra-KnY)
* [LLVM in 100 Seconds](https://youtu.be/BT2Cv-Tjq7Q)
* [Mojo Dojo](https://mojodojo.dev/mojo_team_answers.html)
Expand Down
2 changes: 1 addition & 1 deletion algorithm/MLIR_libc.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ from Pointer import DTypePointer, Pointer
# We can use `from String import String` but for clarification we will use a full form.
# DynamicVector[SIMD[DType.si8, 1]] == DynamicVector[SI8] == String

# Compile time staff.
# Compile time stuff.
alias cArrayOfStrings = DynamicVector[SIMD[DType.si8, 1]]
alias capacity = 1024

Expand Down
35 changes: 35 additions & 0 deletions algorithm/TCPSocketServer.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Mojo TCP Socket Server with PythonInterface"""
from PythonInterface import Python

# Python Socket
let socket = Python.import_module('socket')

let host = socket.gethostbyname(socket.gethostname()).to_string()
print(host)
py_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

py_socket.bind(("10.8.95.167", 55555))
print("Socket bind to", "10.8.95.167", 55555)

py_socket.listen()
print("Listen for client connection ..")

var conn_addr = py_socket.accept()
var conn = conn_addr[0]
let addr = conn_addr[1]
print("Connected by client", addr)

while True:
let data = conn.recv(1024)
if data:
print("Message from client", data)
else:
break
# ping-pong
conn.sendall(data)

conn.close()
print("Connection close.")

py_socket.close()
print("Socket close.")
Binary file added img/TCPSocketClient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/TCPSocketServer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ff2c3de

Please sign in to comment.