网络复习(1)

1、socket函数

函数原型:

复制代码
#include <sys/socket.h>
int socket(int domain, int type, int protocol);

参数解析:

作用:

socket函数的作用是创建一个套接字描述符(文件描述符),这个描述符是后续进行网络通信(绑定、监听、连接、收发数据)的唯一标识。

一、写一个回显程序:客户端发什么,服务器端就回什么

(1)、创建一个udp socket套接字:

cpp 复制代码
#pragma once

#include<iostream>
using namespace std;
#include<sys/types.h>
#include<sys/socket.h>
#include<string>
#include<cstdlib>
class UdpServer
{
    public:
    UdpServer(){}
    //初始化一个socket套接字
    void InitServer()
    {
        int sockfd = socket(AF_INET,SOCK_DGRAM,0);
        if(sockfd < 0)
        {
            cout<<"创建失败"<<endl;
            exit(1);
        }
    }
    void start()
    {

    }
    ~UdpServer()
    {

    }
    private:

};

2、bind函数

作用:

在网络编程(C/C++ 中调用系统 Socket API)里,bind函数是将套接字与特定的 IP 地址和端口号绑定的核心函数,只有绑定后,套接字才能监听(服务端)或发起连接(在客户端中,部分场景可选)

函数原型:

cpp 复制代码
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

参数解析:

注意:

通常我们要自己填充sockaddr结构。

(2)填充sockaddr结构,且优化结构。

cpp 复制代码
#pragma once

#include<iostream>
using namespace std;
#include<sys/types.h>
#include<sys/socket.h>
#include<strings.h>
#include<cstdlib>
#include <netinet/in.h>
#include <arpa/inet.h>    // 提供 IP 地址转换函数(如 inet_pton()、inet_ntop()、inet_addr())
const static int defaultfd = -1;//套接字默认值,-1代表创建失败
class UdpServer
{
    public:
    //需要外部传入port,ip
    UdpServer(uint16_t port,const string& ip):_sockfd(defaultfd),_port(port),_ip(ip)
    {}
    //初始化一个socket套接字
    void InitServer()
    {
        _sockfd = socket(AF_INET,SOCK_DGRAM,0);
        if(_sockfd < 0)
        {
            cout<<"创建失败"<<endl;
            exit(1);
        }
        //创建成功后,填充sockaddr结构
        struct sockaddr_in local;//struct sockaddr_in 是系统提供的数据类型,local是变量,属于用户在栈上开辟的空间(类似于int a = 100;)
        bzero(&local,sizeof(local));//对一份空间清零
        local.sin_family = AF_INET;
        local.sin_port = htonl(_port);//port要经过网络传输给对面,必须先到网络,而网络是大端序
        //对于ip,我们需要注意两点:
        //a:要把字符串风格的点分十进制的IP地址转换成4字节IP地址
        //b:要将主机序列转换成网络序列
        //上述两点只需要一个函数可以同时进行:inet_addr
        local.sin_addr.s_addr = inet_addr(_ip.c_str());
    }
    void start()
    {
        
    }
    ~UdpServer()
    {

    }
    private:
    int _sockfd;
    uint16_t _port;
    string _ip;//暂时先这样写(不是必须的)
};

(3)开始绑定

cpp 复制代码
#pragma once

#include<iostream>
using namespace std;
#include<sys/types.h>
#include<sys/socket.h>
#include<strings.h>
#include<cstdlib>
#include <netinet/in.h>
#include <arpa/inet.h>    // 提供 IP 地址转换函数(如 inet_pton()、inet_ntop()、inet_addr())
const static int defaultfd = -1;//套接字默认值,-1代表创建失败
class UdpServer
{
    public:
    //需要外部传入port,ip
    UdpServer(uint16_t port,const string& ip):_sockfd(defaultfd),_port(port),_ip(ip)
    {}
    //初始化一个socket套接字
    void InitServer()
    {
        _sockfd = socket(AF_INET,SOCK_DGRAM,0);
        if(_sockfd < 0)
        {
            cout<<"创建失败"<<endl;
            exit(1);
        }
        //创建成功后,填充sockaddr结构
        struct sockaddr_in local;//struct sockaddr_in 是系统提供的数据类型,local是变量,属于用户在栈上开辟的空间(类似于int a = 100;)
        bzero(&local,sizeof(local));//对一份空间清零
        local.sin_family = AF_INET;
        local.sin_port = htonl(_port);//port要经过网络传输给对面,必须先到网络,而网络是大端序
        //对于ip,我们需要注意两点:
        //a:要把字符串风格的点分十进制的IP地址转换成4字节IP地址
        //b:要将主机序列转换成网络序列
        //上述两点只需要一个函数可以同时进行:inet_addr
        local.sin_addr.s_addr = inet_addr(_ip.c_str());

        //开始bind  绑定sockfd和网络信息(IP+Port)
        int n = bind(_sockfd,(struct sockaddr*)&local,sizeof(local));
        if(n < 0)
        {
            cout<<"绑定失败"<<endl;
            exit(1);
        }
        cout<<"绑定成功"<<endl;
    }
    void start()
    {
        
    }
    ~UdpServer()
    {

    }
    private:
    int _sockfd;
    uint16_t _port;
    string _ip;//暂时先这样写(不是必须的)
};

(4)增加服务器是否启动的属性,默认是不启动,并且规定用户启动程序的命令行输入格式

Main.cc

cpp 复制代码
#include "UdpServer.hpp"
#include <memory>
//提醒用户使用方式错误
void Usage(string proc)
{
    cout<<"Usage:\n\t"<<proc<<"local_ip local_port\n"<<endl;
}

int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        exit(1);
    }
    //EnableScreen();
    string ip = argv[1];
    uint16_t port = stoi(argv[2]);
    unique_ptr<UdpServer> usvr = make_unique<UdpServer>(ip,port);
    usvr->InitServer();
    usvr->start();
    return 0;
}

UdpServer.hpp

cpp 复制代码
 void start()
    {
        //服务器都是死循环,所以我们要一直运行就循环
        _isrunning = true;
        
    }

3、



相关推荐
为思念酝酿的痛7 小时前
POSIX信号量
linux·运维·服务器·后端
ylscode8 小时前
PureLogs 信息窃取恶意软件惊现高危变种:借道 MsBuild.exe 进程空心化实施无痕攻击
网络·安全·安全威胁分析
IPHWT 零软网络8 小时前
MX60E-A信创级智能语音网关技术实现与架构分析
网络·网络安全·国产自研·技术实现·智能语音网关·政企通信·信创技术
隔窗听雨眠8 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
IT大白鼠8 小时前
RSTP协议原理与配置详解:快速生成树技术的深度解析
网络·网络协议
人还是要有梦想的9 小时前
linux下用搜狗输入法,中英文切换
linux·运维·服务器
9分钟带帽9 小时前
linux_通过NFS挂载远程服务器的硬盘
linux·服务器
Ether IC Verifier9 小时前
SystemVerilog 数据类型详解
php·systemverilog·uvm·ic验证
弥树子9 小时前
踩坑记录:服务器内网调用接口,真实请求URL与官方公开URL不一致问题排查
开发语言·php
C+++Python9 小时前
BIO、NIO、AIO 区别
网络·nio