hey, i am trying to implement the function nfs_write(int fd, char* buff, int length) for my network file system. This function is supposed to write bytes to the file and return the number of bytes written. i kindof know the problem with the way i am trying to do it. but i think my method is a good way, i just need to know what i am doing wrong. Following is the code for this function (which is implemented in client side):
Code:
int nfs_write(int fd, char *buff, int length) {
typedef struct {
char *buff;
int length;
int fd;
} Block;
Block write[100];
write[0].buff = malloc(102400);
strcpy(write[0].buff,buff);
write[0].length = length;
write[0].fd = fd;
char *cmd = "write";
int i = find_server_using_fdt(fd); //this function just finds which server the filesystem is connected to
if (send(server[i].socket,cmd,strlen(cmd),0)==-1) {
perror("send");
exit(1);
}
if(send(server[i].socket,write,sizeof(write),0)==-1) {
printf("send 2: Error in write\n");
exit(1);
}
int num = recv(server[i].socket,write, sizeof(write),0);
if(num == -1) {
perror("recv");
exit(1);
}
num = write[0].length;
if (num == -1) {
fprintf(stderr, "nfs_write: Error in write\n");
return -1;
}
else {
printf("The number of bytes written to file with file descriptor %d: %d\n",fd,num);
return num;
}
}
so, what i am trying to do is send two send() commands. one with "write" command and one as a block structure (typedef struct Block), so that i can send the whole block at once which contains info like file descriptor, the buff that needs to be written and the size of the buffer. the problem is that when i send the block, the element of the Block struct which contains the buffer data is not being sent; only the pointer is sent. when i do send(server[i].socket, write,sizeof(write)), the write[0].buff data does not get sent(i get a segmentation fault on the server side which means it never received the buffer that was meant to be written on the file)!! i do not know how to solve this part. how can i modify the struct so that all the information inside it gets sent.
i would really appreciate your help. i am only left with the nfs_write and nfs_read function and i know once i figure out write, the read function will be easier to implement. hope to hear from u soon and if u need more info let me know.