-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
177 lines (140 loc) · 3.19 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#define LENGTH 2048
// Global variables
//sig_atomic_t = this type guarantees that read and write use one command
volatile sig_atomic_t flag = 0;
int sockfd = 0;
char name[32];
void str_overwrite_stdout()
{
printf("%s", "> ");
fflush(stdout);
}
void str_trim_lf (char* arr, int length)
{
int i;
for (i = 0; i < length; i++)
{
if (arr[i] == '\n')
{
arr[i] = '\0';
break;
}
}
}
void catch_ctrl_c_and_exit(int sig)
{
flag = 1;
}
void send_msg_handler()
{
char message[LENGTH] = {};
char buffer[LENGTH + 32] = {};
while(1)
{
str_overwrite_stdout();
fgets(message, LENGTH, stdin);
str_trim_lf(message, LENGTH);
//exiting from program
if (strcmp(message, "exit") == 0)
{
break;
}
else
{
sprintf(buffer, "\033[37;1;4m%s\033[0m: %s\n", name, message);
send(sockfd, buffer, strlen(buffer), 0);
}
bzero(message, LENGTH);
bzero(buffer, LENGTH + 32);
}
catch_ctrl_c_and_exit(2);
}
void recv_msg_handler()
{
char message[LENGTH] = {};
while (1)
{
int receive = recv(sockfd, message, LENGTH, 0);
if (receive > 0)
{
printf("%s", message);
str_overwrite_stdout();
}
else if (receive == 0)
{
break;
}
else
{
// -1
}
memset(message, 0, sizeof(message));
}
}
int main(int argc, char **argv)
{
if(argc != 2)
{
printf(" %s <port> in use \n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
signal(SIGINT, catch_ctrl_c_and_exit);
printf("Please enter your name which will show on program: ");
fgets(name, 32, stdin);
str_trim_lf(name, strlen(name));
if (strlen(name) > 32 || strlen(name) < 2)
{
printf("Name variable must be more than 2 characters and less than 30. \n");
return EXIT_FAILURE;
}
struct sockaddr_in server_addr;
// Socket settings
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
// Connecting into server
int err = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (err == -1)
{
printf("ERROR: connection error \n");
return EXIT_FAILURE;
}
// Sending the name of user
send(sockfd, name, 32, 0);
printf("=== SAY HELLO TO THE CHAT APP IN BASH === \n");
pthread_t send_msg_thread;
if(pthread_create(&send_msg_thread, NULL, (void *) send_msg_handler, NULL) != 0)
{
printf("ERROR: pthread\n");
return EXIT_FAILURE;
}
pthread_t recv_msg_thread;
if(pthread_create(&recv_msg_thread, NULL, (void *) recv_msg_handler, NULL) != 0)
{
printf("ERROR: pthread\n");
return EXIT_FAILURE;
}
while (1)
{
if(flag)
{
printf("\n BYE. STAY METAL \n");
break;
}
}
close(sockfd);
return EXIT_SUCCESS;
}