-
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
Showing
6 changed files
with
126 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CA1/*.out | ||
CA1/*.exe | ||
#CA1/CMakeLists.txt | ||
#CA1/cmake-build-debug |
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,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; | ||
} |
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,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; | ||
} |
This file was deleted.
Oops, something went wrong.
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,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 |
This file was deleted.
Oops, something went wrong.