You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include<string>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<cstdint>
#include<sys/socket.h>
#include<netinet/tcp.h>
#include<unistd.h>
#include"InetAddr.h"
class Socket
{
private:
int fd_; //文件描述符
std::string ip_; //ip地址
uint16_t port_; //端口
public:
Socket(std::string _ip, uint16_t _port); //服务端socket构造函数ip和port
Socket(int _fd,std::string _ip,uint16_t _port);//客户端socket构造函数
~Socket(); //析构函数
void SetNonBlock(); //非阻塞
void SetReuseAddr(); //重用地址避免time_wait状态
void SetReusePort(); //重用端口避免惊群效应在reactor中意义不大
void SetTcpNoDelay(); //避免小数据包延迟
void SetKeepAlive(); //避免僵尸进程
void bind(InetAddr _addr); //绑定ip和端口
void listen(int len); //设置监听
int accept(InetAddr& clieAddr_); //受理连接
int fd(); //fd接口
std::string ip(); //ip接口
uint16_t port(); //port接口
};