-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
124 lines (109 loc) · 4.1 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
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#include <readline/readline.h>
#include <readline/history.h>
#include "msg.h"
#define RCVBUFSIZE 32 /* Size of receive buffer */
void DieWithError(char *errorMessage); /* Error handling function */
ssize_t getServerMessage(int clntSocket, ServerMessage *data) {
ssize_t numBytesRcvd;
numBytesRcvd = recv(clntSocket, data, sizeof(ServerMessage), 0);
data->messageType = ntohl(data->messageType);
data->SenderId = ntohl(data->SenderId);
data->RecipientId = ntohl(data->RecipientId);
return numBytesRcvd;
}
ssize_t sendClientMessage(int clntSocket, ClientMessage *data) {
ssize_t numBytesSent;
data->request_Type = htonl(data->request_Type);
data->SenderId = htonl(data->SenderId);
data->RecipientId = htonl(data->RecipientId);
numBytesSent = send(clntSocket, data, sizeof(ClientMessage), 0);
return numBytesSent;
}
int main(int argc, char *argv[]) {
int sock; /* Socket descriptor */
int recipientId;
char ch;
char* buffer = NULL;
struct sockaddr_in chatServAddr; /* Echo server address */
unsigned short chatServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
struct timeval timeout;
fd_set readfds;
uint32_t clientId;
ClientMessage outgoingMessage;
ServerMessage incomingMessage;
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
and total bytes read */
if ((argc < 3) || (argc > 4)) { /* Test for correct number of arguments */
fprintf(stderr, "Usage: %s <User ID> <Server IP> [<Echo Port>]\n",
argv[0]);
exit(1);
}
servIP = argv[2]; /* First arg: server IP address (dotted quad) */
recipientId = atoi(argv[1]);
if (argc == 4)
chatServPort = atoi(argv[3]); /* Use given port, if any */
else
chatServPort = 3333;
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
/* Construct the server address structure */
memset(&chatServAddr, 0, sizeof(chatServAddr));
/* Zero out structure */
chatServAddr.sin_family = AF_INET; /* Internet address family */
chatServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
chatServAddr.sin_port = htons(chatServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &chatServAddr, sizeof(chatServAddr))
< 0)
DieWithError("connect() failed");
getServerMessage(sock, &incomingMessage);
printf("got handshake %d (%d) %x\n", incomingMessage.messageType,
Server_Handshake, incomingMessage.RecipientId);
if (incomingMessage.messageType != Server_Handshake
|| incomingMessage.RecipientId != 0xDEADBEEF)
DieWithError("bad handshake");
outgoingMessage.RecipientId = clientId = incomingMessage.SenderId;
outgoingMessage.request_Type = Client_Handshake;
outgoingMessage.SenderId = 0xDEADBEEF;
sendClientMessage(sock, &outgoingMessage);
printf("my Id is %d\n", clientId);
for (;;) {
free(buffer);
buffer = readline("Type your message: ");
if (buffer == NULL || *buffer == '\0')
continue;
//quit message
if (strcmp(buffer, "quit") == 0)
break;
add_history(buffer);
//populate outgoing message
outgoingMessage.RecipientId = recipientId;
outgoingMessage.SenderId = clientId;
outgoingMessage.request_Type = Send;
strncpy(outgoingMessage.message, buffer, 100);
sendClientMessage(sock, &outgoingMessage);
//check for new messages
outgoingMessage.RecipientId = recipientId;
outgoingMessage.SenderId = clientId;
outgoingMessage.request_Type = Retrieve;
sendClientMessage(sock, &outgoingMessage);
//get new messages as long as they are there
do {
getServerMessage(sock, &incomingMessage);
if (incomingMessage.messageType != No_Message) {
printf("%d: %s\n", recipientId, incomingMessage.message);
}
} while (incomingMessage.messageType != No_Message);
}
free(buffer);
close(sock);
exit(0);
}