Linux socket编程(二) 服务器与客户端的通信
2015-06-23 23:32
429 查看
http://www.cnblogs.com/-Lei/archive/2012/09/04/2670964.html
上一篇写了对套接字操作的封装,这一节使用已封装好的Socket类实现服务器与客户端的通信(Socket的定义见上篇Socket.h)
服务器端:ServerSocket.h
#ifndef SERVERSOCKET_H #define SERVERSOCKET_H #include "Socket.h" class ServerSocket:public Socket { public: ServerSocket(const int port); ServerSocket(); virtual ~ServerSocket(); void Accept(Socket& socket); }; #endif
ServerSocket.cpp
#include "ServerSocket.h" #include "SocketException.h" ServerSocket::ServerSocket(const int port) { if ( ! Socket::Create() ) { throw SocketException ( "Could not create server socket." ); } if ( ! Socket::Bind ( port ) ) { throw SocketException ( "Could not bind to port." ); } if ( ! Socket::Listen() ) { throw SocketException ( "Could not listen to socket." ); } } ServerSocket::~ServerSocket() { } void ServerSocket::Accept(Socket& socket) { if ( ! Socket::Accept ( socket ) ) { throw SocketException ( "Could not accept socket." ); } }
//============================================================================ // Name : ChatServer.cpp // Author : Lei // Version : // Copyright : // Description : ChatServer in C++, Ansi-style //============================================================================ #include <iostream> #include <string> #include "ServerSocket.h" #include "SocketException.h" using namespace std; int main() { cout<<"Running server..."<<endl; try { ServerSocket server(8080); while(true) { Socket newSocket; server.Accept(newSocket); try { string message; server.Receive(newSocket,message); cout<<"Receive message: "<<message<<endl; message="Here is server"; server.Send(newSocket,message); } catch(SocketException&){} } } catch(SocketException& ex) { cout << "Exception was caught:" << ex.Description() << "\nExiting.\n"; } return 0; }
接下来是客户端:
ClientSocket.h
#ifndef CLIENTSOCKET_H #define CLIENTSOCKET_H #include "Socket.h" #include <string> class ClientSocket:public Socket { public: ClientSocket (const std::string& host,const int port ); virtual ~ClientSocket(); bool Send(const std::string& message) ; int Receive(std::string& message) ; }; #endif
ClientSocket.cpp
#include "ClientSocket.h" #include "SocketException.h" ClientSocket::ClientSocket(const std::string& host,const int port) { if(!Socket::Create()) throw SocketException("Could not create client socket."); if(!Socket::Connect(host,port)) throw SocketException( "Could not connect to port." ); } ClientSocket::~ClientSocket() {} bool ClientSocket::Send(const std::string& message) { return Socket::Send(static_cast<Socket&>(*this),message); } int ClientSocket::Receive(std::string& message) { return Socket::Receive(static_cast<Socket&>(*this),message); }
这里使用了 dynamic_cast来将this指针向下转型,转成指向基类Socket的指针
//============================================================================ // Name : ChatClient.cpp // Author : Lei // Version : // Copyright : // Description : ChatClient in C++, Ansi-style //============================================================================ #include <iostream> #include <string> #include "ClientSocket.h" #include "SocketException.h" using namespace std; int main() { cout<<"Running client...."<<endl; try { ClientSocket clientSocket("127.0.0.1",8080); clientSocket.Send("Hello,here is client"); string message; clientSocket.Receive(message); cout<<"Response from server: "<<message<<endl; } catch(SocketException& ex) { cout << "Exception was caught:" << ex.Description() << "\n"; } return 0; }
结果:
服务器端
客户端
相关文章推荐
- Linux socket编程(三) 简单的多线程聊天室
- centos安装lnmp环境
- c++ socket 服务器 线程池 海量 并发 windoes linux 源代码 SPServer
- 速记的LINUX高编笔记1
- Linux下C++ Socket编程
- Linux-命令-netstat
- Linux TC 带宽管理队列规则
- Linux设置环境变量命令
- Linux TC(Traffic Control) 简介(一)
- Linux常用命令
- Linux系统下挂载光盘
- linux程序调试命令addr2line之入门简介(本文先不聊gdb调试)
- linux 常用命令及技巧
- Linux-命令-awk
- Linux_4.7_信号量同步编程
- Linux下防火墙iptables设置
- linux 下搭建zabbix邮件报警 推荐
- SELinux原理和使用
- linux基础3——与XP共享文件夹的设置
- linux 下zabbix 监控简单配置