ahmet
ahmet

Reputation: 646

Sending a message from server to client

I am implementing both server and client side of a simple file download program. Client side requests file names from the server with get command, and server responses quickly. While server writes to the socket, clients reads the socket and prints out the buffer. After that time, program starts not to interpret my commands unless I press 'Enter' twice. (You can see it from Shell output below)

After debugging, I found out that it is because of the buffer size. While server writing to the socket; everything works properly if I use a small buffer size. But if I use a larger buffer size such as 1024, that problem occurs. How can I get rid of this issue?

#define F_BUFF_SIZE 1024

On server side:

/* ... */
if(!strcmp(buffer, "list\n")) {

    char buff[F_BUFF_SIZE];
    bzero(buff, F_BUFF_SIZE);

    pt_ret = pthread_create(&thread_id, NULL, getfiles, (void*) buff);
    pthread_join(thread_id, pt_ret);

    n = write(sock, buff, F_BUFF_SIZE);
/* ... */

On client side:

/* ... */
char buffer[F_BUFF_SIZE];

bzero(buffer, F_BUFF_SIZE);
n = read(b_sock, buffer, F_BUFF_SIZE - 1);

if (n < 0) {
#ifdef _DEBUG_
    fprintf(stderr, "Error: Could not read from the socket.\n");
#endif
    return 0;
}

fputs(buffer, stdout);
/* ... */

Shell:

Opening socket: OK!
Connecting: OK!
 # list
client
project1.mk
cs342.workspace
client.c
project1.project
cs342.workspace.session
server
cs342_wsp.mk
server.c
cs342.tags
 # get
 # take
get 
# take
Unknown command.
...

Upvotes: 2

Views: 1762

Answers (2)

dbeer
dbeer

Reputation: 7213

There is no magic to having a smaller buffer size, this is just exposing that you have an error elsewhere. Joerg's comment is an important point - you need to be reading the same amount of data that you are writing. I'm wondering if there's also an issue with how you populate buff. You need to make sure that you are not overrunning the end of the buffer, or forgetting to add a null terminator to the end of the string.

By the way, it is important to read the size - 1; you're correct to do this since read won't append a null terminator to a string. You just need to be sure that you're writing that amount because otherwise there can be problems.

You must have some issue along these lines - this would explain why changing the size avoids the problem because problems like this are only exposed when the numbers line up perfectly. Running the program in valgrind may expose the issue (look for invalid write errors).

Upvotes: 1

Ramon Araujo
Ramon Araujo

Reputation: 1738

@ahmet, your useful question lead me to research a little bit. If you read a little bit on this paper you will have a better idea of what you are dealing with and then you will be able to determine the best buffer size in your situation.

As you may know, values like that should always be part of the settings of the application, so don't scramble those values inside the code.

Also, here are good advices on how to figure buffer size, hope that helps,

Upvotes: 1

Related Questions