-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_utils.c
55 lines (48 loc) · 1.59 KB
/
client_utils.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
/* File of utility functions for the client
* Author: Nick Riasanovsky. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "client.h"
#include "client_utils.h"
#include "student_client.h"
/* Sends a message to the server. Assumes the input message has been properly
* formatted by process input. */
void send_message () {
errno = 0;
int size;
int total_length = strlen (input_message);
int length = 0;
while (length < total_length) {
size = write (socket_fd, input_message + length, total_length - length);
if (size == 0 || (errno && errno != EAGAIN && errno != EWOULDBLOCK)) {
handle_server_disconnect ();
}
length += size;
}
}
/* Outputs the "[Me]:" the user should see. */
void display_prefix () {
printf ("[Me]:");
fflush (stdout);
}
/* Removes the "[Me]:" so that messages can be properly displayed.
* Works by resetting the stdout position to the start of the line,
* then outputing 5 spaces and once again returning the stdout location
* to the start of the line. */
void clear_prefix () {
printf ("\r \r");
fflush (stdout);
}
/* Function to handle the server having disconnected. It should output
* "Server has disconnected" and exit properly. Don't forget to clear
* the prefix. */
void handle_server_disconnect () {
clear_prefix ();
printf ("Server has disconnected\n");
/* Be sure and close your file descriptors. */
close (socket_fd);
exit (0);
}