sendDataByUdp Function

sgy618發表於2011-04-21

sendDataByUdp Function

[@more@]
C語言:
001 #include
002 #include
003 #include
004 #include
005 #include
006 #include
007 #include
008 #include
009 #include
010 #include
011
012 #define SERVERPORT "4950"   // the port users will be connecting to
013
014 int udp_upload_test(void *buf, uint len)
015 {
016     int sockfd;
017     struct addrinfo hints, *servinfo, *p;
018     int rv;
019     int numbytes;
020     int ret = 0;
021     char str[20];
022     memcpy(str,"192.168.88.58",20);
023
024
025     memset(&hints, 0, sizeof hints);
026     hints.ai_family = AF_UNSPEC;
027     hints.ai_socktype = SOCK_DGRAM;
028
029     if ((rv = getaddrinfo(str, SERVERPORT, &hints, &servinfo)) != 0) {
030         fprintf(stderr, "getaddrinfo: %sn", gai_strerror(rv));
031         return 1;
032     }
033
034     // loop through all the results and make a socket
035     for(p = servinfo; p != NULL; p = p->ai_next) {
036         if ((sockfd = socket(p->ai_family, p->ai_socktype,
037                 p->ai_protocol)) == -1) {
038             perror("talker: socket");
039             continue;
040         }
041
042         break;
043     }
044
045     if (p == NULL) {
046         fprintf(stderr, "talker: failed to bind socketn");
047         return 2;
048     }
049
050     if(g_nuManager.sockFd == -1)
051     {
052
053         if ((g_nuManager.sockFd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
054         {
055             dbgPrint(WARN, "creat sock for upload failed !!!");
056
057             ret = ADMIND_NETUPLOAD_EINCREATESOCKET;
058             return 2;
059         }
060     }
061 #if 0
062     if ((numbytes = sendto(g_nuManager.sockFd, buf, len, 0,
063              p->ai_addr, p->ai_addrlen)) == -1) {
064         perror("talker: sendto");
065         exit(1);
066     }
067
068     freeaddrinfo(servinfo);
069
070         printf("talker: sent %d bytes to %sn", numbytes, str);
071     close(sockfd);
072 #endif
073     struct sockaddr_in addrAlmCenter;
074     NetCfg* pNetCfg = NULL;
075
076     /* 獲得網路的所有配置 */
077     pNetCfg = cfgNetManagerGet();
078     if(pNetCfg == NULL)
079     {
080         dbgPrint(FATAL, "ADMIND_NETUPLOAD_EINGETNETCFG!!!");
081         ret = ADMIND_NETUPLOAD_EINGETNETCFG;
082         goto EXIT0;
083     }
084
085     bzero(&addrAlmCenter, sizeof(struct sockaddr_in));
086
087     addrAlmCenter.sin_family = AF_INET;
088     addrAlmCenter.sin_port = htons(UPLOAD_CENTER_PORT);
089
090     /* 是個主機序的地址 */
091     inet_pton(AF_INET,"192.168.88.58",&addrAlmCenter.sin_addr);
092     printf("%s,%d: *** s_addr=%x n",__FILE__,__LINE__,addrAlmCenter.sin_addr.s_addr);
093     addrAlmCenter.sin_addr.s_addr = htonl(pNetCfg->advance.nsAlertCfg.alertServerList[0].addr);
094     printf("%s,%d: *** s_addr=%x n",__FILE__,__LINE__,addrAlmCenter.sin_addr.s_addr);
095
096
097     printf("%s,%d: ========== n",__FILE__,__LINE__);
098     numbytes = sendto(g_nuManager.sockFd, buf, len, 0, (struct sockaddr*)&addrAlmCenter, sizeof(addrAlmCenter));
099     printf("%s,%d: ========== nSend=%dn",__FILE__,__LINE__,numbytes);
100     printf("%s,%d: ========== len=%dn",__FILE__,__LINE__,len);
101     if(numbytes != len)
102     {
103     ret = ADMIND_NETUPLOAD_EINSENDTO;
104         printf("%s,%d: ========== ret=%dn",__FILE__,__LINE__,ret);
105         goto EXIT0;
106     }
107
108     ret = APP_OK;
109 EXIT0:
110     return 0;
111 }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23168012/viewspace-1048950/,如需轉載,請註明出處,否則將追究法律責任。

相關文章