accepts a single tcp connection

This commit is contained in:
lolcat 2024-03-09 22:07:29 -05:00
parent 20d9045229
commit aed22bdd5e
4 changed files with 114 additions and 1 deletions

BIN
chat Executable file

Binary file not shown.

View File

@ -9,7 +9,7 @@ lulchat relies on HTTP, webRTC and websocket technologies. We use this in order
When we refer to data types, always assume that they are being sent over the wire in network-byte order (big endian).
# Usernames
Usernames must only contain alphanumeric characters, numbers, and underscores. Must contain between 3 and 21 characters. The following RegexP validates all usernames:
Usernames must only contain alphanumeric characters, numbers, and underscores. Must contain between 1 and 21 characters. The following RegexP validates all usernames:
```sh
^[A-Za-z0-9_]{1,21}$
```

107
src/chat.c Normal file
View File

@ -0,0 +1,107 @@
#include <stdio.h>
#include <unistd.h> // sleep
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
void *loop(){
while(1) {
sleep(1);
printf("testing\n");
}
}
int main(int argc, char *argv[]){
int server_handle;
if((server_handle = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("Failed to create socket");
exit(1);
}
int opt = 1;
if(
setsockopt(
server_handle,
SOL_SOCKET,
SO_REUSEADDR | SO_REUSEPORT,
&opt,
sizeof(opt)
)
){
perror("Could not set socket options");
exit(1);
}
struct sockaddr_in address;
address.sin_family = AF_UNSPEC;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if(
(
bind(
server_handle,
(struct sockaddr*)&address,
sizeof(address)
)
) < 0
){
perror("Failed to bind to socket");
exit(1);
}
if(
listen(
server_handle,
3
) < 0
){
perror("Failed to listen to socket");
exit(1);
}
printf("[INFO] Listening for connections\n");
int client_handle;
socklen_t addrlen = sizeof(address);
//while(1){
if(
(
client_handle = accept(
server_handle,
(struct sockaddr*)&address,
&addrlen
)
) < 0
){
perror("Failed to accept client connection");
exit(1);
}
//}
ssize_t buffer_read;
while(1){
char buffer[1024] = {0};
buffer_read = read(client_handle, buffer, 1023);
printf("Message (%zd): %s", buffer_read, buffer);
send(client_handle, buffer, strlen(buffer), 0);
}
return 0;
/*
pthread_t thread_id;
pthread_create(&thread_id, NULL, loop, NULL);*/
//pthread_join(thread_id, NULL);
}

6
src/configutils.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdlib.h>
int main(){
printf("boobs");
}