简体   繁体   English

通过套接字从客户端到服务器传递结构

[英]Passing structure over a Socket from client to server

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 (ie 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. 您需要做的是封送数据以进行传输,而各种RPC机制(OPC,DCE等)都可以很好地完成这一工作。

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: 例如,具有以下结构:

struct person {
    int age;
    char *name;
    char *addr;
} p;

you could do something like: 您可以执行以下操作:

msgbuff = outbuff = malloc (
    sizeof (int) +
    strlen (p.name) + 1 +
    strlen (p.addr) + 1
    );
if (msgbuff != NULL) {
    *((int*)outbuff) = p.age;  outbuf += sizeof (p.age);
    strcpy (outbuff, p.name) ; outbuf += strlen (p.name) + 1;
    strcpy (outbuff, p.addr) ; outbuf += strlen (p.addr) + 1;
    // Send msgbuff
    free (msgbuff);
} else {
    // Some error condition.
}

Note that an int is transferred directly since it's in the structure. 请注意,由于int 结构中,因此它是直接传输的。 For the character pointers (C strings), you have to get at the target of the pointer rather than the pointer itself. 对于字符指针(C字符串),您必须到达指针的目标,而不是指针本身。

Essentially, you transform: 本质上,您可以进行以下转换:

p:  age  (46)
    name (0x11111111)  -->  0x11111111: "paxdiablo"
    addr (0x22222222)  -->  0x22222222: "Circle 9, Hades"
   |--------------------|-------------------------------|
    structure memory <- | -> other memory

into: 变成:

msgbuff (0x88888888) -> {age}{"paxdiablo"}{"Circle 9, Hades"}

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 int types. 这使该过程有些复杂,因为您还必须在另一端进行封送处理,并且需要注意具有不同大小的int类型的系统。 But that's basically how it's done. 但这基本上就是完成的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM