linux socket 发送 struct

在linux下发送数据大家都是很熟悉的了,但是发送的数据是结构体该怎么办呢?

办法很简单,但是考虑到平台兼容性,你可能需要自定义一些数据类型。

下面两个文件是server.c和client.c,分别表示服务器端和客户端。

server.c 文件:

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
    int sfp,
    nfp;
    struct sockaddr_in s_add,
    c_add;
    int sin_size;
    unsigned short portnum = 8884;
    printf("Hello,welcome to my server !\r\n");
    sfp = socket(AF_INET, SOCK_STREAM, 0);
    if ( - 1 == sfp) {
        printf("socket fail ! \r\n");
        return - 1;
    }
    printf("socket ok !\r\n");

    bzero( & s_add, sizeof(struct sockaddr_in));
    s_add.sin_family = AF_INET;
    s_add.sin_addr.s_addr = htonl(INADDR_ANY);
    s_add.sin_port = htons(portnum);

    if ( - 1 == bind(sfp, (struct sockaddr * )( & s_add), sizeof(struct sockaddr))) {
        printf("bind fail !\r\n");
        return - 1;
    }
    printf("bind ok !\r\n");

    if ( - 1 == listen(sfp, 5)) {
        printf("listen fail !\r\n");
        return - 1;
    }
    printf("listen ok\r\n");
    while (1) {
        sin_size = sizeof(struct sockaddr_in);

        nfp = accept(sfp, (struct sockaddr * )( & c_add), &sin_size);
        if ( - 1 == nfp) {
            printf("accept fail !\r\n");
            return - 1;
        }
        printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n", ntohl(c_add.sin_addr.s_addr), ntohs(c_add.sin_port));
                struct protocol {
            int type;
            char sim[5];
            char getwayip[16];
        };
        struct protocol p_t;
        struct protocol * p = &p_t;
        memset(p, 0, sizeof(struct protocol));
        p->type = 32;
        strcpy(p->sim, "test");
        //if(-1 == write(nfp,"hello,welcome to my server \r\n",32))
        if ( - 1 == write(nfp, p, sizeof(struct protocol))) {
            printf("write fail!\r\n");
            return - 1;
        }
        printf("write ok!\r\n");
        close(nfp);
    }
    close(sfp);
    return 0;
}

 

client.c文件:

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main() {
    int cfd;
    int recbytes;
    int sin_size;
    char buffer[1024] = {
        0
    };
    struct sockaddr_in s_add,
    c_add;
    unsigned short portnum = 8884;
    printf("Hello,welcome to client !\r\n");

    cfd = socket(AF_INET, SOCK_STREAM, 0);
    if ( - 1 == cfd) {
        printf("socket fail ! \r\n");
        return - 1;
    }
    printf("socket ok !\r\n");

    bzero( & s_add, sizeof(struct sockaddr_in));
    s_add.sin_family = AF_INET;
    s_add.sin_addr.s_addr = inet_addr("127.0.0.1");
    s_add.sin_port = htons(portnum);
    printf("s_addr = %#x ,port : %#x\r\n", s_add.sin_addr.s_addr, s_add.sin_port);

    if ( - 1 == connect(cfd, (struct sockaddr * )( & s_add), sizeof(struct sockaddr))) {
        printf("connect fail !\r\n");
        return - 1;
    }
    printf("connect ok !\r\n");
    struct data {
        int a;
        char b;
    };
    struct protocol {
            int type;
            char sim[5];
            char getwayip[16];
        };
        struct protocol p_t;
        struct protocol * p = &p_t;
    memset(p, 0, sizeof(struct protocol));
    //if(-1 == (recbytes = read(cfd,buffer,1024)))
    //if(-1 == (recbytes = read(cfd,&data_t,sizeof(struct data))))
    if ( - 1 == (recbytes = read(cfd, p, sizeof(struct protocol)))) {
        printf("read data fail !\r\n");
        return - 1;
    }
    printf(" type= %d,sim = %s\n", p->type, p->sim);
}

编译:

gcc -o server server.c
gcc -o client client.c

 

运行:
server:

./server 
Hello,welcome to my server !
socket ok !
bind ok !
listen ok
accept ok!
Server start get connect from 0x7f000001 : 0xdfb8
write ok!
accept ok!
Server start get connect from 0x7f000001 : 0xdfb9
write ok!
accept ok!
Server start get connect from 0x7f000001 : 0xdfba
write ok!
accept ok!
Server start get connect from 0x7f000001 : 0xdfbb
write ok!
^C

client:

 ./client 
Hello,welcome to client !
socket ok !
s_addr = 0x100007f ,port : 0xb422
connect fail !

============================
Hello,welcome to client !
socket ok !
s_addr = 0x100007f ,port : 0xb422
connect ok !
 type= 32,sim = test

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>