Reputation: 1643
I'm so lost. I can't figure out how to stop reading from the server. I'm basically sending a list of every file in the directory from the server to the client. Heres what I'm doing.
SERVER SIDE:
struct dirent *ep = readdir(dp);
while( ep ){
sprintf(buf, "%s", (ep->d_name));
n = write(newsock, buf, MAX );
ep = readdir(dp);
}
CLIENT SIDE:
while( n = read(sock, buf, MAX)){
printf("buf: %s\n" , buf);
}
So the server side stuff works fine. I can see I'm sending all the file names right, but on the client side it reads all the names but just sits waiting to read more.
Upvotes: 1
Views: 1641
Reputation: 70911
Define a protocol.
A very simple one might be to let the server send an empty (0-length) file name to the client to tell the client that it's done with sending. To successfully do so you might like to delimit each file name by for example an addtionaly \r\n
sequence, or similar.
Upvotes: 1
Reputation: 54138
You either need to send some "EOF" indicator from the server so the client knows to break the loop, or have the server close the socket (after allowing 'some time' for the client to read all the data). This will cause the read to exit with an error condition.
Second is easier on the face of it, first is likely to be more reliable and efficient. Congratulations, you have now built your first communications protocol.
Upvotes: 3