<?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.ez2learn.com/tag/%e8%99%9b%e6%93%ac%e4%b8%bb%e6%a9%9f/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ez2learn.com</link>
	<description>Victor&#039;s個人部落格，關於程式設計與小提琴</description>
	<lastBuildDate>Tue, 07 Feb 2012 03:26:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WebFaction能不能跑自己的C/C++語言Server?</title>
		<link>http://blog.ez2learn.com/2009/07/30/run-self-compiled-c-cpp-server-on-webfaction/</link>
		<comments>http://blog.ez2learn.com/2009/07/30/run-self-compiled-c-cpp-server-on-webfaction/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 13:11:34 +0000</pubDate>
		<dc:creator>victor</dc:creator>
				<category><![CDATA[中文文章]]></category>
		<category><![CDATA[分享]]></category>
		<category><![CDATA[主機商]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[自行編譯]]></category>
		<category><![CDATA[虛擬主機]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Virtual host]]></category>
		<category><![CDATA[WebFaction]]></category>

		<guid isPermaLink="false">http://blog.ez2learn.com/?p=697</guid>
		<description><![CDATA[介紹如何在WebFaction虛擬主機下跑自行編譯的C/C++ Server <a href="http://blog.ez2learn.com/2009/07/30/run-self-compiled-c-cpp-server-on-webfaction/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>我之前寫了這篇<a href="http://blog.ez2learn.com/2009/07/19/webfaction-plans/">WebFaction不止是虛擬主機</a>的介紹，介紹關於<a href="http://www.webfaction.com/?affiliate=victorlin">WebFaction</a>雖然是虛擬主機，但是他能做得比虛擬主機多太多了，價格實惠，功能強大，有人問<a href="http://www.webfaction.com/?affiliate=victorlin">WebFaction</a>能不能跑自己的C/C++語言寫的Server，<strong>答案是當然可以</strong>，這點有明確地列在他們開出來的規格裡，為了示範如何做以及證明的確可以，我把我之前修網路程式設計寫的無聊Server丟上去編譯並執行給大家看</p>
<p><span id="more-697"></span></p>
<p>首先是伺服器的部份，因為只是作業，我寫的是一個很無聊的伺服器，client會丟一個數字n過來，而server會計算出這個數字在費伯那契數列中的第n個數然後回傳，只是為了示範，所以只以簡單為主，以下為程式碼片段:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #000080;">&lt;</span><span style="color: #000040;">!</span><span style="color: #008000;">&#91;</span>CDATA<span style="color: #008000;">&#91;</span>
<span style="color: #339900;">#include &quot;tcp_socket.h&quot;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/**
    @brief Handle incoming request
    @param [in] Socket to handle
**/</span>
<span style="color: #0000ff;">void</span> handleRequest<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> s<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    string request <span style="color: #000080;">=</span> readString<span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// remove \n</span>
    request <span style="color: #000080;">=</span> request.<span style="color: #007788;">substr</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, request.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    stringstream stream<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">size_t</span> n<span style="color: #008080;">;</span>
    stream <span style="color: #000080;">&lt;&lt;</span> request<span style="color: #008080;">;</span>
    stream <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Receved request : &quot;</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">size_t</span> i<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> a <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> b <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">int</span> temp <span style="color: #000080;">=</span> a <span style="color: #000040;">+</span> b<span style="color: #008080;">;</span>
        a <span style="color: #000080;">=</span> b<span style="color: #008080;">;</span>
        b <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#123;</span>
        stringstream stream<span style="color: #008080;">;</span>
        stream <span style="color: #000080;">&lt;&lt;</span> b<span style="color: #008080;">;</span>
        writeString<span style="color: #008000;">&#40;</span>s, stream.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">try</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>argc <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;usage: fib interface port&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">const</span> string interface <span style="color: #000080;">=</span> argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #666666;">// get port</span>
        <span style="color: #0000ff;">int</span> port<span style="color: #008080;">;</span>
        stringstream str<span style="color: #008080;">;</span>
        str <span style="color: #000080;">&lt;&lt;</span> argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        str <span style="color: #000080;">&gt;&gt;</span> port<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// listen</span>
        <span style="color: #0000ff;">int</span> s <span style="color: #000080;">=</span> listenTCP<span style="color: #008000;">&#40;</span>port, <span style="color: #0000dd;">5</span>, interface<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Listing on &quot;</span> <span style="color: #000080;">&lt;&lt;</span> interface <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> port <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; ...&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">;;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #666666;">// accept</span>
            sockaddr_in addr<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">size_t</span> len <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>addr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">int</span> client <span style="color: #000080;">=</span> accept<span style="color: #008000;">&#40;</span>s, <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>sockaddr <span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>addr<span style="color: #008000;">&#41;</span>, <span style="color: #000040;">&amp;</span>len<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>client <span style="color: #000080;">==</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                THROW<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;A connection from &quot;</span> <span style="color: #000080;">&lt;&lt;</span> ipAsString<span style="color: #008000;">&#40;</span>addr.<span style="color: #007788;">sin_addr</span>.<span style="color: #007788;">s_addr</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            handleRequest<span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Close connection&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
            close<span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">catch</span><span style="color: #008000;">&#40;</span>SocketError <span style="color: #000040;">&amp;</span>e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        e.<span style="color: #007788;">print</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span></pre></div></div>

<p>有了程式碼之後，我們要如何把它丟到<a href="http://www.webfaction.com/?affiliate=victorlin">WebFaction</a>上面跑呢? 這部份比較討人厭，我個人不是很喜歡在unix-like的環境下開發C/C++程式，因為實在太麻煩了，各家OS的header都不盡相同，文件也七零八落，不知道去哪裡找，不像M$雖然邪惡，可是他的MSDN就在那裡，也只有一份，比起來要跨平台Python方便多了，寫一次到處跑，還好這作業原本就是在unix-like下跑的，所以只差了errno的link問題，解決後就可以編譯了</p>
<p>首先，我們需要從<a href="http://www.webfaction.com/?affiliate=victorlin">WebFaction</a>的control panel中開一個custom app，也就是自訂的Server，因為port如果都任憑大家亂開，撞port號碼是個很大的問題，因此<a href="http://www.webfaction.com/?affiliate=victorlin">WebFaction</a>的port是交給他們統一分配的，如下圖所示，操作面版就可以建立一個custom app</p>
<p><a href="http://blog.ez2learn.com/wp-content/uploads/2009/07/custom_app.png"><img class="alignnone size-medium wp-image-698" title="custom_app" src="http://blog.ez2learn.com/wp-content/uploads/2009/07/custom_app-300x198.png" alt="custom_app" width="300" height="198" /></a></p>
<p>如你所見列表裡還有一堆其它程式可以透過這樣自動建成，不過我們今天重點不在那裡，而在自製的server，所以那些部份留在改天再介紹</p>
<p>這個步驟他會在你的home/webapps/下建立一個c_server_demo的資料夾，基本上，WebFaction的所有自動生程的程式都會在webapps下建立相對應的資料夾，而該安裝的檔案也會放在裡面，因為我們建立的是custom app，所以裡面是空的，不果我們還是照慣例，把我們的server程式碼丟進去，而在建立完c_server_demo之後，除了他會自動建立資料夾外，他還會分配一個port給你</p>
<p><a href="http://blog.ez2learn.com/wp-content/uploads/2009/07/custom_port.png"><img class="alignnone size-medium wp-image-699" title="custom_port" src="http://blog.ez2learn.com/wp-content/uploads/2009/07/custom_port-300x198.png" alt="custom_port" width="300" height="198" /></a></p>
<p>有了這個port，稍後我們的server就可以開在這個port上面，我們先進行編譯的工作，編譯完成後就可以執行我們的server，把它綁在我們想要的ip以及port上</p>
<p><a href="http://blog.ez2learn.com/wp-content/uploads/2009/07/compile_app.png"><img class="alignnone size-medium wp-image-700" title="compile_app" src="http://blog.ez2learn.com/wp-content/uploads/2009/07/compile_app-300x188.png" alt="compile_app" width="300" height="188" /></a></p>
<p>接著，server上線了，就可以來試著連線看看</p>
<p><a href="http://blog.ez2learn.com/wp-content/uploads/2009/07/connect.png"><img class="alignnone size-medium wp-image-701" title="connect" src="http://blog.ez2learn.com/wp-content/uploads/2009/07/connect-300x159.png" alt="connect" width="300" height="159" /></a></p>
<p><strong>成功了，就是這麼簡單</strong>，啥? 你說為什麼會有怪怪的數字? 因為server本來設計是接受binrary的整數，telnet丟字串給他當然會出問題，以上就是對於<a href="http://www.webfaction.com/?affiliate=victorlin">WebFaction</a>跑自行編譯Server的示範，因為有60天無條件退款，所以有興趣的朋友們別客氣申請來試試看，可以的話推薦人請填victorlin，或是從部落格的文章連結進入申請，<strong>還有其它問題的話可以問我，或是我幫各位問他們的客服</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ez2learn.com/2009/07/30/run-self-compiled-c-cpp-server-on-webfaction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

