波奇学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

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

linux hostname设置全过程

《linuxhostname设置全过程》:本文主要介绍linuxhostname设置全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录查询hostname设置步骤其它相关点hostid/etc/hostsEDChina编程A工具license破解注意事项总结以RHE

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.