-
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
hieu253
committed
Jan 19, 2024
1 parent
e79c3a1
commit 8ebf458
Showing
19 changed files
with
722 additions
and
0 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,9 @@ | ||
.PHONY: all clean | ||
|
||
all : | ||
gcc -o client client.c | ||
gcc -o server server.c | ||
|
||
clean: | ||
rm -rf client | ||
rm -rf server |
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,93 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
|
||
#define buff_size 256 | ||
#define handler_error(msg) \ | ||
do \ | ||
{ \ | ||
perror(msg); \ | ||
exit(EXIT_FAILURE); \ | ||
} while (0) | ||
|
||
void chat_func(int server_fd) | ||
{ | ||
int num_write, num_read; | ||
char sendbuff[buff_size]; | ||
char recvbuff[buff_size]; | ||
while (1) | ||
{ | ||
memset(sendbuff, '0', buff_size); | ||
memset(recvbuff, '0', buff_size); | ||
printf("Please enter the message: "); | ||
fgets(sendbuff, buff_size, stdin); | ||
|
||
// Gui thong diep toi server bang ham write | ||
num_write = write(server_fd, sendbuff, sizeof(sendbuff)); | ||
if (num_write < 0) | ||
{ | ||
handler_error("read()"); | ||
} | ||
if (strncmp("exit", sendbuff, 4) == 0) | ||
{ | ||
printf("Client exit ...\n"); | ||
break; | ||
} | ||
// Nhan thong diep tu server bang ham read | ||
num_read = read(server_fd, recvbuff, sizeof(recvbuff)); | ||
if (num_read < 0) | ||
{ | ||
handler_error("read()"); | ||
} | ||
if (strncmp("exit", recvbuff, 4) == 0) | ||
{ | ||
printf("Server exit ...\n"); | ||
break; | ||
} | ||
printf("\nMessage form Server: %s\n", recvbuff); | ||
} | ||
close(server_fd); | ||
} | ||
int main(int argc, char *argv[]) | ||
{ | ||
int port_no; | ||
int server_fd; | ||
struct sockaddr_in serv_addr; | ||
memset(&serv_addr, '0', sizeof(serv_addr)); | ||
if (argc < 3) | ||
{ | ||
printf("command: ./client <server address> <port number>\n"); | ||
exit(1); | ||
} | ||
port_no = atoi(argv[2]); | ||
|
||
// khoi tao dia chi server | ||
serv_addr.sin_family = AF_INET; | ||
serv_addr.sin_port = htons(port_no); | ||
serv_addr.sin_addr.s_addr = INADDR_ANY; | ||
if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) == -1) | ||
{ | ||
handler_error("inet_pton()"); | ||
} | ||
|
||
// tao socket | ||
server_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (server_fd == -1) | ||
{ | ||
handler_error("socket()"); | ||
} | ||
|
||
// ket noi toi server | ||
if (connect(server_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) | ||
{ | ||
handler_error("connect()"); | ||
} | ||
chat_func(server_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,117 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
#include <stdlib.h> | ||
|
||
#define buff_size 256 | ||
#define BACK_LOG 50 | ||
#define handler_error(msg) \ | ||
do \ | ||
{ \ | ||
perror(msg); \ | ||
exit(EXIT_FAILURE); \ | ||
} while (0) | ||
|
||
void chat_func(int client_fd) | ||
{ | ||
int num_read, num_write; | ||
char sendbuff[buff_size]; | ||
char recvbuff[buff_size]; | ||
while (1) | ||
{ | ||
memset(sendbuff, '0', buff_size); | ||
memset(recvbuff, '0', buff_size); | ||
|
||
// doc du lieu tu socket | ||
num_read = read(client_fd, recvbuff, buff_size); | ||
if (num_read == -1) | ||
{ | ||
handler_error("read()"); | ||
} | ||
if (strncmp("exit", recvbuff, 4) == 0) | ||
{ | ||
system("clear"); | ||
break; | ||
} | ||
printf("\nMessage from client: %s", recvbuff); | ||
printf("\nMessage respond the message: "); | ||
fgets(sendbuff, buff_size, stdin); | ||
|
||
// ghi du lieu toi client thong qua ham write | ||
num_write = write(client_fd, sendbuff, sizeof(sendbuff)); | ||
if (num_write == -1) | ||
{ | ||
handler_error("write()"); | ||
} | ||
if (strncmp("exit", sendbuff, 4) == 0) | ||
{ | ||
system("clear"); | ||
break; | ||
} | ||
sleep(1); | ||
} | ||
close(client_fd); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int opt, port_no; | ||
int server_fd, client_fd; | ||
struct sockaddr_in serv_addr, client_addr; | ||
|
||
if (argc < 2) | ||
{ | ||
printf("No port provided\ncommand: ./server <port number>\n"); | ||
exit(1); | ||
} | ||
port_no = atoi(argv[1]); | ||
memset(&serv_addr, 0, sizeof(struct sockaddr_in)); | ||
memset(&client_addr, 0, sizeof(struct sockaddr_in)); | ||
|
||
// tao socket | ||
server_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (server_fd == -1) | ||
handler_error("socket()"); | ||
|
||
/* Ngăn lỗi : “address already in use” */ | ||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) | ||
handler_error("setsockopt()"); | ||
|
||
// khoi tao dia chi cho server | ||
serv_addr.sin_family = AF_INET; | ||
serv_addr.sin_port = htons(port_no); | ||
serv_addr.sin_addr.s_addr = INADDR_ANY; | ||
|
||
// gan socket voi dia chi cua server | ||
if (bind(server_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) | ||
{ | ||
handler_error("bind()"); | ||
} | ||
|
||
// Nghe toi da 50 ket noi trong hang doi | ||
if (listen(server_fd, BACK_LOG) == -1) | ||
{ | ||
handler_error("listen()"); | ||
} | ||
int len = sizeof(client_addr); | ||
|
||
while (1) | ||
{ | ||
printf("Server is listening at port: %d\n", port_no); | ||
client_fd = accept(server_fd, (struct sockaddr *)&client_addr, (socklen_t *)&len); | ||
if (client_fd == -1) | ||
{ | ||
handler_error("accept()"); | ||
} | ||
system("clear"); | ||
printf("Server: got connection \n"); | ||
chat_func(client_fd); | ||
} | ||
close(server_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,7 @@ | ||
.PHONY: all clean | ||
|
||
all: | ||
gcc -o exam main.c | ||
|
||
clean: | ||
rm -rf exam |
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,57 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <signal.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
|
||
#define MSG_SIZE 20 | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int num_read = 0; | ||
char in_buff[MSG_SIZE]; | ||
int fds[2]; | ||
char *msg = "Data from parent !!"; | ||
if (pipe(fds) < 0) | ||
{ | ||
printf("pipe() unsuccessfully()\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
pid_t child_pid; | ||
child_pid = fork(); | ||
if (child_pid >= 0) | ||
{ | ||
if (child_pid == 0) | ||
{ | ||
|
||
printf("Im child process\n"); | ||
printf("Child process ID is: %d\n", getpid()); | ||
num_read = read(fds[0], in_buff, MSG_SIZE); | ||
|
||
printf("Child is reading from pipe - Message is: %s\n", in_buff); | ||
|
||
if (close(fds[1] == -1)) | ||
{ | ||
printf("close(fds[1]) failed\n"); | ||
} | ||
} | ||
|
||
else | ||
{ | ||
if (close(fds[0]) == -1) | ||
{ | ||
printf("close(fds[0]) failed\n"); | ||
} | ||
|
||
printf("Im parent process\n"); | ||
printf("Parent process ID is: %d\n", getpid()); | ||
write(fds[1], msg, MSG_SIZE); | ||
} | ||
} | ||
else | ||
{ | ||
printf("fork() initialization unsuccessfully\n"); | ||
} | ||
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,27 @@ | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <signal.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <sys/stat.h> | ||
|
||
#define BUFF_SIZE 1024 | ||
#define FIFO_FILE "./myfifo" | ||
int main(int argc, char *argv[]) | ||
{ | ||
char buffer[BUFF_SIZE]; | ||
int fd; | ||
mkfifo(FIFO_FILE, 0666); | ||
while (1) | ||
{ | ||
printf("Message to FIFOs_2: "); | ||
fflush(stdin); | ||
fgets(buffer, BUFF_SIZE, stdin); | ||
fd = open(FIFO_FILE, O_WRONLY); | ||
write(fd, buffer, sizeof(buffer)); | ||
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,26 @@ | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <signal.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <sys/stat.h> | ||
|
||
#define FIFO_FILE "./myfifo" | ||
#define BUFF_SIZE 1024 | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
char buffer[BUFF_SIZE]; | ||
int fd; | ||
mkfifo(FIFO_FILE, 0666); | ||
while (1) | ||
{ | ||
fd = open(FIFO_FILE, O_RDONLY); | ||
read(fd, buffer, BUFF_SIZE); | ||
printf("FIFOs_2 message: %s", buffer); | ||
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,9 @@ | ||
.PHONY: all clean | ||
|
||
all: | ||
gcc -o FIFOs_1 FIFOs_1.c | ||
gcc -o FIFOs_2 FIFOs_2.c | ||
|
||
|
||
clean: | ||
rm -rf FIFOs_1 FIFOs_2 myfifo |
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,8 @@ | ||
.PHONY: all clean | ||
|
||
|
||
all: | ||
gcc -o exam main.c -lrt | ||
|
||
clean: | ||
rm -rf exam |
Oops, something went wrong.