基于linux c++的使用sock实现客户端和服务的消息回复和发送的通信demo
程序员文章站
2022-05-28 14:33:22
...
本例程进行了sock通信C++的类的封装,简化了sock通信主函数的代码量,其中类的封装亦可用于其他项目的移植.
0功能演示
1所需要的头文件(不是所有都需要,此头文件是我经常用到的头文件)
/*
* author:jill
* name:_myhead.h
* date:2019-10-02
* function:all the head file you can use
*/
#ifndef _HEAD_H
#define _HEAD_H 1
#include <stdio.h>
#include<unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<time.h>
#include <stdarg.h>
#endif
2服务端程序
/*
*author:jill
*nameinitsever_for_class.cpp
*date:2019-10-02
*function:服务端socket的class封装.port端口号
*
*
*/
#include"/home/jill/mylib/_myhead.h"
class CServer
{
private:
int m_listenfd;
int m_clientfd;
public:
CServer();
~CServer();
bool initserver(const int port);
bool Accept();
ssize_t Recv(void *buf,ssize_t len);
ssize_t Send(const void *buf,ssize_t len);
};
CServer::CServer()
{
m_listenfd=0;
m_clientfd=0;
}
CServer::~CServer()
{
if(m_listenfd>0) close(m_listenfd);
if(m_clientfd>0) close(m_clientfd);
}
bool CServer::initserver(const int port)
{
m_listenfd=socket(AF_INET,SOCK_STREAM,0);//监听
struct sockaddr_in serveaddr;
memset(&serveaddr,0,sizeof(serveaddr));
serveaddr.sin_family=AF_INET; //ipv4协议族
serveaddr.sin_addr.s_addr=htonl(INADDR_ANY);
serveaddr.sin_port=htons(port);
if(bind (m_listenfd,(const struct sockaddr*)&serveaddr,sizeof(serveaddr))!=0){
perror("bind");close(m_listenfd);return false;}
if(listen(m_listenfd,5)!=0){
perror("listen");close(m_listenfd);return false;}
return true;
}
bool CServer::Accept()
{
int socklen;
struct sockaddr_in clientaddr; //客户端的地址信息
m_clientfd=accept(m_listenfd,(struct sockaddr *)&clientaddr,
(socklen_t*)&socklen);
if(m_clientfd<0) return false;
return true;
}
ssize_t CServer::Recv(void *buf,ssize_t len)
{
memset(buf,0,len);
return recv(m_clientfd,buf,len,0);
}
ssize_t CServer::Send(const void *buf,ssize_t len)
{
return send(m_clientfd,buf,len,0);
}
//-------------------------------------
int main()
{
CServer server;
if(server.initserver(5000)==false) return -1;
if(server.Accept()==false) return -1;
char buffer[1024];
while(true)
{
CServer server;
if(server.initserver(5000)==false) return -1;
if(server.Accept()==false) return -1;
char buffer[1024];
while(true)
{
if(server.Recv(buffer,sizeof(buffer))<=0) break;
printf("收到来信:%s\n",buffer);
if(strcmp(buffer,"bye")==0) break;
memset(buffer,0,sizeof(buffer));
printf("请键入消息并回车键发送:");
scanf("%s",buffer);
if(server.Send(buffer,strlen(buffer))<=0) break;
}
return 0;
}
3客户端程序
/*
*author:jill
*name:connecttosever_for_class.cpp
*date:2019-10-02
*function:客户段socket的class封装,serveip:客服端的ip地址,port端口号
*/
#include"/home/jill/mylib/_myhead.h"
class CClient
{
private:
int m_clientfd;
public:
CClient();
~CClient();
bool connect_to_server(const char *serveip,const int port);
ssize_t Recv(void *buf, size_t len);
ssize_t Send(const void *buf, size_t len);
};
CClient::CClient()
{
m_clientfd=0;
}
CClient::~CClient()
{
if(m_clientfd>0) close(m_clientfd);
}
bool CClient::connect_to_server(const char *serveip,const int port)
{
m_clientfd=socket(AF_INET,SOCK_STREAM,0);
struct hostent* h;
memset(h,0,sizeof(h));
h=gethostbyname(serveip);
if(h==0)
{
close(m_clientfd);
m_clientfd=0;
return false;
}
struct sockaddr_in serveaddr;
memset(&serveaddr,0,sizeof(serveaddr));
serveaddr.sin_family=AF_INET; //ipv4协议族
serveaddr.sin_port=htons(port);
memcpy(&serveaddr.sin_addr,h->h_addr,h->h_length);
if(connect(m_clientfd,(struct sockaddr *)&serveaddr,sizeof(s
erveaddr))!=0)
{
perror("connect");
close(m_clientfd);
return false;
}
return true;
}
ssize_t CClient::Recv(void *buf,size_t len)
{
memset(buf,0,len);
return recv(m_clientfd,buf,len,0);
}
ssize_t CClient::Send(const void *buf,size_t len)
{
return send(m_clientfd,buf,len,0);
}
//------------------------------------------------------
int main( )
{
CClient client;
if(client.connect_to_server("192.168.226.1",5000)==false) return -1;
char buffer[1024];
while(true)
{
printf("请键入消息回车键发送:");
scanf("%s",buffer);
if(client.Send(buffer,strlen(buffer))<=0) break;
memset(buffer,0,sizeof(buffer));
if(client.Recv(buffer,sizeof(buffer))<=0) break;
printf("收到来信:%s\n",buffer);
}
return 0;
}
上一篇: QT自绘滑动开关按钮
下一篇: Java | 深拷贝与浅拷贝