Hi, I'm doing client/server interaction with sockets in C. What I'm trying to do is have the client request a file to be read on the server, the server return a buffer of the file contents, and the client print out the file.
Though I was able to accomplish the server sending a buffer of a file to the client & the client printing it out successfully, I can't seem to get the server to successfully read a filename sent by the client.
Here is what I mean:
///This is the structure of the message that gets sent back and forth
struct message {
int code; //Just indicates 1 or 2 for readfile or writefile
int size;
char buffer[256]; //This will hold the filename when client sends readfile request
};
Works:
char *filename = "test.c";
infile = open(filename, O_RDONLY);
//Send the file and everything back to the client
Doesn't Work:
while( read(sockfd, &msg, sizeof(int) * 2) > 0) {
if(msg.code == 1) { //Perform a read operation
int infile, filesize, rr;
int length;
char output[256];
size_t rb = 0;
while(rb < msg.size) {
ssize_t r = read(sockfd, msg.buffer, msg.size - rb);
if(r < 0) {
error(sockfd, r, msg.buffer);
break;
}
rb += r;
}
msg.buffer[rb] = '\0';
//This has been printing out the correct amount
printf("\nBytes read: %i", rb);
//This has also been printing out properly
printf("\nmsg.buffer: %s", msg.buffer);
infile = open(msg.buffer, O_RDONLY);
I've edited to show the current state my program is in, and is still not working. Before, I had a strcpy that was improperly placed.