Linux线程版服务器


目录

  • server.cpp
  • client.cpp
  • 总结
    • 要用detach()把子线程和主线程分离
    • 编译指令 : g++ -std=c++11 -pthread thread1.cpp -o a
  • 效果图


server.cpp

#include 
using namespace std;
#include 
#include 
#include 
#include 
#include 

//利用CPP新线程库写
class info{
	public:
		info(sockaddr_in cli, int clifd){
		this->cli = cli;
		this->clifd = clifd;
		}
		void delet(){
			cout<<"调用了析构函数"<<endl;
			flag=1;
		}
		sockaddr_in cli;
		int clifd;
		bool flag=0;
};
void readWrite(info a){
while(1){
	char buf[1024];
memset(buf,0,sizeof(buf));
//cin.getline(buf,sizeof(buf));
size_t size;
size =  read(a.clifd,buf,sizeof(buf));
if(size <0){cout<<"read error"<<endl;break;}
else if(size == 0 ){cout<<ntohs(a.cli.sin_port)<<"client disconnect"<<endl;break;}
else{
cout<<"客户端"<<ntohs(a.cli.sin_port)<<"说:"<<buf<<endl;
memset(buf,0,sizeof(buf));
strcpy(buf,"2023-03-02");
write(a.clifd,buf,sizeof(buf));
}

}

close(a.clifd);
a.delet();

}

int main(){
int sockfd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
ser.sin_port = htons(10000);
bind(sockfd , (sockaddr*)&ser , sizeof(ser));
listen(sockfd,128);
struct sockaddr_in cli;
memset((sockaddr*)&cli,0,sizeof(cli));
socklen_t len = sizeof(cli);
fir:
while(1){
	int clifd = accept(sockfd,(sockaddr*)&cli,&len);
	if(clifd < 0 )goto fir;
	cout<<"new client"<<endl;
	cout<<"ip:"<<inet_ntoa(cli.sin_addr)<<endl;
	cout<<"port:"<<ntohs(cli.sin_port)<<endl;
	info *a = new info(cli,clifd);
	thread *t1;
	t1 = new thread(readWrite,*a);
	//这里要用detach不能用join会被阻塞
	t1->detach();
	if(a->flag){
		delete a;
		goto fir;
	}
}
}

client.cpp

#include 
using namespace std;
#include 
#include 
#include 
#include 
int main(){

        int sockfd = socket(AF_INET,SOCK_STREAM,0);

        struct sockaddr_in cli;//创建结构体
        bzero(&cli,sizeof(cli));
        cli.sin_family = AF_INET;
        cli.sin_addr.s_addr = inet_addr("127.0.0.1");
        cli.sin_port = htons(10000);

        connect(sockfd,(sockaddr*)&cli,sizeof(cli));

        while(1){
                //string str="";
                char buf[1024];
                bzero(&buf,sizeof(buf));
                cin>>buf;
                ssize_t size = write(sockfd,buf,sizeof(buf));
                //cout<<"客户机说:"<
                if(size == -1 ){
                cout<<"写程序出错"<<endl;
                }
                bzero(&buf,sizeof(buf));

                ssize_t read_size = read(sockfd,buf,sizeof(buf));

                if(size > 0 ){
                cout<<"服务器说:"<<buf<<endl;
                }
                else if(size == 0){
                cout<<"服务器没有连接"<<endl;
                break;
                }
                else if(size == -1){
                cout<<"socket出错"<<endl;
                close(sockfd);
                }
        }
        close(sockfd);
        return 0;
}


总结


要用detach()把子线程和主线程分离


编译指令 : g++ -std=c++11 -pthread thread1.cpp -o a

效果图

Linux线程版服务器_第1张图片

你可能感兴趣的:(C++网络编程,linux,服务器,运维)