C
C代码示例
//接口类型:达信通语音验证码接口。 //账户注册:请通过该地址开通账户http://sms.wx96.com/register.html //注意事项: //(1)调试期间,请仔细阅读接口文档; //(2)请使用 用户名(例如:cf_demo123)及 APIkey来调用接口,APIkey在会员中心可以获取; //(3)该代码仅供接入达信通短信接口参考使用,客户可根据实际需要自行编写; #include #include #include #include #include #include #include #include #include #include #include #include #include #define IPSTR "api.voice.wx96.com" #define PORT 80 #define BUFSIZE 1024 //调试日期 2015-07-27 //linux下的编译方法 gcc -o sms sms.c int main(int argc, char **argv) { int sockfd, ret, i, h,srandnum; struct sockaddr_in servaddr; char str1[4096], str2[4096], buf[BUFSIZE], *str; socklen_t len; fd_set t_set1; struct timeval tv; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { printf("创建网络连接失败,本线程即将终止---socket error!\n"); exit(0); }; bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); if (inet_pton(AF_INET, IPSTR, &servaddr.sin_addr) <= 0 ){ printf("创建网络连接失败,本线程即将终止--inet_pton error!\n"); exit(0); }; if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ printf("连接到服务器失败,connect error!\n"); exit(0); } printf("与远端建立了连接\n"); //发送数据 //用户名是登录wx96.com账号名(例如:cf_demo123) //查看密码请登录用户中心->语音验证码->帐户及签名设置->APIKEY memset(str2, 0, 4096); strcat(str2, "account=用户名&password=密码&mobile=189xxxxxxxx&content=1125"); str=(char *)malloc(128); len = strlen(str2); sprintf(str, "%d", len); memset(str1, 0, 4096); strcat(str1, "POST /webservice/voice.php?method=Submit&format=json HTTP/1.1\n"); strcat(str1, "Host: api.voice.wx96.com\n"); strcat(str1, "Content-Type: application/x-www-form-urlencoded\n"); strcat(str1, "Content-Length: "); strcat(str1, str); strcat(str1, "\n\n"); strcat(str1, str2); strcat(str1, "\r\n\r\n"); printf("%s\n",str1); ret = write(sockfd,str1,strlen(str1)); if (ret < 0) { printf("发送失败!错误代码是%d,错误信息是'%s'\n",errno, strerror(errno)); exit(0); }else{ printf("消息发送成功,共发送了%d个字节!\n\n", ret); } FD_ZERO(&t_set1); FD_SET(sockfd, &t_set1); while(1){ sleep(2); tv.tv_sec= 0; tv.tv_usec= 0; h = 0; printf("--------------->1\r\n"); h = select(sockfd +1, &t_set1, NULL, NULL, &tv); printf("--------------->2%d\r\n",h); //if (h == 0) continue;//break; if (h == 0) { close(sockfd); printf("连接关闭!\n"); return 1; }; if (h < 0) { close(sockfd); printf("在读取数据报文时SELECT检测到异常,该异常导致线程终止!\n"); return -1; }; if (h > 0){ memset(buf, 0, 4096); i= read(sockfd, buf, 4095); if (i==0){ close(sockfd); printf("读取数据报文时发现远端关闭,该线程终止!\n"); return -1; } printf("%s\n", buf); } } close(sockfd); return 0; }