#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SA struct sockaddr
#define MAXLINE 4096
#define MAXSUB 2000
#define MAXPARAM 2048
#define LISTENQ 1024
extern int h_errno;
int sockfd;
char *hname = "yunpian.com";
char *send_sms_json = "/v1/sms/send.json";
char *get_user_json = "/v1/user/get.json";
/**
* 发http post请求
*/
ssize_t http_post(char *page, char *poststr)
{
char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
ssize_t n;
snprintf(sendline, MAXSUB,
"POST %s HTTP/1.0rn"
"Host: %srn"
"Content-type: application/x-www-form-urlencodedrn"
"Content-length: %zurnrn"
"%s", page, hname, strlen(poststr), poststr);
write(sockfd, sendline, strlen(sendline));
while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = '';
printf("%s", recvline);
}
return n;
}
/**
* 查账户信息
*/
ssize_t get_user(char *apikey)
{
char params[MAXPARAM + 1];
char *cp = params;
sprintf(cp,"apikey=%s", apikey);
return http_post(get_user_json, cp);
}
/**
* 使用通用接口发短信
*/
ssize_t send_sms(char *apikey, char *mobile, char *text)
{
char params[MAXPARAM + 1];
char *cp = params;
sprintf(cp,"apikey=%s&mobile=%s&text=%s", apikey, mobile, text);
return http_post(send_sms_json, cp);
}
int main(void)
{
struct sockaddr_in servaddr;
char **pptr;
char str[50];
struct hostent *hptr;
if ((hptr = gethostbyname(hname)) == NULL) {
fprintf(stderr, "通过域名获取IP失败: %s: %s",
hname, hstrerror(h_errno));
exit(1);
}
printf("域名: %sn", hptr->h_name);
if (hptr->h_addrtype == AF_INET
&& (pptr = hptr->h_addr_list) != NULL) {
printf("IP: %sn",
inet_ntop(hptr->h_addrtype, *pptr, str,
sizeof(str)));
} else {
fprintf(stderr, "Error call inet_ntop n");
exit(1);
}
//建立socket连接
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80);
inet_pton(AF_INET, str, &servaddr.sin_addr);
connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
//修改为您的apikey
char *apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//修改为您要发送的手机号
char *mobile = "188xxxxxxxx";
//设置您要发送的内容
char *text = "您的验证码是1234";
/**************** 查账户信息调用示例 *****************/
get_user(apikey);
/**************** 使用通用接口发短信 *****************/
//send_sms(apikey, mobile, text);
close(sockfd);
exit(0);
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
mysql 连接数太多(SequelizeConnectionError: Too many connections) 这个就是最常见的问题了。因为我做的这个是前端监控系统,日志上报量比较大,所以经常会遇到连接数不够用的情况。 除了你要使用其他技术来缓解并发量…