-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_getFileByFirstLetter.c
More file actions
208 lines (162 loc) · 6.15 KB
/
client_getFileByFirstLetter.c
File metadata and controls
208 lines (162 loc) · 6.15 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*-------------------------------------------------------------------------*
*--- ---*
*--- client_getFileByFirstLetter.c ---*
*--- ---*
*--- This file defines a C program that asks the user for a ---*
*--- letter, and then asks a server for the text of a file that ---*
*--- begins with that letter. ---*
*--- ---*
*--- ---- ---- ---- ---- ---- ---- ---- ---- ---*
*--- ---*
*--- Version 1.0 2016 May 23 Joseph Phillips ---*
*--- ---*
*-------------------------------------------------------------------------*/
/*--- Header file inclusion ---*/
#include "getFileByFirstLetter.h"
#include <ctype.h> // isalpha()
// PURPOSE: To ask the user for the name and the port of the server. The
// server name is returned in 'url' up to length 'urlLen'. The port
// number is returned in '*portPtr'. No return value.
void obtainUrlAndPort (int urlLen,
char* url,
int* portPtr
)
{
// I. Application validity check:
if ( (url == NULL) || (portPtr == NULL) )
{
fprintf(stderr,"BUG: NULL ptr to obtainUrlAndPort()\n");
exit(EXIT_FAILURE);
}
if (urlLen <= 1)
{
fprintf(stderr,"BUG: Bad string length to obtainUrlAndPort()\n");
exit(EXIT_FAILURE);
}
// II. Get server name and port number:
// II.A. Get server name:
printf("Machine name [%s]? ",DEFAULT_HOSTNAME);
fgets(url,urlLen,stdin);
char* cPtr = strchr(url,'\n');
if (cPtr != NULL)
*cPtr = '\0';
if (url[0] == '\0')
strncpy(url,DEFAULT_HOSTNAME,urlLen);
// II.B. Get port numbe:
char buffer[BUFFER_LEN];
printf("Port number? ");
fgets(buffer,BUFFER_LEN,stdin);
*portPtr = strtol(buffer,NULL,10);
// III. Finished:
}
// PURPOSE: To attempt to connect to the server named 'url' at port 'port'.
// Returns file-descriptor of socket if successful, or '-1' otherwise.
int attemptToConnectToServer (const char* url,
int port
)
{
// I. Application validity check:
if (url == NULL)
{
fprintf(stderr,"BUG: NULL ptr to attemptToConnectToServer()\n");
exit(EXIT_FAILURE);
}
// II. Attempt to connect to server:
// II.A. Create a socket:
int socketDescriptor = socket(AF_INET, // AF_INET domain
SOCK_STREAM, // Reliable TCP
0);
// II.B. Ask DNS about 'url':
struct addrinfo* hostPtr;
int status = getaddrinfo(url,NULL,NULL,&hostPtr);
if (status != 0)
{
fprintf(stderr,gai_strerror(status));
return(-1);
}
// II.C. Attempt to connect to server:
struct sockaddr_in server;
// Clear server datastruct
memset(&server, 0, sizeof(server));
// Use TCP/IP
server.sin_family = AF_INET;
// Tell port # in proper network byte order
server.sin_port = htons(port);
// Copy connectivity info from info on server ("hostPtr")
server.sin_addr.s_addr =
((struct sockaddr_in*)hostPtr->ai_addr)->sin_addr.s_addr;
status = connect(socketDescriptor,(struct sockaddr*)&server,sizeof(server));
if (status < 0)
{
fprintf(stderr,"Could not connect %s:%d\n",url,port);
return(-1);
}
// III. Finished:
return(socketDescriptor);
}
// PURPOSE: To do the work of the application. Gets letter from user, sends
// it to server over file-descriptor 'fd', and either prints text of
// returned error code, or prints returned file. No return value.
void communicateWithServer (int fd
)
{
// I. Application validity check:
// II. Do work of application:
// II.A. Get letter from user:
char buffer[BUFFER_LEN+1];
do
{
printf("Please enter a letter to look for: ");
fgets(buffer,BUFFER_LEN,stdin);
}
while ( !isalpha(buffer[0]) );
// II.B. Send letter to server:
write(fd,buffer,1);
// II.C. Get response from server:
uint32_t fileSize;
read(fd,&fileSize,sizeof(fileSize));
fileSize = ntohl(fileSize);
// II.D. Interpret server response:
switch (fileSize)
{
case NO_MATCH_CODE :
printf("No matching file found for %c\n",buffer[0]);
break;
case CANT_READ_FILE_CODE :
printf("Matching file found for %c, but could not open\n",buffer[0]);
break;
case CANT_READ_DIR_CODE :
printf("Server could not open .\n",buffer[0]);
break;
default :
{
unsigned int totalNumBytesRead = 0;
int numBytesRead;
printf("The file that matches %c has size %u\n",buffer[0],fileSize);
while ( (totalNumBytesRead < fileSize) &&
( (numBytesRead = read(fd,buffer,BUFFER_LEN)) > 0)
)
{
buffer[numBytesRead] = '\0';
printf("%s",buffer);
totalNumBytesRead += (unsigned int)numBytesRead;
}
}
}
// III. Finished:
}
// PURPOSE: To do the work of the client. Ignores command line parameters.
// Returns 'EXIT_SUCCESS' to OS on success or 'EXIT_FAILURE' otherwise.
int main ()
{
char url[BUFFER_LEN];
int port;
int fd;
obtainUrlAndPort(BUFFER_LEN,url,&port);
fd = attemptToConnectToServer(url,port);
if (fd < 0)
exit(EXIT_FAILURE);
communicateWithServer(fd);
close(fd);
return(EXIT_SUCCESS);
}