kb_net.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:6k
- //*****************************************************************************
- //文件名:kb_net.c
- //
- //描 述 :网口处理
- //
- //作 者:韩彦锋
- //
- //日 期: 2006.12.25
- //
- //版 本: v1.0
- //*****************************************************************************
- #include "kb_net.h"
- #include "osp.h"
- #include "tcpip.h"
- #include "kb_dc.h"
- #include "kb_machblue_client_vote.h"
- static void kb_NETTask(void);
- //////////////投票系统所需的函数//////////////
- extern BOOL kb_vote_info_send(kb_vote_t *pVote);
- extern BOOL kb_vote_result_get(unsigned char *pName,kb_vote_info_t * kb_voteResult);
- ///////////////////////////////////////////////////////
- //*****************************************************************************
- //名称:KB_NETInit
- //
- //功能:初始化网口模块
- //
- //输入:无
- //
- //返回:RETOK 成功
- // others 失败
- //*****************************************************************************
- INT32 KB_NETInit(void)
- {
- UINT8 hwMac[6]={'18','11','22','33','44','55'};
- unsigned int ip,mask,gw;
- int rel;
- UINT32 _netQueueID;
- UINT32 _netTaskID;
- int tmp,addr=0x41000000;
- ip=0xC0A80918;
- mask=0xFFFFFF00;
- gw=0xC0A80905;
- rel=netif_init(ip,mask,gw,hwMac);
- if(rel!=1)
- {
- return RETFIAL1;
- }
-
-
- //create task
- KB_OSPQueInit("NET", 128, &_netQueueID);
- KB_RegQueID(_netQueueID);
- KB_OSPTaskInit("NET",1024 * 128,(void (*)(void*))kb_NETTask,MENU_PRIORITY,NULL,&_netTaskID);
- return RETOK;
- }
- //*****************************************************************************
- //名称:kb_NETTask
- //
- //功能:网络任务处理
- //
- //输入:无
- //
- //返回:无
- //*****************************************************************************
- static void kb_NETTask(void)
- {
- struct sockaddr_in tcpc_a,tcpc_address;
- char data[2000];
- int len, i=0, j=0 ,tmp;
- int str[500];
- char *p;
- int _socket;
- struct sockaddr name;
- int namelen;
-
- while(getip()==0)
- task_delay(ST_GetClocksPerSecond()*1);
- }
- /*
- *Send note infomation to server
-
- *pVote <vote information>
- *@return None
- */
- BOOL kb_vote_info_send(kb_vote_t *pVote)
- {
- int tmp;
- char buf;
- int _socket;
- struct sockaddr_in tcpc_address;
- kb_vote_t kb_vote;
- _socket = socket(AF_INET,SOCK_STREAM,0);/* create _socket */
- //printf("n [kb_vote_info_send]:_socket()-0= %d n",_socket);
- if(_socket<0)
- return FALSE;
- tcpc_address.sin_family=AF_INET;//以太网协议//PF_INET 广域网协议(暂时还用不到)
- tcpc_address.sin_port=htons(6000);
- tcpc_address.sin_addr.s_addr=0xc0a809fd;//0xC0A80905;//192.168.0.100;//inet_addr(srvr_addr);
- tmp = connect(_socket, (struct sockaddr *)&tcpc_address, sizeof(struct sockaddr_in));
- printf("n [kb_vote_info_send]:connect--------tmp=%d",tmp);
- if(tmp!=0)
- {
- return FALSE;
- }
- kb_vote.index=pVote->index;
- kb_vote.ip=getip();
- strcpy(kb_vote.name, pVote->name);
- kb_vote.count = pVote->count;
- printf("n [kb_vote_info_send]:kb_vote.count--------=%d",kb_vote.count);
- tmp=send(_socket, &kb_vote, sizeof(kb_vote), 0);
- if(tmp!=sizeof(kb_vote))
- return FALSE;
- tmp=recv(_socket,&buf,sizeof(char), 0);
- //printf("n [kb_vote_info_send]:[confirm message after vote]recv------tmp=%d,buf=%d",tmp,buf);
- if(tmp==1)
- return TRUE;
- else
- return FALSE;
- }
- /*
- *Get vote result from server
- *pName <name of vote movie>
- *@return None
- */
- BOOL kb_vote_result_get(unsigned char *pName,kb_vote_info_t * kb_voteResult)
- {
- int tmp,i,buf;
- int _socket;
- struct sockaddr_in tcpc_address;
- kb_vote_info_t kb_voteResulttemp;
-
- _socket = socket(AF_INET,SOCK_STREAM,0);/* create _socket */
- //printf("n [kb_vote_result_get]:_socket()-0= %d n",_socket);
- if(_socket<0)
- return FALSE;
- tcpc_address.sin_family=AF_INET;//以太网协议//PF_INET 广域网协议(暂时还用不到)
- tcpc_address.sin_port=htons(6000);
- tcpc_address.sin_addr.s_addr=0xc0a809fd;//0xC0A80905;//192.168.0.100;//inet_addr(srvr_addr);
- tmp = connect(_socket, (struct sockaddr *)&tcpc_address, sizeof(struct sockaddr_in));
- printf("n [kb_vote_result_get]:connect--------tmp=%d",tmp);
- if(tmp!=0)
- return FALSE;
- //printf("n [kb_vote_result_get]:_socket to ask for result is:[%d],pName:[%s]",_socket,pName);
- tmp=send(_socket, pName, 60, 0);
- //printf("n [kb_vote_result_get]:send------tmp=%d",tmp);
- if(tmp!=60)
- return FALSE;
-
- tmp=recv(_socket,&kb_voteResulttemp,sizeof(kb_voteResulttemp), 0);
- if(tmp!=sizeof(kb_voteResulttemp))
- return FALSE;
- /*
- printf("n [kb_vote_result_get]:recv------tmp=%d",tmp);
- printf("n [kb_vote_result_get]:[before copy]name:[%s],vote count:[%d]",kb_voteResulttemp.name,kb_voteResulttemp.resCount);
- for(i=0;i<kb_voteResulttemp.resCount;i++)
- {
- printf("n [kb_vote_result_get]:[before copy]num:[%d],index:[%d],count:[%d]",i,kb_voteResulttemp.result[i].index,kb_voteResulttemp.result[i].result);
- }
- */
-
- strcpy(kb_voteResult->name,kb_voteResulttemp.name,sizeof(kb_voteResulttemp.name));
- kb_voteResult->resCount=kb_voteResulttemp.resCount;
- for(i=0;i<kb_voteResult->resCount;i++)
- {
- kb_voteResult->result[i].index=kb_voteResulttemp.result[i].index;
- kb_voteResult->result[i].result=kb_voteResulttemp.result[i].result;
- }
- /*
- printf("n [kb_vote_result_get]:[after copy] name:[%s],vote count:[%d]",kb_voteResult->name,kb_voteResult->resCount);
- for(i=0;i<kb_voteResulttemp.resCount;i++)
- {
- printf("n [kb_vote_result_get]:[after copy]num:[%d],index:[%d],count:[%d]",i,kb_voteResult->result[i].index,kb_voteResult->result[i].result);
- }
- */
- tmp=recv(_socket,&buf,sizeof(char), 0);
- //printf("n [kb_vote_result_get]:[confirm message if you want the result]recv------tmp=%d,buf=%d",tmp,buf);
- if(tmp==1)
- return TRUE;
- else
- return FALSE;
- }
- /* EOF... */