日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux socket编程(二) 服务器与客户端的通信

發布時間:2023/11/30 linux 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux socket编程(二) 服务器与客户端的通信 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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编程(二) 服务器与客户端的通信的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。