<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>憂藍夢境‧部落格 &#187; 傳簡訊</title>
	<atom:link href="http://blog.linym.net/archives/tag/%e5%82%b3%e7%b0%a1%e8%a8%8a/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.linym.net</link>
	<description>我的學習心得、筆記</description>
	<lastBuildDate>Fri, 09 Dec 2011 12:33:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>TwSMS 發簡訊 (Linux C 版)</title>
		<link>http://blog.linym.net/archives/216</link>
		<comments>http://blog.linym.net/archives/216#comments</comments>
		<pubDate>Sun, 20 Jan 2008 17:39:20 +0000</pubDate>
		<dc:creator>lym520</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[程式筆記]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[TwSMS]]></category>
		<category><![CDATA[傳簡訊]]></category>

		<guid isPermaLink="false">http://blog.linym.net/archives/216</guid>
		<description><![CDATA[台灣簡訊(TwSMS)是國內一家線上傳簡訊的服務商，提供文字簡訊、語音簡訊等服務，價格也很合理，最重要的是有提供 API 介面，方便用戶在自己的程式中加入發送簡訊功能，官網已經有提供不少範例(PHP/ASP/JSP/Java/Perl/VB/BCB/Delphi)，這邊也有 Ruby 的版本，不過就是沒看到 C 的，所以大略寫了一個 Linux C 版本，打算加入自己的嵌入式專題使用。 TwSMS 提供的 API 很簡單，只要由 HTTP 對 API server 發送 Request 即可，接著 server 就會回傳結果。程式先建立一個 socket 連線，然後發送簡訊，最後再擷取回傳碼檢查是否成功。 #include &#60;stdio.h&#62; #include &#60;stdlib.h&#62; #include &#60;string.h&#62; #include &#60;sys/socket.h&#62; #include &#60;netinet/in.h&#62; #include &#60;arpa/inet.h&#62; #include &#60;netdb.h&#62; int main() { /* TWSMS 相關設定 */ char *username = &#34;username&#34;; // 帳號 char *password = [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.twsms.com/" target="_blank">台灣簡訊</a>(TwSMS)是國內一家線上傳簡訊的服務商，提供文字簡訊、語音簡訊等服務，價格也很合理，最重要的是有提供 API 介面，方便用戶在自己的程式中加入發送簡訊功能，官網已經有提供不少範例(PHP/ASP/JSP/Java/Perl/VB/BCB/Delphi)，<a href="http://github.com/cfc/twsmsr/tree/master" target="_blank">這邊</a>也有 Ruby 的版本，不過就是沒看到 C 的，所以大略寫了一個 Linux C 版本，打算加入自己的嵌入式專題使用。</p>
<p>TwSMS 提供的 API 很簡單，只要由 HTTP 對 API server 發送 Request 即可，接著 server 就會回傳結果。程式先建立一個 socket 連線，然後發送簡訊，最後再擷取回傳碼檢查是否成功。</p>
<pre title="code" class="cpp">

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;

int main()
{
	/* TWSMS 相關設定 */
	char *username = &quot;username&quot;;   // 帳號
	char *password = &quot;password&quot;; // 密碼
	char *type = &quot;now&quot;;         // 發送型態
	char *mobile = &quot;0912xxxxxx&quot;; // 電話
	char *message = &quot;簡訊測試&quot;; // 簡訊內容
	char *encoding = &quot;big5&quot;;    // 簡訊內容編碼
	char *popup = &quot;&quot;;          // 使用 POPUP 顯示
	char *mo = &quot;&quot;;             // 使用雙向簡訊
	char *vldtime = &quot;86400&quot;;    // 簡訊有效期限(秒)
	char *dlvtime = &quot;&quot;;         // 預約時間

	int sockfd;
	int len = 0;
	char *host = &quot;api.twsms.com&quot;;
	char msg[512], MSGData[512], buf[512];
	char *res, *checkRes;
	struct sockaddr_in address;
	struct hostent *hostinfo;

	bzero(&amp;address, sizeof(address));
	hostinfo = gethostbyname(host);
	if (!hostinfo) {
		fprintf(stderr, &quot;no host: %s\n&quot;, host);
		exit(1);
	}
	address.sin_family = AF_INET;
	address.sin_port = htons(80);
	address.sin_addr = *(struct in_addr *)*hostinfo-&gt;h_addr_list;

	/* Create socket */
	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	/* Connect to server */
	if (connect(sockfd, (struct sockaddr *)&amp;address, sizeof(address)) == -1) {
		perror(&quot;connect faild!\n&quot;);
		exit(1);
	}

	/* Request string */
	len = snprintf(msg, 512,
				  &quot;username=%s&amp;password=%s&amp;type=%s&amp;encoding=%s&amp;popup=%s&amp;mo=%s&amp;mobile=%s&quot;
				  &quot;&amp;message=%s&amp;vldtime=%s&amp;dlvtime=%s&quot;, username, password, type, encoding,
				  popup, mo, mobile, message, vldtime, dlvtime);

	/* HTTP request content */
	snprintf(MSGData, 512,
			&quot;POST /send_sms.php HTTP/1.1\r\n&quot;
			&quot;Host: %s\r\n&quot;
			&quot;Content-Length: %d\r\n&quot;
			&quot;Content-Type: application/x-www-form-urlencoded\r\n&quot;
			&quot;Connection: Close\r\n\r\n&quot;
			&quot;%s\r\n&quot;, host, len, msg);

	/* Send message */
	send(sockfd, MSGData, strlen(MSGData), 0);

	/* Response message */
	recv(sockfd, buf, 512, 0);

	for (res = strtok(buf, &quot;\n&quot;); strncmp(res, &quot;resp&quot;, 4) != 0; res = strtok(NULL, &quot;\n&quot;));
	checkRes = strtok(res, &quot;=&quot;);
	checkRes = strtok(NULL, &quot;=&quot;);

	if (atoi(checkRes) &lt;= 0) {
		printf(&quot;傳送失敗\n&quot;);
	} else {
		printf(&quot;傳送完成\n&quot;);
	}

	close(sockfd);
	return 0;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.linym.net/archives/216/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

