发送端 client.c
//server1 发送数据
#include "stock.h"
#define MYPORT 8866 /*定义端口为8866*/
int main(int argc,char *argv[])
{
int sockfd,n,m;
struct sockaddr_in server_addr;
struct hostent *host;
//保证输入2个参数
if(argc!=2)
{
printf("Usage:%s server_ip server_port\n",argv[0]);
exit(1);
}
//gethostbyname可通过名称得到主机的IP地址
host=gethostbyname(argv[1]);
//创建套接字
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
printf("Socket Error:%s\n",strerror(errno));
exit(1);
}
//填充服务端的资料
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(MYPORT);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
int i,j;
struct stock_a
{
int code; //股票代码
int bprice[10]; //标价
int price[10]; //现价
};
struct stock_a M[100];
//randrom seed
srand(time(0));
///////初始化100只股票///////
for(i=0;i<100;i++)
{
//生成code
M[i].code=i;
//printf("%d\n",M[i].code);///打印股票代码
///生成价格//
for(j=0;j<10;j++)
{
M[i].bprice[j] = 100 + rand()%100;
M[i].price[j]=100 + rand()%100;
//printf(" %d %d\n",M[i].price[j],M[i].bprice[j]);
}
}
/*向服务器端发送数据报*/
int t;
char msg[1024];
for(i=0;i<100;i++)
{
for(j=0;j<3;j++)
{
if(j==0)
{
sprintf(msg,"%d",M[i].code);//转化成字符串,发送code
n=sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(struct sockaddr));
usleep(10000);
//发送失败
if(n<0)
{
printf("send code error!");
}
}
else if(j==1)
{
for(t=0;t<10;t++)
{
sprintf(msg,"%d",M[i].bprice[t]);//发送bprice
n=sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(struct sockaddr));
//发送失败
if(n<0)
{
printf("send bprice error!");
}
}
}
else if(j==2)
{
for(t=0;t<10;t++)
{
printf("%s A00%d :000000%d:000000%d\n","A111",M[i].code,M[i].bprice[t],M[i].price[t]);
sprintf(msg,"%d",M[i].price[t]);//发送price
n=sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(struct sockaddr));
//发送失败
if(n<0)
{
printf("send price error!");
}
}
}
}
}
printf("stock A send over!\n");
usleep(1000000);
/////股票C111
struct stock_c
{
int code; //股票代码
int bprice[10]; //标价
int price[10]; //现价
};
struct stock_c N[100];
///////初始化200只股票///////
for(i=0;i<100;i++)
{
//生成code
N[i].code=100+i;
//printf("%d\n",M[i].code);///打印股票代码
///生成价格//
for(j=0;j<10;j++)
{
N[i].bprice[j] = 100 + rand()%100;
N[i].price[j]=100 + rand()%100;
//printf(" %d %d\n",M[i].price[j],M[i].bprice[j]);
}
}
char ms[1024];
for(i=0;i<100;i++)
{
for(j=0;j<3;j++)
{
if(j==0)
{
sprintf(ms,"%d",N[i].code);//转化成字符串 发送code
m=sendto(sockfd,ms,sizeof(ms),0,(struct sockaddr *)&server_addr,sizeof(struct sockaddr));
usleep(10000);
//发送失败
if(m<0)
{
printf("send code error!");
}
}
else if(j==1)
{
for(t=0;t<10;t++)
{
sprintf(ms,"%d",N[i].bprice[t]);//转化成字符串 发送标价
m=sendto(sockfd,ms,sizeof(ms),0,(struct sockaddr *)&server_addr,sizeof(struct sockaddr));
}
//发送失败
if(m<0)
{
printf("send bprice error!");
}
}
else if(j==2)
{
for(t=0;t<10;t++)
{
printf("%s A00%d :0000000%d:0000000%d\n","C111",N[i].code,N[i].bprice[t],N[i].price[t]);
sprintf(ms,"%d",N[i].price[t]);//转化成字符串 发送现价
m=sendto(sockfd,ms,sizeof(ms),0,(struct sockaddr *)&server_addr,sizeof(struct sockaddr));
}
}
}
}
printf("stock C send over!\n");
close(sockfd);
return 0;
}
接收端server.c
//server2 接收数据
#include "stock.h"
#define MYPORT 8866 /*定义端口为8866*/
int main(void)
{
int sockfd; //创建socket
int n,m; //接收socket
int addrlen; //地址长度
int i,j,t;
//服务器地址信息
struct sockaddr_in server_addr;
//客户端地址信息
struct sockaddr_in client_addr;
//接收数据struct
struct stock_a
{
int mtype; //消息类型
int code; //code
int bprice[10]; //标价
int price[10]; //现价
};
struct stock_a M[100];
//创建数据报套接字
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
printf("Socket Error:%s\n",strerror(errno));
exit(1);
}
// 服务器端填充 sockaddr_in结构
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(MYPORT);
//绑定套接字
if(bind(sockfd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr))<0)
{
printf("Bind Error:%s\n",strerror(errno));
exit(1);
}
addrlen=sizeof(struct sockaddr);
printf("The server is waiting datas:\n");
// 接收客户端数据报
char msg[100];
for(i=0;i<100;i++)
{
for(j=0;j<3;j++)
{
if(j==0)
{
//接收数据code
n=recvfrom(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&client_addr,&addrlen);
// 将字符串转为整形
M[i].code = atoi(msg);
//接送数据失败
if(n<0)
{
printf("receive code error!");
}
}
else if(j==1)
{
for(t=0;t<10;t++)
{
//接收数据bprice
n=recvfrom(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&client_addr,&addrlen);
// 将字符串转为整形
M[i].bprice[t] = atoi(msg);
}
//接送数据失败
if(n<0)
{
printf("receive bprice error!");
}
}
else if(j==2)
{
for(t=0;t<10;t++)
{
//接收数据price
n=recvfrom(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&client_addr,&addrlen);
// 将字符串转为整形
M[i].price[t] = atoi(msg);
//打印接收到的数据
printf("%s A00%d:0000000%d:0000000%d\n","A111",M[i].code,M[i].bprice[t],M[i].price[t]);
}
//接送数据失败
if(n<0)
{
printf("receive price error!");
}
}
}
}
//stock A 接收完毕
printf("receive Stock A over!\n");
usleep(1000000);
/////////////////////////////////////////
//接收数据struct a
/////////////////////////////////////////
struct stock_c
{
int mtype; //消息类型
int code; //code
int bprice[10]; //标价
int price[10]; //现价
};
struct stock_c N[100];
char ms[100];
for(i=0;i<100;i++)
{
for(j=0;j<3;j++)
{
if(j==0)
{
//接收数据code
m=recvfrom(sockfd,ms,sizeof(ms),0,(struct sockaddr *)&client_addr,&addrlen);
// 将字符串转为整形
N[i].code = atoi(ms);
//接送数据失败
if(m<0)
{
printf("receive Stock C code error!");
}
}
else if(j==1)
{
for(t=0;t<10;t++)
{
//接收数据bprice
m=recvfrom(sockfd,ms,sizeof(ms),0,(struct sockaddr *)&client_addr,&addrlen);
// 将字符串转为整形
N[i].bprice[t] = atoi(ms);
//接送数据失败
if(m<0)
{
printf("receive Stock C bprice error!");
}
}
}
else if(j==2)
{
for(t=0;t<10;t++)
{
//接收数据price
m=recvfrom(sockfd,ms,sizeof(ms),0,(struct sockaddr *)&client_addr,&addrlen);
// 将字符串转为整形
N[i].price[t] = atoi(ms);
//接送数据失败
if(m<0)
{
printf("receive Stock C price error!");
}
//打印接收到的数据
printf("%s A00%d:0000000%d:0000000%d\n","C111",N[i].code,N[i].bprice[t],N[i].price[t]);
}
}
}
}
printf("receive Stock C over!\n");
//关闭套接字
close(sockfd);
printf("Message Queue Stock A Send!\n");
//Message queue 消息队列发送
//stock a
key_t key;
int msgid;
int ret;
struct stock_a tr;
key=ftok("/tmp/1",'c');
printf("key =[%d]\n",key);
//创建消息队列
msgid=msgget(key,IPC_CREAT|0666); //通过文件对应
if(msgid==-1)
{
printf("create error\n");
return -1;
}
for(i=0;i<100;i++)
{
tr.code = M[i].code;
tr.mtype = 100+i; //设置消息类型
for(j=0;j<10;j++)
{
tr.bprice[j]=M[i].bprice[j];
tr.price[j]=M[i].price[j];
}
//发送消息队列
ret=msgsnd(msgid,&tr,sizeof(tr),IPC_NOWAIT);
if(ret==-1)
{
printf("send message err\n");
}
}
printf("Message Queue Stock C Send!\n");
//stock c
struct stock_c cr;
for(i=0;i<100;i++)
{
cr.code = N[i].code;
cr.mtype = 200+i; //设置消息类型
for(j=0;j<10;j++)
{
cr.bprice[j]=N[i].bprice[j];
cr.price[j]=N[i].price[j];
}
//发送消息队列
ret=msgsnd(msgid,&cr,sizeof(cr),IPC_NOWAIT);
if(ret==-1)
{
printf("send message err\n");
}
}
}
阅读(8596) | 评论(0) | 转发(0) |