I need to code for uploading a file from client to server.
In client side, i have declared a structure,
typedef struct upload_file
{
char *filename;
char *filebuffer;
}upload_file;
I am getting filename from command line argument.
In main function, I am assigning the file name with structure variable.
upload_file.filename = argv[1];
Then I am reading the file content, putting into a buffer & copying that to structure’s buffer value.
strcpy(upld.buffer,tmp); //tmp is a buffer which will contain the file content
After that I am writing the structure into socket as follows,
write(sd, &upld, sizeof(upld));
This part is fine with the client side. In the server side if I read the whole structure & how do i separate the file content & file name?
Also , the buffer value (i.e file content ) from client side is malloced & could it be available to server side?
How do to this?
Thanks in advance.
Passing a structure with pointers in it is useless. The pointers themselves will be sent across but not the things they point to.
What you need to do is marshal the data for transmission, something which the various RPC mechanisms (OPC, DCE, etc) do quite well.
If you can’t use an established method like that, it’s basically a matter of going through the structure element by element, copying the targets into a destination buffer.
For example, with the structure:
you could do something like:
Note that an
intis transferred directly since it’s in the structure. For the character pointers (C strings), you have to get at the target of the pointer rather than the pointer itself.Essentially, you transform:
into:
This complicates the process a little since you also have to unmarshal at the other end, and you’ll need to watch out for system that have different sized
inttypes. But that’s basically how it’s done.