-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handling early returns inside the functions
- Loading branch information
1 parent
dab4946
commit 6ec546a
Showing
5 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
def extern print_int(x: integer) -> void; | ||
def extern print(x: str) -> void; | ||
|
||
def plus(x: integer, y: integer) -> integer { | ||
if (x > y) { | ||
return x - y; | ||
} | ||
return x + y; | ||
} | ||
def extern sock(port: integer) -> integer; | ||
def extern sock_listen(fd: integer, backlog: integer) -> void; | ||
def extern sock_accept(fd: integer) -> integer; | ||
def extern sock_write(fd: integer, msg: str, size: integer) -> void; | ||
def extern sock_close(fd: integer) -> void; | ||
|
||
def main() -> void { | ||
let a: integer = 12; | ||
let b: integer = plus(a, 2); | ||
print_int(b); | ||
let fd = sock(8080); | ||
sock_listen(fd, 3); | ||
let new_fd = sock_accept(fd); | ||
|
||
let http_response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 25\r\n\r\n<b>Hello from Bichara</b>"; | ||
|
||
sock_write(new_fd, http_response, 89); | ||
sock_close(new_fd); | ||
sock_close(fd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def extern sock(port: integer) -> integer; | ||
def extern sock_listen(fd: integer, backlog: integer) -> void; | ||
def extern sock_accept(fd: integer) -> integer; | ||
def extern sock_write(fd: integer, msg: str, size: integer) -> void; | ||
def extern sock_close(fd: integer) -> void; | ||
|
||
def main() -> void { | ||
let fd = sock(8080); | ||
sock_listen(fd, 3); | ||
let new_fd = sock_accept(fd); | ||
|
||
let http_response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 25\r\n\r\n<b>Hello from Bichara</b>"; | ||
|
||
sock_write(new_fd, http_response, 89); | ||
sock_close(new_fd); | ||
sock_close(fd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Server side C program to demonstrate Socket | ||
// programming | ||
#include <netinet/in.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
|
||
struct sockaddr_in address; | ||
socklen_t addrlen; | ||
|
||
void sock_close(int fd) { | ||
close(fd); | ||
} | ||
|
||
void sock_write(int fd, const char* msg, int size) { | ||
send(fd, msg, size, 0); | ||
} | ||
|
||
void sock_listen(int fd, int backlog) { | ||
if (listen(fd, backlog) < 0) { | ||
perror("listen"); | ||
exit(EXIT_FAILURE); | ||
} | ||
puts("Socket listen"); | ||
} | ||
|
||
int sock_accept(int fd) { | ||
int new_socket = accept(fd, (struct sockaddr*)&address, &addrlen); | ||
if (new_socket < 0) { | ||
perror("accept"); | ||
exit(EXIT_FAILURE); | ||
} | ||
puts("New socket connection accepted"); | ||
return new_socket; | ||
} | ||
|
||
int sock(int port) { | ||
int opt = 1; | ||
addrlen = sizeof(address); | ||
char buffer[1024] = { 0 }; | ||
|
||
int server_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (server_fd < 0) { | ||
perror("socket failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
puts("Socket open"); | ||
|
||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { | ||
perror("setsockopt"); | ||
exit(EXIT_FAILURE); | ||
} | ||
address.sin_family = AF_INET; | ||
address.sin_addr.s_addr = INADDR_ANY; | ||
address.sin_port = htons(port); | ||
|
||
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) { | ||
perror("bind failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
puts("Socket bound"); | ||
return server_fd; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters