patterncMinor
Fetching filesystem directory listings on a server
Viewed 0 times
directoryserverlistingsfetchingfilesystem
Problem
I developed a server which takes a directory name then lists files in it and sends this list to a client. And I want that server to work with Telnet.
It works but I have some questions on how I can do this better:
-
My client can take some directory names from arguments and sends them to the server in a cycle. Server responds filenames line by line. After the last line client have to send the next directory. So, how can I split the blocks of lines for each directory? Now I use '/' as a delimiter because this symbol can't be in a file name.
Example:
Is this a good way?
-
Telnet sends each line with CRLF but my client doesn't. I check each string from the client via the function
-
Maybe you have comments about my code style.
This is server.c file:
```
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
void doaction(int sock);
void
error(const char* err)
{
perror(err);
exit(1);
}
int
main(int argc, char *argv[])
{
int sockfd, newsockfd, port, pid, clilen;
struct sockaddr_in server, client;
int reuse = 1;
if(argc != 2) {
printf("usage: server port\n");
exit(1);
}
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) 0)
{
pch=strchr(buffer,'\r');
if (pch != NULL)
{
telnet = 1;
buffer[n-2] = 0;
}
if ((dir = opendir (buffer)) == NULL) {
char str1[] = "\tCan't open the ";
write(sock , str1 , strlen(str1));
write(sock , buffer , strlen(buffer));
write(sock , "\n" , 1);
if (telnet == 0)
write(sock , "/" , 1);
bzero(&buffer, BUFFER_SIZE);
It works but I have some questions on how I can do this better:
-
My client can take some directory names from arguments and sends them to the server in a cycle. Server responds filenames line by line. After the last line client have to send the next directory. So, how can I split the blocks of lines for each directory? Now I use '/' as a delimiter because this symbol can't be in a file name.
Example:
client:
dirname1
server:
filename1
filename2
/
client:
dirname2
server:
filename3
filename4
/
etc ...Is this a good way?
-
Telnet sends each line with CRLF but my client doesn't. I check each string from the client via the function
strchr(buffer,'\r'). If a string has the '\r' symbol, I delete the two last symbols. I think it's strange way, but what do you think? -
Maybe you have comments about my code style.
This is server.c file:
```
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
void doaction(int sock);
void
error(const char* err)
{
perror(err);
exit(1);
}
int
main(int argc, char *argv[])
{
int sockfd, newsockfd, port, pid, clilen;
struct sockaddr_in server, client;
int reuse = 1;
if(argc != 2) {
printf("usage: server port\n");
exit(1);
}
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) 0)
{
pch=strchr(buffer,'\r');
if (pch != NULL)
{
telnet = 1;
buffer[n-2] = 0;
}
if ((dir = opendir (buffer)) == NULL) {
char str1[] = "\tCan't open the ";
write(sock , str1 , strlen(str1));
write(sock , buffer , strlen(buffer));
write(sock , "\n" , 1);
if (telnet == 0)
write(sock , "/" , 1);
bzero(&buffer, BUFFER_SIZE);
Solution
Recommend more validation of command line parameter:
Too easy for
For consistency, use same error handler:
Too easy for
argv[1] to have a value like "-h" and then code proceeds with port with the value of 0.// port = atoi(argv[1]);
char *endptr;
port = strtol(argv[1], &endptr, 10);
if (argv[1] == endptr || *endptr) Handle_NoConverisonExtraData();
if (port PORT_MAX) Handle_BadPortNumber();For consistency, use same error handler:
if(argc != 2) {
error("usage: server port");
}Code Snippets
// port = atoi(argv[1]);
char *endptr;
port = strtol(argv[1], &endptr, 10);
if (argv[1] == endptr || *endptr) Handle_NoConverisonExtraData();
if (port < PORT_MIN || port > PORT_MAX) Handle_BadPortNumber();if(argc != 2) {
error("usage: server port");
}Context
StackExchange Code Review Q#93791, answer score: 3
Revisions (0)
No revisions yet.