Skip to content

Commit

Permalink
fix: echo server example
Browse files Browse the repository at this point in the history
  • Loading branch information
oriollinan committed Jan 16, 2025
1 parent 3f88b8b commit d11fb00
Show file tree
Hide file tree
Showing 6 changed files with 525 additions and 24 deletions.
34 changes: 34 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ feature of the `frost` programming language.
sine wave.
- [Assembly Sum (aarm64)](sum_aarm64.ff): This example demonstrates how to write
a function in assembly and call it from `frost`.
- [Echo Server](echo.ff): This example demonstrates how to write
a server that echos to the client.

## Running the Examples

Expand Down Expand Up @@ -77,6 +79,38 @@ displayed in the terminal.
@@@@@@
```

### Writing a server in `frost`

1. Use the standard library to write your server

Have a look at [`echo.ff`]("./echo.ff") for the acutal implementation

2. Compile the program

```sh
$ ./glados -i echo.ff -o echo.ll
```

3. Run the server

```
➜ server ✗ lli main.ll
Listening on port 8001
Connection accepted
Message Received: hello world
```

```
➜ client ✗ telnet localhost 8001
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello world
hello world
```

### Embedding `frost` in other languages

1. Create a `sum.ff` file with the following content:
Expand Down
30 changes: 24 additions & 6 deletions examples/echo.ff
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
%%
% Generate an echo server.
%%

%%
% Standard Library
%%
import "std/io.ff"
import "std/socket.ff"
import "std/lib.ff"
import "std/inet.ff"
import "std/uni.ff"

PORT: int = 8001
BUFFER_SIZE: int = 1024
AF_INET: int = 2


in_addr :: struct {
s_addr -> int
}

sockaddr_in :: struct {
sin_family -> int16
Expand All @@ -17,6 +22,19 @@ sockaddr_in :: struct {
sin_zero -> *int8
}

sockaddr :: struct {
sa_len -> byte
sa_family -> byte
sa_data -> [14]byte
}

PORT: int = 8001
BUFFER_SIZE: int = 1024
AF_INET: int = 2

%%
% Main function
%%
main: never -> int = {

SIZEOF_ADDR: int = 16
Expand Down Expand Up @@ -83,7 +101,7 @@ main: never -> int = {
perror("recv")
stop
}
printf("Message Received: ")
printf("Message Received: %s" buffer)

from 0 to read_size by 1 |i: int| {
printf("%c" buffer.#i)
Expand Down
18 changes: 0 additions & 18 deletions examples/std/socket.ff
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,3 @@ recvfrom: foreign int *byte int int *byte *int -> int
shutdown: foreign int int -> int
setsockopt: foreign int int int *byte int -> int
getsockopt: foreign int int int *byte *int -> int

in_addr :: struct {
s_addr -> int
}

sockaddr_in :: struct {
sin_len -> byte
sin_family -> byte
sin_port -> int16
sin_addr -> in_addr
sin_zero -> [8]byte
}

sockaddr :: struct {
sa_len -> byte
sa_family -> byte
sa_data -> [14]byte
}
Binary file added glados
Binary file not shown.
89 changes: 89 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> // read(), write(), close()
#define MAX 80
#define PORT 8080
#define SA struct sockaddr

// Function designed for chat between client and server.
void func(int connfd) {
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);

// read the message from client and copy it in buffer
read(connfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;

// and send that buffer to client
write(connfd, buff, sizeof(buff));

// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}

// Driver function
int main() {
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;

// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
} else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);

// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA *)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
} else
printf("Socket successfully binded..\n");

// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
} else
printf("Server listening..\n");
len = sizeof(cli);

// Accept the data packet from client and verification
connfd = accept(sockfd, (SA *)&cli, &len);
if (connfd < 0) {
printf("server accept failed...\n");
exit(0);
} else
printf("server accept the client...\n");

// Function for chatting between client and server
func(connfd);

// After chatting close the socket
close(sockfd);
}
Loading

0 comments on commit d11fb00

Please # to comment.