sendDataByUdp Function

本文介绍了一个使用C语言实现的UDP数据发送功能的具体代码实现过程。该功能通过建立UDP连接并发送指定长度的数据到指定服务器。文章详细展示了如何创建套接字、设置服务器地址及端口信息,并最终完成数据发送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sendDataByUdp Function

[@more@]
C语言: Codee#18774
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: %s n " , 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 socket n ");
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=%d n " , __FILE__ , __LINE__ , numbytes);
100     printf( "%s,%d: ========== len=%d n " , __FILE__ , __LINE__ , len);
101     if( numbytes != len)
102     {
103     ret = ADMIND_NETUPLOAD_EINSENDTO;
104         printf( "%s,%d: ========== ret=%d n " , __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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23168012/viewspace-1048950/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值