-
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.
- Loading branch information
1 parent
c4d6a12
commit dd3906c
Showing
6 changed files
with
90 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
fn main() Int { | ||
var sockint: Int = clientsock(); | ||
println(recvsock(sockint)); | ||
while true { | ||
sendsock(readln(), sockint); | ||
} | ||
closesock(sockint); | ||
return 0; | ||
} |
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,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; | ||
} |
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
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,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); | ||
} |
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,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; |