才写完,我觉得很吊
主要逻辑,父类写的创建套接字和绑定,TCP子类把该有的函数写进去,UDP子类也同理
这样,可以通过不同的函数组合,来分别实现TCP服务器、TCP客户端,UDP服务器,UDP客户端,第一次感觉C++真牛逼
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstdio>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <cstring>
using namespace std;
class Sock
{
protected:
int sockfd;
struct sockaddr_in saddr;
int domain;
int type;
public:
Sock()
{
this->domain=AF_INET;
this->type=SOCK_STREAM;
sockfd = socket(domain, type, 0);
cout << "creat sock success" << endl;
//cout<<domain<<endl;
//cout<<AF_INET<<endl;
}
Sock(int domain,int type)
{
this->domain=domain;
this->type=type;
sockfd = socket(domain, type, 0);
cout << "creat sock success" << endl;
//cout<<domain<<endl;
//cout<<AF_INET<<endl;
}
~Sock()
{
close(sockfd);
}
#if 1
int init_sock(int domain, int type) // 初始化,创建套接字
{
this->domain=domain;
sockfd = socket(domain, type, 0);
cout << "creat sock success" << endl;
//cout<<domain<<endl;
//cout<<AF_INET<<endl;
return sockfd;
}
#endif
int bind_sock(const in_port_t sin_port, const char *s_addr) // 绑定地址和ip
{
//cout<<domain<<endl;
saddr.sin_family = domain;
//saddr.sin_family=AF_INET;
saddr.sin_port = htons(sin_port);
saddr.sin_addr.s_addr = inet_addr(s_addr);
int addrlen = sizeof(saddr);
int ret = bind(sockfd, (struct sockaddr *)&saddr, addrlen);
#if 0
cout<<saddr.sin_family<<endl;
cout<<domain<<endl;
cout<<sockfd<<endl;
cout << "ip---:" << inet_ntoa(saddr.sin_addr) << endl;
cout << "port---:" << ntohs(saddr.sin_port) << endl;
cout<<addrlen<<endl;
#endif
if (ret < 0)
{
close(sockfd);
}
cout << "bind success!!!" << endl;
return ret;
}
};
#if 1
class Tcp : public Sock // TCP
{
protected:
int connfd;
struct sockaddr_in caddr;
public:
int listen_tcp(int backlog) // 监听
{
int ret = listen(sockfd, backlog);
if (ret < 0)
{
close(sockfd);
}
return 0;
}
int accept_tcp() // 等待连接
{
socklen_t c_len = sizeof(caddr);
cout << "wait for connect..." << endl;
connfd = accept(sockfd, (struct sockaddr *)&caddr, &c_len);
if (connfd < 0)
{
cout<<"--------"<<endl;
close(sockfd);
}
cout << "connect successful!!!" << endl;
cout << "ip---:" << inet_ntoa(caddr.sin_addr) << endl;
cout << "port---:" << htons(caddr.sin_port) << endl;
return connfd;
}
int read_tcp()
{
char *buf=new char[64];
memset(buf,0,64);
read(connfd,buf,64);
cout<<buf;
if(strncasecmp(buf,"quit",4)==0)
{
close(connfd);
return -1;
}
return 0;
}
int connect_tcp(const in_port_t sin_port, const char *s_addr)
{
saddr.sin_family=AF_INET;
saddr.sin_port=htons(sin_port);
saddr.sin_addr.s_addr=inet_addr(s_addr);
int ret=connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
if(ret<0)
{
perror("connect");
exit(-1);
}
cout<<"connect successful!!!"<<endl;
return 0;
}
int write_tcp()
{
char *buf=new char[64];
memset(buf,0,64);
fgets(buf,64,stdin);
if(strncasecmp(buf,"quit",4)==0)
{
return -1;
}
write(sockfd,buf,64);
return 0;
}
};
#endif
#if 0
class Udp:public Sock
{
protected:
struct sockaddr_in caddr;
public:
Udp() //无参初始化写的是适用于TCP的
{
}
Udp(int domain,int type):Sock(domain,type) //给父类初始化
{
}
#if 1
size_t sendto_udp(const in_port_t sin_port, const char *s_addr)
{
char *buf=new char[64];
caddr.sin_family=domain;
caddr.sin_port=htons(sin_port);
caddr.sin_addr.s_addr=inet_addr(s_addr);
memset(buf,0,64);
fgets(buf,sizeof(buf),stdin);
if(strncasecmp(buf,"quit",4)==0)
{
return -1;
}
size_t len=sizeof(buf);
size_t c_len=sizeof(caddr);
size_t ret=sendto(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&caddr,c_len);
return ret;
}
#endif
#if 0
int recv_udp()
{
char *buf=new char[64];
memset(buf,0,64);
int len=sizeof(buf);
socklen_t c_len=sizeof(caddr);
int ret=recvfrom(sockfd,buf,len,0,(struct sockaddr*)&caddr,&c_len);
if(ret<0)
{
return -1;
}
if(strncasecmp(buf,"quit",4)==0)
{
return -1;
}
cout<<buf;
return ret;
}
#endif
};
#endif
int main()
{
//Tcp 服务器
#if 0
Tcp server;
server.init_sock(AF_INET, SOCK_STREAM); // 创建套接字
server.bind_sock(6666, "192.168.16.74"); // 绑定ip
server.listen_tcp(5); // 设置监听套接字
server.accept_tcp(); // 等待连接
while(1)
{
int ret=server.read_tcp();
if(ret<0)
break;
}
#endif
//Tcp 客户端
Tcp client;
client.connect_tcp(6666,"192.168.31.97");
while(1)
{
int ret=client.write_tcp();
if(ret<0)
{
return -1;
}
}
//Udp 服务器
#if 0
Udp server(AF_INET,SOCK_DGRAM); //通过给子类传参再给父类传参进行给父类初始化
//server.Sock(AF_INET, SOCK_DGRAM);
server.bind_sock(6666,"192.168.31.97");
while(1)
{
int ret=server.recv_udp();
if(ret<0)
break;
}
#endif
//Udp 客户端
#if 0
Udp client(AF_INET,SOCK_DGRAM);
while(1)
{
int ret=client.sendto_udp(6666,"192.168.31.97");
if(ret<0)
{
return -1;
}
}
#endif
return 0;
}
真小白,写错勿喷