-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
190 lines (170 loc) · 5.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
/* #include <termios.h> */
#define BUFFER_LENGTH 256
#define SERVER_PORT 8080
#define SERVER_NAME "127.0.0.1"
int main(int argc, char* argv[])
{
struct sockaddr_in serveraddr;
char
*server,
username[BUFFER_LENGTH],
target[BUFFER_LENGTH],
buffer[BUFFER_LENGTH],
temp[BUFFER_LENGTH],
recieve[BUFFER_LENGTH],
message[BUFFER_LENGTH];
int socket_descriptor;
pid_t pid;
/* struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &newt); */
do
{
int
status = 0,
valread,
c;
// socket
socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);
if(socket_descriptor < 0)
{
perror("socket() failed");
break;
}
if(argc > 1)
server = strdup(argv[1]);
else
server = strdup(SERVER_NAME);
puts("Chat client\nHelp:\nType \"exit()\" to exit\nType \"change()\" to change message target\n-----");
puts("Enter a username:");
fgets(buffer, BUFFER_LENGTH, stdin);
strcpy(username, buffer);
username[strcspn(username, "\n")] = '\0';
// set up server info
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, server, &serveraddr.sin_addr);
// connect
fputs("connecting...", stdout);
status = connect(socket_descriptor, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if(status < 0)
{
perror("connect() failed");
break;
}
puts("\tconnected to server");
status = send(socket_descriptor, username, strlen(username) + 1, 0);
if(status <= 0)
{
perror("send() failed");
break;
}
sleep(1);
puts("Enter a username to send messages to:");
fgets(buffer, BUFFER_LENGTH, stdin);
strcpy(target, buffer);
target[strcspn(target, "\n")] = '\0';
puts("-----");
int
leave = 1,
*die = &leave; // pointer jic so we can reference on both forks
pid = fork(); // 0 is child, nonzero is parent
if(pid < 0)
{
perror("fork() failed");
break;
}
char
keyword[7] = "exit()",
change[9] = "change()";
if(pid == 0)
{
//printf("\tpid %d recieving\n", pid);
do
{
// recv
if(!*die) break;
valread = recv(socket_descriptor, recieve, BUFFER_LENGTH, 0);
if (valread < 0)
{
perror("recv() failed");
break;
}
else if (valread == 0)
{
perror("server closed");
break;
}
recieve[valread] = '\0'; // trim string properly
/* fgets(temp, sizeof(temp), stdin);
c = strlen(temp)-1;
while(c >= 0)
{
ungetc(temp[c--], stdin);
} */
//printf("\33[0G\n%s\n\33[F", recieve);
puts(recieve);
} while (*die);
//puts("closing recv() fork");
break;
}
if(pid > 0)
{
//printf("\tpid %d sending\n", pid);
do
{
//fputs(">>", stdout);
fgets(buffer, BUFFER_LENGTH, stdin);
buffer[strcspn(buffer, "\n")] = '\0'; // $ man strcspn
*die = strcmp(buffer, keyword);
if(!*die) break;
if(!strcmp(buffer, change))
{
puts("Enter a username to send messages to:");
fgets(buffer, BUFFER_LENGTH, stdin);
strcpy(target, buffer);
target[strcspn(target, "\n")] = '\0';
//continue;
}
else
{
int message_length = (strlen(target)+strlen(username)+strlen(buffer)+6);
snprintf(message, message_length, "%s:%s : %s", target, username, buffer);
status = send(socket_descriptor, message, sizeof(message), 0);
if (status <= 0)
{
perror("send() failed");
break;
}
}
} while (*die);
//puts("closing send() fork");
break;
}
/* TODO fix user input being interrupted by recieving message
-save user input
-print escape character to clear line
-print recieved message
-print partial input from user
*/
} while(0);
/* tcsetattr(STDIN_FILENO, TCSANOW, &oldt); */
kill(pid, SIGKILL);
// close
close(socket_descriptor);
exit(EXIT_SUCCESS);
return 0;
}