Skip to content

Commit

Permalink
sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 29, 2023
1 parent c4d6a12 commit dd3906c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
9 changes: 9 additions & 0 deletions examples/client.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() Int {
var sockint: Int = clientsock();
println(recvsock(sockint));
while true {
sendsock(readln(), sockint);
}
closesock(sockint);
return 0;
}
14 changes: 14 additions & 0 deletions examples/server.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() Int {
var sockint: Int = serversock();
sendsock("Welcome to slothnet!", sockint);
var con: Bool = true;
while con == true {
var msg: String = recvsock(sockint);
println(msg);
if sequals(msg, "kill") {
con = false;
}
}
closesock(sockint);
return 0;
}
13 changes: 13 additions & 0 deletions std/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdbool.h>

int wait(int msec) {
struct timespec ts;
Expand Down Expand Up @@ -40,6 +41,18 @@ int as_int(float x) {
return (int) x;
}

bool sequals(char* a, char* b) {
if (strlen(a) != strlen(b)) {
return false;
}
for (int i=0; i<strlen(a); i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}

char* istr(int x) {
char* snum = malloc(12);
sprintf(snum, "%d", x);
Expand Down
2 changes: 1 addition & 1 deletion std/stdlib.sloth
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ foreign fn termpos(x: Int, y: Int);
foreign fn as_int(x: Float) Int;
foreign fn istr(x: Int) String;
foreign fn system(cmd: String) Int;

foreign fn sequals(a: String, b: String) Bool;
foreign fn termclear() Void;
48 changes: 48 additions & 0 deletions std/stdsocket.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080

int serversock() {
int opt = 1;
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
int addrlen = sizeof(address);
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

bind(sock, (struct sockaddr*)&address, sizeof(address));
listen(sock, 3);
int new_sock = accept(sock, (struct sockaddr*)&address, (socklen_t*)&addrlen);
return new_sock;
}

int clientsock() {
struct sockaddr_in serv_addr;
int sock = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
int status = connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
return sock;
}

char* recvsock(int soc) {
char* buf = malloc(1024);
int valread = read(soc, buf, 1024);
return buf;
}

void sendsock(char* msg, int soc) {
send(soc, msg, strlen(msg), 0);
}

void closesock(int soc) {
close(soc);
}
5 changes: 5 additions & 0 deletions std/stdsocket.sloth
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foreign fn serversock() Int;
foreign fn clientsock() Int;
foreign fn closesock(soc: Int);
foreign fn sendsock(msg: String, soc: Int);
foreign fn recvsock(soc: Int) String;

0 comments on commit dd3906c

Please sign in to comment.