Linux Socket通信 C/S模型
代码片段(8)
 
 [代码] MySocket.h
    | 01 | #ifndef _MYSOCKET_0623_H | 
    | 02 | #define _MYSOCKET_0623_H | 
     | 04 | #include <sys/socket.h> | 
      | 07 | #include <netinet/in.h> | 
                 | 21 |     boolBind(const unsigned shortport); | 
     | 23 |     boolAccept(Socket &new_sock) const; | 
    | 24 |     boolConnect(std::string host, constunsigned short port); | 
    | 25 |     boolSend(const std::string s) const; | 
    | 26 |     intRecv(std::string &s) const; | 
             | 36 | classServerSocket : privateSocket | 
      | 39 |   ServerSocket(){Create();}      | 
    | 40 |   ServerSocket(unsigned shortport);     | 
    | 41 |   virtual~ServerSocket(); | 
    | 42 |   voidAccept(ServerSocket &new_sock) const; | 
     | 44 |   constServerSocket& operator << (conststd::string&) const; | 
    | 45 |   constServerSocket& operator >> (std::string&) const; | 
       | 49 | classClientSocket : privateSocket | 
      | 52 |     ClientSocket(){Create();} | 
    | 53 |     ClientSocket(std::string host, constunsigned shortport); | 
    | 54 |     virtual~ClientSocket(); | 
     | 56 |     constClientSocket& operator << (conststd::string&) const; | 
    | 57 |     constClientSocket& operator >> (std::string&) const; | 
          | 64 |     SocketException(std::string msg = std::string("Socket Exception")): m_msg(msg){} | 
    | 65 |     voidPrint(){std::cout << m_msg << std::endl;} | 
     | 67 |     conststd::string m_msg; | 
          
  
 [代码] MySocket.cpp
         | 006 | static voidTrace(conststring& msg = "Socket Class Error") | 
             | 016 |   m_addr.sin_family = AF_INET; | 
    | 017 |   m_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | 
    | 018 |   m_addr.sin_port = htons(8089); | 
                | 031 |   m_sock = socket(AF_INET, SOCK_STREAM, 0); | 
      | 034 |       Trace("Create Socket Failure"); | 
        | 039 |       Trace("Create Socket Succeed"); | 
        | 044 | boolSocket::Bind(constunsigned short port) | 
     | 046 |     assert(m_sock != -1); | 
    | 047 |     m_addr.sin_family = AF_INET; | 
    | 048 |     m_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | 
    | 049 |     m_addr.sin_port = htons(port); | 
    | 050 |     intserver_len = sizeof( m_addr ); | 
    | 051 |     if( bind(m_sock, (structsockaddr *)& m_addr, server_len) == -1 ) | 
     | 053 |         Trace("Server Bind Failure"); | 
            | 062 | boolSocket::Listen() const | 
     | 064 |     if( listen(m_sock, 5) == -1 ) | 
     | 066 |         Trace("Server Listen Failure"); | 
            | 075 | boolSocket::Accept(Socket & new_socket) const | 
     | 077 |     intlen = sizeof( new_socket.m_addr ); | 
    | 078 |     new_socket.m_sock = accept( m_sock,  | 
    | 079 |              (structsockaddr *)& new_socket.m_addr, (socklen_t*)&len ); | 
    | 080 |     if( new_socket.m_sock == -1 ) | 
     | 082 |         Trace("Server Accept Failure"); | 
            | 091 | boolSocket::Connect(conststd::string host, constunsigned short port) | 
     | 093 |     assert(m_sock != -1); | 
    | 094 |     m_addr.sin_family = AF_INET; | 
    | 095 |     m_addr.sin_addr.s_addr = inet_addr(host.c_str()); | 
    | 096 |     m_addr.sin_port = htons(port); | 
    | 097 |     intlen = sizeof(m_addr); | 
    | 098 |     intres = connect( m_sock, (structsockaddr*)& m_addr, len); | 
      | 101 |         Trace("Client Connect Failure"); | 
         | 107 | boolSocket::Send(conststd::string send_str) const | 
      | 110 |     size_tlen = send_str.size() + 1; | 
    | 111 |     char*send_buf = NULL; | 
    | 112 |     send_buf = newchar[len]; | 
    | 113 |     if(NULL == send_buf) | 
     | 115 |         Trace("Socket: memory allocation failed in Send()"); | 
      | 118 |     chart = send_str[0]; | 
                   | 134 |         xs = write(m_sock, send_buf+xoff, len-xoff); | 
      | 137 |             Trace("Socket: send data failed"); | 
            | 146 |     Trace("Socket: send data succeed"); | 
        | 151 | intSocket::Recv( std::string & recv_str) const | 
     | 153 |     cout << "enter recv"<< endl; | 
     | 155 |     recv_buf = newchar[256]; | 
    | 156 |     memset(recv_buf, 0 ,256); | 
    | 157 |     intlen = read(m_sock, recv_buf, 256); | 
      | 160 |         Trace("Socket: recv data failed"); | 
                | 173 | boolSocket::IsValid() const | 
     | 175 |   return( m_sock != -1 ); | 
                   
  
 [代码] MyClientSocket.cpp
       | 04 | static voidTrace(conststring& msg = "ServerSocket Class Error") | 
          | 11 | ClientSocket::ClientSocket(std::string host, constunsigned shortport) | 
       | 15 |         Trace("Client Socket Constructor Failed"); | 
    | 16 |         throwSocketException("Client Socket Constructor Failed"); | 
     | 18 |     if( !Connect(host, port) ) | 
     | 20 |         throwSocketException("Client Socket Constructor Failed"); | 
       | 24 | ClientSocket::~ClientSocket() | 
        | 29 | voidClientSocket::Close() | 
        | 34 | constClientSocket& ClientSocket::operator << (conststd::string &s) const | 
       | 38 |         Trace("Could not write to server socket"); | 
    | 39 |         throwSocketException("Could not write to client socket"); | 
        | 44 | constClientSocket& ClientSocket::operator >> (std::string& s) const | 
       | 48 |         Trace("Could not read from socket"); | 
    | 49 |         throwSocketException("Could not read from client socket"); | 
          
  
 [代码] MyServerSocket.cpp
        | 05 | static voidTrace(conststring& msg = "ServerSocket Class Error") | 
          | 12 | ServerSocket::ServerSocket(unsigned shortport) | 
       | 16 |         Trace("Could not create server socket!"); | 
    | 17 |         throwSocketException("Could not create : server socket!"); | 
       | 21 |         Trace("Could not bind to port!"); | 
    | 22 |         throwSocketException("Could not Bind : server socket!"); | 
       | 26 |         Trace("Could not listen to socket"); | 
    | 27 |         throwSocketException("Could not Litsen : server socket!"); | 
       | 31 | ServerSocket::~ServerSocket() | 
         | 37 | voidServerSocket::Accept(ServerSocket& new_socket) const | 
     | 39 |     if( !Socket::Accept(new_socket) ) | 
     | 41 |         throwSocketException("Server Accept Failed"); | 
       | 45 | voidServerSocket::Close() | 
        | 50 | constServerSocket& ServerSocket::operator << (conststd::string& s) const | 
       | 54 |         Trace("Could not write to server socket"); | 
    | 55 |         throwSocketException("Could not write to server socket"); | 
        | 60 | constServerSocket& ServerSocket::operator >> (std::string& s) const | 
        | 65 |         Trace("Could not read from server socket"); | 
    | 66 |         throwSocketException("Could not read form server socket"); | 
          
  
 [代码] ServerTest.cpp
      | 03 | #include "Calculator.h" | 
          | 10 |         ServerSocket server(30000); | 
        | 15 |             ServerSocket new_sock; | 
    | 16 |             server.Accept( new_sock ); | 
       | 20 |                 cout << "receive : \t"<< s << endl; | 
      | 23 |                 cout << "send: \t "<< s << endl;  | 
         | 29 |     catch(SocketException &e) | 
     | 31 |         cout << "SocketException:\t"; | 
        | 36 |         cout << "Common Exception"<< endl; | 
          
  
 [代码] ClientTest.cpp
     | 02 | #include "Calculator.h" | 
          | 09 |         ClientSocket client("127.0.0.1",30000); | 
         | 15 |             cout << "input a string : "; | 
     | 17 |             cout << "You have input "<< s << endl; | 
     | 19 |             cout<< "Send to Socket : "<<s<<'\t'<<count<<endl; | 
     | 21 |             cout<< "Read from Socket : "<<s<<'\t'<<count<<endl; | 
          | 28 |     catch(SocketException &e) | 
     | 30 |         cout << "SocketException:\t"; | 
             
  
 [代码] MakeClient
    | 01 | objects = ClientTest.o MyClientSocket.o MySocket.o Calculator.o mathop.o | 
    | 02 | ClientTest.out: $(objects) | 
    | 03 |     g++ $(objects) -o ClientTest.out | 
    | 04 | ClientTest.o: ClientTest.cpp MySocket.h  Calculator.h | 
    | 05 |     g++ -c -g -Wall -pedantic -ansi -DDEBUG -o ClientTest.o ClientTest.cpp  | 
    | 06 | MyClientSocket.o: MyClientSocket.cpp MySocket.h | 
    | 07 |     g++ -c -g -Wall -pedantic -ansi -DDEBUG -o MyClientSocket.o MyClientSocket.cpp  | 
    | 08 | MySocket.o: MySocket.cpp MySocket.h | 
    | 09 |     g++ -c -g -Wall -pedantic -ansi -DDEBUG MySocket.cpp -o MySocket.o | 
    | 10 | Calculator.o: Calculator.cpp Calculator.h | 
    | 11 |     g++ -g -c -Wall -pedantic -ansi -DDEBUG -o Calculator.o Calculator.cpp | 
    | 12 | mathop.o: mathop.cpp mathop.h | 
    | 13 |     g++ -g -c -Wall -pedantic -ansi -DDEBUG -o mathop.o mathop.cpp | 
      | 16 |     -rm $(objects) ClientTest.out  | 
        
  
[代码] MakeServer
 
 | 01 | objects = ServerTest.o MyServerSocket.o MySocket.o Calculator.o mathop.o | 
 
  
 | 02 | ServerTest.out: $(objects) | 
 
  
 | 03 |     g++ $(objects) -o ServerTest.out | 
 
  
 | 04 | ServerTest.o: ServerTest.cpp MySocket.h  Calculator.h | 
 
  
 | 05 |     g++ -c -g -Wall -pedantic -ansi -DDEBUG -o ServerTest.o ServerTest.cpp  | 
 
  
 | 06 | MyServerSocket.o: MyServerSocket.cpp MySocket.h | 
 
  
 | 07 |     g++ -c -g -Wall -pedantic -ansi -DDEBUG -o MyServerSocket.o MyServerSocket.cpp  | 
 
  
 | 08 | MySocket.o: MySocket.cpp MySocket.h | 
 
  
 | 09 |     g++ -c -g -Wall -pedantic -ansi -DDEBUG MySocket.cpp -o MySocket.o | 
 
  
 | 10 | Calculator.o: Calculator.cpp Calculator.h | 
 
  
 | 11 |     g++ -g -c -Wall -pedantic -ansi -DDEBUG -o Calculator.o Calculator.cpp | 
 
  
 | 12 | mathop.o: mathop.cpp mathop.h | 
 
  
 | 13 |     g++ -g -c -Wall -pedantic -ansi -DDEBUG -o mathop.o mathop.cpp | 
 
  
 
 
 | 16 |     -rm $(objects) ServerTest.out  | 
 
  
 
 
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/445183.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!