2014-12-19 54 views
-1

我正在使用boost :: asio庫製作TCP服務器。C2228:TCP asio服務器出錯

在這一刻,我有這樣的代碼: 注:這是一個非營利性的測試服務器。

int main(){ 
     const int SERVER_PORT = 60000; 

     try{ 
      ... 
      //declare the io_service, endpoint, acceptor and socket 
      ... 
      { 
       acceptor.accept(socket); 
       boost::asio::write(socket, boost::asio::buffer(message)); 
       boost::asio::streambuf received; 
       boost::asio::read_until(socket, received, "\r\n"); 
       ... 
       //all server operations 
       ... 
       } 
      } 
     } 
     catch (std::exception& ex) 
     { 
      std::cerr << "Exception " << ex.what() << std::endl; 
     } 

     return 0; 
    } 

現在,我wan't提高代碼,我開始由職能組織我的代碼是這樣的:

void session(){ 
     boost::asio::write(socket, boost::asio::buffer(message)); 
     boost::asio::streambuf received; 
     boost::asio::read_until(socket, received, "\r\n"); 
     ... 
     //All server operations 
     ... 
    } 

    void server(boost::asio::io_service& io_service, const int SERVER_PORT){ 
     ... 
     //declare the io_service, endpoint, acceptor and socket 
     ... 
     session();//Call Session 
    } 


    int main(){ 
     const int SERVER_PORT = 60000; 
     boost::asio::io_service io_service; 
     server(io_service, SERVER_PORT); 

     return 0; 
    } 

我的問題是,隨着功能的,它不編譯。 我有Visual Studio的編譯錯誤:C2228

error C2228:The operand to the left of the period .read_some is not a class, structure, or union

error C2228:The operand to the left of the period .write_some is not a class, structure, or union

我不找到如何解決這個問題。

非常感謝!

編輯:根據答案

除非插座解決方案是一個全球性的vairable,無效的會話()是無效的。 好一個是:

void session(boost::asio::ip::tcp::socket& socket) 

非常感謝Orrkid

+3

您發佈的代碼中沒有任何內容試圖調用read_some或write_some。除非您在發生錯誤的地方發佈代碼,否則沒有人能夠爲您提供幫助。 – Wintermute

+0

嗨,'read_some'和'write_some'在庫Boost :: Asio裏面。 例如,'read_some'在'read_until.hpp'裏面,不是我的代碼的一部分。 – SpykeRel04D

+0

你沒有調用你發佈的示例代碼中的任何一個,問題在於你如何調用這些代碼而不是如何實現這些代碼。在展示調用這些代碼的代碼之前,沒有人能夠提供幫助。 – drescherjm

回答

1

凡定義插座?聽起來像沒有正確定義,導致write和read_until函數拋出錯誤。

read_some和write_some被稱爲寫內部和read_until

(編輯),看你的代碼,除非插座是一個全球性的vairable,它是如何獲得通過到您的會話功能,我看到你的評論,你將在你的服務器函數中聲明它們,但是除非你從示例中省略了更多的代碼,否則看起來像void session()是無效的。

+0

謝謝非常多的人!這是我的錯誤! – SpykeRel04D