Skip to content

Commit

Permalink
CA1
Browse files Browse the repository at this point in the history
  • Loading branch information
SM2A committed Oct 23, 2021
1 parent b323354 commit 644c039
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CA1/*.out
CA1/*.exe
#CA1/CMakeLists.txt
#CA1/cmake-build-debug
47 changes: 47 additions & 0 deletions CA1/Source/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/time.h>

int connectServer(int port) {
int fd;
struct sockaddr_in server_address;

fd = socket(AF_INET, SOCK_STREAM, 0);

server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");

if (connect(fd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) { // checking for errors
printf("Error in connecting to server\n");
}

return fd;
}

int main() {

int fd;
char buff[1024] = {0};

fd = connectServer(8080);

read(0, buff, 1024); // fd = 0 -> stdin

send(fd, buff, strlen(buff), 0);

recv(fd, buff, 1024, 0);

printf("Server said: %s\n", buff);

close(fd);


return 0;
}
63 changes: 63 additions & 0 deletions CA1/Source/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/time.h>

int setupServer(int port) {
struct sockaddr_in address;
int server_fd;
server_fd = socket(AF_INET, SOCK_STREAM, 0);

int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);

bind(server_fd, (struct sockaddr *) &address, sizeof(address));

listen(server_fd, 4);

return server_fd;
}

int acceptClient(int server_fd) {
int client_fd;
struct sockaddr_in client_address;
int address_len = sizeof(client_address);

client_fd = accept(server_fd, (struct sockaddr *) &client_address, (socklen_t * ) & address_len);
printf("Client connected!\n");

return client_fd;
}

int main() {

int server_fd, client_fd;
char buff[1024] = {0};

server_fd = setupServer(8080);

int client_count = 0;

while (1) {
client_fd = acceptClient(server_fd);
client_count++;

memset(buff, 0, 1024);
recv(client_fd, buff, 1024, 0);

printf("Client said: %s\n", buff);

sprintf(buff, "Hello from server, you're client %d", client_count);
}

return 0;
}
4 changes: 0 additions & 4 deletions CA1/client.c

This file was deleted.

12 changes: 12 additions & 0 deletions CA1/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
COMPILER=gcc
SRC=Source

all: client.out server.out

client.out: ${SRC}/client.c
${COMPILER} ${SRC}/client.c -o $@
server.out: ${SRC}/client.c
${COMPILER} ${SRC}/server.c -o $@

clean:
rm *.out
4 changes: 0 additions & 4 deletions CA1/server.c

This file was deleted.

0 comments on commit 644c039

Please sign in to comment.