Skip to content

Commit 4a17cea

Browse files
authored
Add files via upload
1 parent 036e487 commit 4a17cea

File tree

4 files changed

+163
-67
lines changed

4 files changed

+163
-67
lines changed

Makefile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
CLIENT = client
2+
SERVER = server
3+
OBJS = $(SRCS:.c=.o)
4+
5+
CC = gcc
6+
CFLAGS = -Wall -Werror -Wextra
7+
8+
LIB = ./ft_printf/libftprintf.a
9+
10+
NAME = minitalk
11+
12+
all: $(NAME)
13+
14+
$(LIB):
15+
make -C ./ft_printf
16+
17+
$(SERVER): $(LIB ) server.c
18+
$(CC) $(CFLAGS) $(LIB) server.c -o $(SERVER)
19+
20+
$(CLIENT): $(LIB) client.c
21+
$(CC) $(CFLAGS) $(LIB) client.c -o $(CLIENT)
22+
23+
$(NAME): $(LIB) $(CLIENT) $(SERVER)
24+
25+
clean:
26+
rm -f *.o
27+
28+
fclean: clean
29+
rm -f $(SERVER) $(CLIENT)
30+
31+
re: fclean all
32+
33+
.PHONY: all clean fclean re

client.c

+71-32
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,80 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* client.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: binurtas <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/01/14 18:50:01 by binurtas #+# #+# */
9+
/* Updated: 2023/01/14 18:53:08 by binurtas ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
113
#include "minitalk.h"
214

3-
void ft_send_bits(int pid, char c)
15+
int ft_atoi(const char *nptr)
416
{
5-
static int bit;
17+
int i;
18+
int result;
19+
int pn;
620

7-
while (bit < 8)
8-
{
9-
if (c & (0x01 << bit) != 0)
10-
kill(pid, SIGONE);
11-
else
12-
kill(pid, SIGZERO);
13-
usleep(100);
14-
bit++;
15-
}
21+
i = 0;
22+
result = 0;
23+
pn = 1;
24+
while ((nptr[i] >= 9 && nptr[i] <= 13) || (nptr[i] == 32))
25+
i++;
26+
if (nptr[i] == '+' || nptr[i] == '-')
27+
{
28+
if (nptr[i] == '-')
29+
pn *= -1;
30+
i++;
31+
}
32+
while (nptr[i] <= '9' && nptr[i] >= '0')
33+
{
34+
result = (result * 10) + (nptr[i] - '0');
35+
i++;
36+
}
37+
return (result * pn);
38+
}
1639

40+
void ft_send_bits(int pid, char c)
41+
{
42+
static int bit;
43+
44+
while (bit < 8)
45+
{
46+
if ((c & (0x01 << bit)) != 0)
47+
kill(pid, SIGONE);
48+
else
49+
kill(pid, SIGZERO);
50+
usleep(100);
51+
bit++;
52+
}
53+
bit = 0;
1754
}
1855

19-
int main(int ac, char **av)
56+
int main(int ac, char **av)
2057
{
21-
int pid;
22-
int i;
58+
int pid;
59+
int i;
2360

24-
i = 0;
25-
if (ac == 3)
26-
{
27-
pid = ft_atoi(av[1]);
28-
while (av[2][i])
29-
{
30-
ft_send_bits(pid, av[2][i]);
31-
i++;
32-
}
33-
ft_send_bits(pid, '\n');
34-
}
35-
else
36-
{
37-
ft_printf("Wrong format. You have to use like this :\n");
38-
ft_printf("./client <PID> <MESSAGE>\n");
39-
}
40-
return (0);
41-
}
61+
i = 0;
62+
if (ac == 3)
63+
{
64+
pid = ft_atoi(av[1]);
65+
if (pid == -1)
66+
return (0);
67+
while (av[2][i])
68+
{
69+
ft_send_bits(pid, av[2][i]);
70+
i++;
71+
}
72+
ft_send_bits(pid, '\n');
73+
}
74+
else
75+
{
76+
ft_printf("Wrong format. You have to use like this :\n");
77+
ft_printf("./client <PID> <MESSAGE>\n");
78+
}
79+
return (0);
80+
}

minitalk.h

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* minitalk.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: binurtas <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/01/14 18:53:44 by binurtas #+# #+# */
9+
/* Updated: 2023/01/14 18:55:21 by binurtas ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
113
#ifndef MINITALK_H
2-
#define MINITALK_H
14+
# define MINITALK_H
315

4-
#include "ft_printf.h"
5-
#include <signal.h>
6-
#include <stdlib.h>
7-
#include <unistd.h>
16+
# include "ft_printf/ft_printf.h"
17+
# include <signal.h>
18+
# include <stdlib.h>
19+
# include <unistd.h>
820

9-
#define SIGZERO SIGUSR1
10-
#define SIGONE SIGUSR2
21+
# define SIGZERO SIGUSR1
22+
# define SIGONE SIGUSR2
1123

12-
#endif
24+
#endif

server.c

+39-27
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
1-
#include "minitalk.h"
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* server.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: binurtas <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/01/14 18:44:41 by binurtas #+# #+# */
9+
/* Updated: 2023/01/14 18:49:08 by binurtas ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
212

13+
#include "minitalk.h"
314

4-
void ft_handler(int signal)
15+
void ft_handler(int signal)
516
{
6-
static int bit;
7-
static unsigned char i;
17+
static int bit;
18+
static unsigned char i;
819

9-
if (signal == SIGONE)
10-
i |= (0x01 << bit);
11-
bit++;
12-
if (bit == 8)
13-
{
14-
ft_printf("%c", i);
15-
bit = 0;
16-
i = 0;
17-
}
20+
if (signal == SIGONE)
21+
i |= (0x01 << bit);
22+
bit++;
23+
if (bit == 8)
24+
{
25+
ft_printf("%c", i);
26+
bit = 0;
27+
i = 0;
28+
}
1829
}
1930

20-
int main(int ac, char **av)
31+
int main(int ac, char **av)
2132
{
22-
if (ac != 1)
23-
{
24-
ft_printf("You should use like that :\n./server");
25-
return (0);
26-
}
27-
ft_print_pid();
28-
while (ac == 1)
29-
{
30-
signal(SIGONE, ft_handler);
31-
signal(SIGZERO, ft_handler);
32-
pause();
33-
}
34-
return (0);
33+
(void)av;
34+
if (ac != 1)
35+
{
36+
ft_printf("You should use like this :\n./server");
37+
return (0);
38+
}
39+
ft_printf("PID : %d\n", getpid());
40+
while (ac == 1)
41+
{
42+
signal(SIGONE, ft_handler);
43+
signal(SIGZERO, ft_handler);
44+
pause();
45+
}
46+
return (0);
3547
}

0 commit comments

Comments
 (0)