服务器
#include
#include
#include
#include
#include
#include
#define MAXLINE 1024
#define SERV_PORT 8000
struct MSG_PACK
{
char cmd;
char status;
short int len;
};
struct host
{
char UserName[20];
char Host[20];
char IP[20];
char PassWord [20];
char SubNet[512];
char Mode;
char ConState;
char Pin[20];
char ConTrol;
char RandPassword[10];
};
int main(void)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */
/* init servaddr */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
/* bind address and port to socket */
if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("bind error");
exit(1);
}
do_echo(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
close(sockfd);
return 0;
}
void do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for(;;)
{
len = clilen;
/* waiting for receive data */
n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
//mesg[n] ='/0';
printf ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^n:%d len=%d/n",n,len);
struct MSG_PACK pocket;
memcpy(&pocket,mesg,sizeof(struct MSG_PACK));
printf("$$$$$$$$$$$$$$/n-----pocket1------->%d/n",pocket.cmd);
printf("-----pocket2------->%d/n",pocket.status);
printf("-----pocket3------->%d/n$$$$$$$$$$$$$$$/n",pocket.len);
struct host sHost2 ;
memcpy(&sHost2 , mesg + sizeof(struct MSG_PACK) , sizeof(struct host));
printf("&&&&&&&&&&&&&&&&&&&&/n-----sHost1------->%s/n",sHost2.UserName);
printf("-----sHost2------->%s/n",sHost2.Host);
printf("-----sHost3------->%s/n",sHost2.IP);
printf("-----sHost4------->%s/n",sHost2.PassWord);
printf("-----sHost5------->%s/n",sHost2.SubNet);
printf("-----sHost6------->%d/n",sHost2.Mode);
printf("-----sHost7------->%d/n",sHost2.ConState);
printf("-----sHost8------->%s/n",sHost2.Pin);
printf("-----sHost9------->%d/n",sHost2.ConTrol);
printf("-----sHost10------->%s/n&&&&&&&&&&&&&&&&&&&&/n",sHost2.RandPassword);
/* sent data back to client */
n = sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define minornode 0 //从节点向主节点发送连接请求
#define hostnode_return 1 //主节点响应从节点的请求
#define minornode_change 2 //从节点向主节点发送更新请求
#define hostnode_returnchange 3 //主节点响应更新请求
//包头的定义
struct MSG_PACK
{
char cmd;
char status;
short int len;
};
//定义host
struct host
{
char UserName[20];
char Host[20];
char IP[20];
char PassWord [20];
char SubNet[512];
char Mode;
char ConState;
char Pin[20];
char ConTrol;
char RandPassword[10];
};
int main()
{
char sentbuf[1024];
struct host newhost;
memset(&newhost,0,sizeof(struct host));
strcpy(newhost.UserName,"aaaa");
strcpy(newhost.Host,"www");
strcpy(newhost.IP,"192.168.8.103");
strcpy(newhost.PassWord,"1111");
strcpy(newhost.SubNet,"255.255.255.0");
newhost.Mode=0;
newhost.ConState=1;
strcpy(newhost.Pin,"20");
newhost.ConTrol=0;
strcpy(newhost.RandPassword,"122122");
//printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@ %s %s %s %s %s %d %d %s %d %s @@@@@@@@@@@@@@@@@@@@@@@@@@@@ ",newhost.UserName,newhost.Host,newhost.IP,newhost.PassWord,newhost.SubNet,newhost.Mode,newhost.ConState,newhost.Pin,newhost.ConTrol,newhost.RandPassword);
struct MSG_PACK newpack;
newpack.cmd=1;
newpack.status=1;
newpack.len=sizeof(newhost);
//printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@ %d %d %d @@@@@@@@@@@@@@@@@@@@@@@@@@@ ",newpack.cmd,newpack.status,newpack.len);
memcpy(sentbuf,&newpack,sizeof(struct MSG_PACK));
memcpy(sentbuf+sizeof(struct MSG_PACK),&newhost,sizeof(struct host));
int iLen = sizeof(struct MSG_PACK)+ sizeof(struct host);
printf("-=-=-=-=-=-=-=-=-=-=-=-=%d ",iLen);
int sock;
//sendto中使用的对方地址
struct sockaddr_in servaddr;
//在recvfrom中使用的对方主机地址
struct sockaddr_in fromAddr;
unsigned int fromLen;
char recvBuffer[1024];
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("192.168.8.200");
servaddr.sin_port = htons(8000);
if(sendto(sock,sentbuf,iLen,0,(struct sockaddr*)&servaddr,sizeof(servaddr)) != iLen)
{
perror("sendto error");
close(sock);
exit(1);
}
fromLen = sizeof(fromAddr);
if(recvfrom(sock,recvBuffer,1024,0,(struct sockaddr*)&fromAddr,&fromLen)<0)
{
perror("recvfrom error");
close(sock);
exit(1);
}
struct MSG_PACK pocket;
memcpy(&pocket , recvBuffer , sizeof(struct MSG_PACK));
printf("-----pocket1------->%d ",pocket.cmd);
printf("-----pocket2------->%d ",pocket.status);
printf("-----pocket3------->%d ",pocket.len);
struct host sHost2 ;
memcpy(&sHost2 , recvBuffer + sizeof(struct MSG_PACK) , sizeof(struct host));
printf("&&&&&&&&&&&&&&&&&&&& -----sHost1------->%s ",sHost2.UserName);
printf("-----sHost2------->%s ",sHost2.Host);
printf("-----sHost3------->%s ",sHost2.IP);
printf("-----sHost4------->%s ",sHost2.PassWord);
printf("-----sHost5------->%s ",sHost2.SubNet);
printf("-----sHost6------->%d ",sHost2.Mode);
printf("-----sHost7------->%d ",sHost2.ConState);
printf("-----sHost8------->%s ",sHost2.Pin);
printf("-----sHost9------->%d ",sHost2.ConTrol);
printf("-----sHost10------->%s &&&&&&&&&&&&&&&&&&&& ",sHost2.RandPassword);
close(sock);
return 0;
}
阅读(7279) | 评论(0) | 转发(1) |