Chinaunix首页 | 论坛 | 博客
  • 博客访问: 309323
  • 博文数量: 20
  • 博客积分: 3011
  • 博客等级: 中校
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-12 10:01
文章分类

全部博文(20)

文章存档

2008年(20)

我的朋友

分类: LINUX

2008-03-18 19:16:43

//talker.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 5001
int main(int argc, char *argv[])
{
 int sockfd;
 struct sockaddr_in their_addr;
 struct hostent *he;
 int numbytes;


//----------------------------------------------------------------------------
// 最小字节对齐  1+8+1+1+4  =15个字节 ;attribute 对char 无效
struct aligned_test
{
char a  ;
unsigned int x[2] ;
char b;
char c;
unsigned int d ;
} __attribute__ ((packed));


// 默认排列 4 + 8 + 4(b ,c)+4  = 20个字节 //4字节对其
struct no_aligned_test
{
char a;
unsigned int x[2];
char b;
char c;
unsigned int d;
};
struct aligned_test  al_t;
struct no_aligned_test noal_t;
al_t.a='a';
al_t.x[0]=58;
al_t.x[1]=88;
al_t.b='b';
al_t.c='c';
al_t.d=236;

noal_t.a='a';
noal_t.x[0]=58;
noal_t.x[1]=99;
noal_t.b='b';
noal_t.c='c';
noal_t.d=236;
printf("sizeof(struct aligned_test)=%d\n",sizeof(struct aligned_test));
printf("sizeof(struct no_aligned_test)=%d\n",sizeof(struct no_aligned_test));
//--------------------------------------------------------------------

 if (argc != 3)
 {

  fprintf(stderr,"usage: talker hostname message\n");
  exit(1);
 }
 if ((he=gethostbyname(argv[1])) == NULL)
 {  
  herror("gethostbyname");
  exit(1);
 }
 if((sockfd=socket(AF_INET, SOCK_DGRAM, 0)) == -1)
 {

  perror ("socket");
  exit(1);
 }

 their_addr.sin_family = AF_INET;
 their_addr.sin_port = htons(MYPORT);
 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
 bzero(&(their_addr.sin_zero),8);
 //if ((numbytes=sendto(sockfd, argv[2], strlen(argv[2]), 0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)
 if ((numbytes=sendto(sockfd, &al_t, sizeof(struct aligned_test), 0,(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)
 {
  perror("recvfrom");
  exit(1);
 }
 printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr));
 close(sockfd);
 return 0;
}

 

//listener.c

 

//listener.c
#include
#include
#include
#include
#include
#include
#include
#include

#define MYPORT 5001

#define MAXBUFLEN 100
main()
{
int sockfd;
int i=0;
struct sockaddr_in my_addr;

struct sockaddr_in their_addr;
int addr_len, numbytes;
struct aligned_test
{
char a  ;
unsigned int x[2] ;
char b;
char c;
unsigned int d ;
} __attribute__ ((packed));


// 默认排列 4 + 8 + 4(b ,c)+4  = 20个字节 //4字节对其
struct no_aligned_test
{
char a;
unsigned int x[2];
char b;
char c;
unsigned int d;
};
struct aligned_test  al_t;
struct no_aligned_test noal_t;
//void *buf=&al_t;

char buf[MAXBUFLEN];
char *ptr;
ptr=buf;
memset(&al_t,0,sizeof(struct aligned_test));
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{

perror("socket");
exit(1);
}

my_addr.sin_family = AF_INET;

my_addr.sin_port = htons(MYPORT);

my_addr.sin_addr.s_addr = INADDR_ANY;

bzero(&(my_addr.sin_zero), 8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
{

perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);

while(1)
{

if ((numbytes=recvfrom(sockfd, &al_t, sizeof(struct aligned_test), 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1)
//if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0,
//(struct sockaddr *)&their_addr, &addr_len)) == -1)
{

perror("recvfrom");
exit(1);
}

printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n",numbytes);
//buf[numbytes] = '\0';
//printf("packet contains\"%s\"\n",buf);

/*
for(i=0;i<15;i++)
 {
 printf("%d\n",*ptr);
 ptr++;
 }
*/
/*
printf("al_t.a=%c\n",noal_t.a);
printf("al_t.x[0]=%d\n",noal_t.x[0]);
printf("al_t.x[1]=%d\n",noal_t.x[1]);
printf("al_t.b=%c\n",noal_t.b);
printf("al_t.c=%c\n",noal_t.c);
printf("al_t.d=%d\n",noal_t.d);
*/

printf("al_t.a=%c\n",al_t.a);
printf("al_t.x[0]=%d\n",al_t.x[0]);
printf("al_t.x[1]=%d\n",al_t.x[1]);
printf("al_t.b=%c\n",al_t.b);
printf("al_t.c=%c\n",al_t.c);
printf("al_t.d=%d\n",al_t.d);

memset(buf,0,sizeof(buf));
}
close(sockfd);
}

 

------------------

执行结果

//talker

sizeof(struct aligned_test)=15
sizeof(struct no_aligned_test)=20
sent 15 bytes to 201.201.201.249

//listener

got packet from 201.201.201.249
packet is 15 bytes long
al_t.a=a
al_t.x[0]=58
al_t.x[1]=88
al_t.b=b
al_t.c=c
al_t.d=236

 

阅读(1767) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~