生活随笔
收集整理的這篇文章主要介紹了
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 | ????bool Bind(const unsigned short port); |
| 22 | ????bool Listen() const; |
| 23 | ????bool Accept(Socket &new_sock) const; |
| 24 | ????bool Connect(std::string host, const unsigned short port);//client |
| 25 | ????bool Send(const std::string s) const; |
| 26 | ????int Recv(std::string &s) const; |
| 28 | ????bool IsValid() const; |
| 32 | ????sockaddr_in m_addr; |
| 35 | //ServerSocket, Use Socket as a service |
| 36 | class ServerSocket : private Socket |
| 39 | ??ServerSocket(){Create();}???? //for normal data-exchange socket |
| 40 | ??ServerSocket(unsigned short port);??? //for listen socket |
| 41 | ??virtual ~ServerSocket(); |
| 42 | ??void Accept(ServerSocket &new_sock) const; |
| 44 | ??const ServerSocket& operator << (const std::string&) const; |
| 45 | ??const ServerSocket& operator >> (std::string&) const; |
| 48 | //ClientSocket derived form Socket |
| 49 | class ClientSocket : private Socket |
| 52 | ????ClientSocket(){Create();} |
| 53 | ????ClientSocket(std::string host, const unsigned short port); |
| 54 | ????virtual ~ClientSocket(); |
| 56 | ????const ClientSocket& operator << (const std::string&) const; |
| 57 | ????const ClientSocket& operator >> (std::string&) const; |
| 64 | ????SocketException(std::string msg = std::string("Socket Exception")): m_msg(msg){} |
| 65 | ????void Print(){std::cout << m_msg << std::endl;} |
| 67 | ????const std::string m_msg; |
[代碼] MySocket.cpp
| 004 | //Implement of Socket class |
| 006 | static void Trace(const string& msg = "Socket Class Error") |
| 009 | ????cout << msg << endl; |
| 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); |
| 023 | ????if (m_sock != -1)?? //valid socket |
| 031 | ??m_sock = socket(AF_INET, SOCK_STREAM, 0); |
| 034 | ??????Trace("Create Socket Failure"); |
| 039 | ??????Trace("Create Socket Succeed"); |
| 044 | bool Socket::Bind(const unsigned 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 | ????int server_len = sizeof( m_addr ); |
| 051 | ????if ( bind(m_sock, (struct sockaddr *)& m_addr, server_len) == -1 ) |
| 053 | ????????Trace("Server Bind Failure"); |
| 062 | bool Socket::Listen() const |
| 064 | ????if ( listen(m_sock, 5) == -1 ) |
| 066 | ????????Trace("Server Listen Failure"); |
| 075 | bool Socket::Accept(Socket & new_socket) const//new_socket as a return |
| 077 | ????int len = sizeof( new_socket.m_addr ); |
| 078 | ????new_socket.m_sock = accept( m_sock, |
| 079 | ?????????????(struct sockaddr *)& new_socket.m_addr, (socklen_t*)&len ); |
| 080 | ????if ( new_socket.m_sock == -1 ) |
| 082 | ????????Trace("Server Accept Failure"); |
| 091 | bool Socket::Connect(const std::string host, const unsigned 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 | ????int len = sizeof(m_addr); |
| 098 | ????int res = connect( m_sock, (struct sockaddr*)& m_addr, len); |
| 101 | ????????Trace("Client Connect Failure"); |
| 107 | bool Socket::Send(const std::string send_str) const |
| 109 | ????//copy the send_str to a buffer so that we can send it. |
| 110 | ????size_t len = send_str.size() + 1; |
| 111 | ????char *send_buf = NULL; |
| 112 | ????send_buf = new char[len]; |
| 113 | ????if (NULL == send_buf) |
| 115 | ????????Trace("Socket: memory allocation failed in Send()"); |
| 118 | ????char t = send_str[0]; |
| 122 | ????????send_buf[i] = t; |
| 124 | ????????t = send_str[i]; |
| 127 | ????//end of copying string |
| 128 | ????assert(i == len-1); |
| 134 | ????????xs = write(m_sock, send_buf+xoff, len-xoff); |
| 137 | ????????????Trace("Socket: send data failed"); |
| 138 | ????????????delete [] send_buf; |
| 139 | ????????????return false; |
| 143 | ????????????xoff += xs; |
| 145 | ????} while(xoff < len); |
| 146 | ????Trace("Socket: send data succeed"); |
| 147 | ????delete [] send_buf; |
| 151 | int Socket::Recv( std::string & recv_str) const |
| 153 | ????cout << "enter recv" << endl; |
| 155 | ????recv_buf = new char[256]; |
| 156 | ????memset(recv_buf, 0 ,256); |
| 157 | ????int len = read(m_sock, recv_buf, 256); |
| 160 | ????????Trace("Socket: recv data failed"); |
| 161 | ????????delete [] recv_buf; |
| 166 | ????????recv_str = recv_buf; |
| 167 | ????????delete [] recv_buf; |
| 173 | bool Socket::IsValid() const |
| 175 | ??return ( m_sock != -1 ); |
| 182 | ????????close(m_sock); |
[代碼] MyClientSocket.cpp
| 04 | static void Trace(const string& msg = "ServerSocket Class Error") |
| 07 | ????cout << msg << endl; |
| 11 | ClientSocket::ClientSocket(std::string host, const unsigned short port) |
| 15 | ????????Trace("Client Socket Constructor Failed"); |
| 16 | ????????throw SocketException("Client Socket Constructor Failed"); |
| 18 | ????if ( !Connect(host, port) ) |
| 20 | ????????throw SocketException("Client Socket Constructor Failed"); |
| 24 | ClientSocket::~ClientSocket() |
| 29 | void ClientSocket::Close() |
| 34 | const ClientSocket& ClientSocket::operator << (const std::string &s) const |
| 38 | ????????Trace("Could not write to server socket"); |
| 39 | ????????throw SocketException("Could not write to client socket"); |
| 44 | const ClientSocket& ClientSocket::operator >> (std::string& s) const |
| 46 | ????if ( Recv(s) == -1 ) |
| 48 | ????????Trace("Could not read from socket"); |
| 49 | ????????throw SocketException("Could not read from client socket"); |
[代碼] MyServerSocket.cpp
| 05 | static void Trace(const string& msg = "ServerSocket Class Error") |
| 08 | ????cout << msg << endl; |
| 12 | ServerSocket::ServerSocket(unsigned short port) |
| 16 | ????????Trace("Could not create server socket!"); |
| 17 | ????????throw SocketException("Could not create : server socket!"); |
| 21 | ????????Trace("Could not bind to port!"); |
| 22 | ????????throw SocketException("Could not Bind : server socket!"); |
| 26 | ????????Trace("Could not listen to socket"); |
| 27 | ????????throw SocketException("Could not Litsen : server socket!"); |
| 31 | ServerSocket::~ServerSocket() |
| 37 | void ServerSocket::Accept(ServerSocket& new_socket) const |
| 39 | ????if ( !Socket::Accept(new_socket) ) |
| 41 | ????????throw SocketException("Server Accept Failed"); |
| 45 | void ServerSocket::Close() |
| 50 | const ServerSocket& ServerSocket::operator << (const std::string& s) const |
| 54 | ????????Trace("Could not write to server socket"); |
| 55 | ????????throw SocketException("Could not write to server socket"); |
| 60 | const ServerSocket& ServerSocket::operator >> (std::string& s) const |
| 65 | ????????Trace("Could not read from server socket"); |
| 66 | ????????throw SocketException("Could not read form server socket"); |
[代碼] ServerTest.cpp
| 01 | //Create a server for testing |
| 03 | #include "Calculator.h" |
| 10 | ????????ServerSocket server(30000); |
| 15 | ????????????ServerSocket new_sock; |
| 16 | ????????????server.Accept( new_sock ); |
| 17 | ????????????while(true) |
| 19 | ????????????????new_sock >> s; |
| 20 | ????????????????cout << "receive : \t" << s << endl; |
| 21 | ????????????????//Deal with client requests |
| 22 | ????????????????new_sock << s; |
| 23 | ????????????????cout << "send: \t " << s << endl; |
| 25 | ????????????new_sock.Close(); |
| 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); |
| 10 | ????????string s = "HELLO"; |
| 13 | ????????while(count <= 5) |
| 15 | ????????????cout << "input a string : " ; |
| 17 | ????????????cout << "You have input " << s << endl; |
| 18 | ????????????client << s; |
| 19 | ????????????cout<< "Send to Socket : "<<s<<'\t'<<count<<endl; |
| 20 | ????????????client >> s; |
| 21 | ????????????cout<< "Read from Socket : "<<s<<'\t'<<count<<endl; |
| 22 | ????????????//sleep(2); |
| 23 | ????????????s = "HELLO"; |
| 26 | ????????client.Close(); |
| 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 |
總結
以上是生活随笔為你收集整理的Linux Socket通信 C/S模型的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。