python socket receive c socket send struct

python socket receive c socket send struct

https://pymotw.com/2/socket/binary.html

http://blog.csdn.net/a77248848/article/details/3013138

https://pymotw.com/2/socket/binary.html

https://docs.python.org/2/library/struct.html

http://www.runoob.com/python/python-lists.html

http://www.cnblogs.com/xudong-bupt/p/3496741.html

http://blog.csdn.net/u013485792/article/details/52807056

http://blog.csdn.net/zz460833359/article/details/51765236

http://www.pythonpub.com/article/668

import binascii
import socket
import struct
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 666)
sock.bind(server_address)
sock.listen(1)

unpacker = struct.Struct('I I c f 3s')

while True:
    print >>sys.stderr, '\nwaiting for a connection'
    connection, client_address = sock.accept()
    try:
        data = connection.recv(unpacker.size)
        print >>sys.stderr, 'received "%s"' % binascii.hexlify(data)

        unpacked_data = unpacker.unpack('IIcf3s',data)
        print >>sys.stderr, 'unpacked:', unpacked_data
        print >>sys.stderr, 'unpacked1:', unpacked_data[0]
	print >>sys.stderr, 'unpacked2:', unpacked_data[1]
        print >>sys.stderr, 'unpacked3:', unpacked_data[2]
	print >>sys.stderr, 'unpacked4:', unpacked_data[3]
        print >>sys.stderr, 'unpacked5:', unpacked_data[4]
        #print('unpacked5:',unpacked_data[4])

    finally:
        connection.close()
#include <sys/types.h>
#include <sys/socket.h>                         // 包含套接字函数库
#include <stdio.h>
#include <netinet/in.h>                         // 包含AF_INET相关结构
#include <arpa/inet.h>                      // 包含AF_INET相关操作的函数
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <pthread.h>

#define MYPORT  666
#define BUFFER_SIZE 1024

typedef struct
{
    int ab;
    int num;
    char group;
    float score;
    char *name;
    char add[3];
}Node;

int main()
{
        ///sockfd
    int sock_cli = socket(AF_INET,SOCK_STREAM, 0);

    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
       servaddr.sin_port = htons(MYPORT);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        perror("connect");
        exit(1);
    }

    Node *myNode=(Node*)malloc(sizeof(Node));
    myNode->ab=123;
    myNode->num=110;
    myNode->group='A';
    myNode->score=136.5;
    strcpy(myNode->name,"TOM");
    printf("name is %s",myNode->name);
    int needSend=sizeof(Node);
    char *buffer=(char*)malloc(needSend);
    memcpy(buffer,myNode,needSend);

    int pos=0;
    int len=0;
    while(pos < needSend)
    {
        len=send(sock_cli, buffer+pos, BUFFER_SIZE,0);
        if(len <= 0)
        {
            perror("ERRPR");
            break;
        }
        pos+=len;
    }
    free(buffer);
    free(myNode);
    close(sock_cli);
    printf("Send over!!!\n");
    printf("add is %s",myNode->add);
    return 0;
}

发表评论

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