波奇学Linux:网络接口

2024-03-25 20:12

本文主要是介绍波奇学Linux:网络接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

127.0.0.1本地回环ip,用于本地测试,不会进行网络通信

TCP是面向连接的,服务器比较被动

需要服套接字监听 listen状态

 正常通信默认会进行主机序列和网络序列的转换

TcpServer.cc

#pragma once#include<iostream>
#include<string>
#include<cstring>
#include<sys/types.h>
#include<sys/socket.h>
#include<cstdlib>
#include<wait.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>
#include<signal.h>const int defaultfd=-1;
const std::string defaultip="0.0.0.0";
const int backlog=5;
class TcpServer;
class ThreadData
{
public:ThreadData(int fd,const std::string &ip,const uint16_t &p,TcpServer* tsvr):sockfd(fd),clientip(ip),clientport(p),tsvr_(tsvr){}
public:int sockfd;std::string clientip;uint16_t clientport;TcpServer* tsvr_;
};
class TcpServer
{
public:TcpServer(const uint16_t &port,const std::string &ip=defaultip):listensockfd_(defaultfd),port_(port),ip_(ip){}void InitServer(){listensockfd_=socket(AF_INET,SOCK_STREAM,0);if(listensockfd_<0){std::cout<<"create socket error"<<std::endl;}std::cout<<"create socket success"<<std::endl;struct sockaddr_in local;memset(&local,0,sizeof(local));local.sin_family=AF_INET;local.sin_port=htons(port_);inet_aton(ip_.c_str(),&(local.sin_addr));if(bind(listensockfd_,(struct sockaddr*)&local,sizeof(local))<0){std::cout<<"bind error"<<std::endl;}// TCP是面向连接的,服务器比较被动if(listen(listensockfd_,backlog)<0){std::cout<<"listen error"<<std::endl;}std::cout<<"listen success"<<std::endl;}static void* Routine(void* args){pthread_detach(pthread_self());ThreadData* td=static_cast<ThreadData*>(args);td->tsvr_->Service(td->sockfd,td->clientip,td->clientport);delete td;return nullptr;}void Start(){for(;;){//1.获取连接struct sockaddr_in client;socklen_t len=sizeof(client);// sock返的fd负责listen状态//accept返回的sockfd负责通信int sockfd=accept(listensockfd_,(struct sockaddr* )&client,&len);if(sockfd<0){std::cout<<"accept error"<<std::endl;}uint16_t clientport=ntohs(client.sin_port);char clientip[32];inet_ntop(AF_INET,&(client.sin_addr),clientip,sizeof(clientip));std::cout<<"get a net link... sockfd : "<< sockfd<<std::endl;sleep(1);// 根据新连接来通信// Service(sockfd, clientip,clientport);// close(sockfd);// 多进程版// pid_t id=fork();// if(id==0)// {//     close(listensockfd_);//     if(fork()>0) exit(0);//     Service(sockfd, clientip,clientport); //孙子进程,system 领养//     close(sockfd);//     exit(0);// }// //father// close(sockfd);// // 关掉子进程  // pid_t rid=waitpid(id,nullptr,0);// (void)rid;// 多线程版本ThreadData*td=new ThreadData(sockfd,clientip,clientport,this);pthread_t tid;pthread_create(&tid,nullptr,Routine,nullptr);}}void Service(int sockfd,const std::string & clientip,const uint16_t &clientport){char buffer[4096];while(true){ssize_t n= read(sockfd,buffer,sizeof(buffer));if(n>0){buffer[n]=0;std::cout<<"client say# "<<buffer<<std::endl;std::string echo_string="tcpserver echo#";echo_string+=buffer;write(sockfd,echo_string.c_str(),echo_string.size());}else if(n==0){break;}else{std::cout<< "read error"<<std::endl;}}}~TcpServer(){}
private:int listensockfd_;uint16_t port_;std::string ip_;
};

TcpClient

#include<iostream>
#include<cstring>
#include<sys/types.h>
#include<sys/socket.h>
#include<cstdlib>
#include<arpa/inet.h>
#include<netinet/in.h>
#include <unistd.h>
void Usage(const std::string& proc)
{std::cout<<"\n\rUsage "<<proc<<" serveip serverport\n"<<std::endl;
}
int main(int argc,char*argv[])
{if(argc!=3){Usage(argv[0]);exit(1);}int sockfd=socket(AF_INET,SOCK_STREAM,0);std::string serverip=argv[1];uint16_t serverport=std::stoi(argv[2]);if(sockfd<0){std::cerr<<"sock error"<<std::endl;}struct sockaddr_in server;memset(&server,0,sizeof(server));server.sin_family=AF_INET;server.sin_port=htons(serverport);// tcp客户端要bind,// 客户端发起connect的时候,进行随机bindinet_pton(AF_INET,serverip.c_str(),&(server.sin_addr));int n=connect(sockfd,(struct sockaddr*)&server,sizeof(server));if(n<0){std::cerr<<"connect error..."<<std::endl;}std::string message;while(true){std::cout<<"Please Enter# ";std::getline(std::cin,message);write(sockfd,message.c_str(),message.size());char inbuffer[4096];int n=read(sockfd,inbuffer,sizeof(inbuffer));if(n>0){inbuffer[n]=0;std::cout<<inbuffer<<std::endl;}}close(sockfd);return 0;
}

这篇关于波奇学Linux:网络接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/846130

相关文章

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

Linux之platform平台设备驱动详解

《Linux之platform平台设备驱动详解》Linux设备驱动模型中,Platform总线作为虚拟总线统一管理无物理总线依赖的嵌入式设备,通过platform_driver和platform_de... 目录platform驱动注册platform设备注册设备树Platform驱动和设备的关系总结在 l

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并