用pack实现UDP包在PYTHON与C结构体透明通信
顺便改出两段简陋的code来说明这个问题.
/*
* udpsrv.c
*
* The test udp server, source code is stolen from UNP 3ed ED.
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
typedef struct my_data
{
char name[10];
int age;
float score;
} MY_DATA;
void dg_echo(int, struct sockaddr *, socklen_t);
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in svraddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
svraddr.sin_port = htons(3000);
bind(sockfd, (struct sockaddr *) &svraddr, sizeof(svraddr));
dg_echo(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));
}
void
dg_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
MY_DATA mydata;
bzero(&mydata, sizeof(MY_DATA));
for( ; ; ) {
len = clilen;
n = recvfrom(sockfd, &mydata, sizeof(MY_DATA), 0, pcliaddr, &len);
printf("Name:%s\tAge:%d\tScore:%f\n", mydata.name, mydata.age, mydata.score);
}
}
#!/usr/bin/python
# Filename: udpcli.py
# a simple udp client
import socket, struct
hostname = 'localhost'
port = 3000
host = socket.gethostbyname(hostname)
name = 'sleetdrop'
age = 18
score = 100
buf = struct.pack("10sif", name, age, score)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(buf, (host, port))
在linux上编译并起动udp服务器 $gcc udpsrv.c -o udpsrv $./udpsrv 在另一个term执行 $python udpcli.py 在服务器所在term会得到如下输出: Name:sleetdrop Age:18 Score:100.000000
