C++ 网络编程 建立简单的TCP通信

  • Post author:
  • Post category:其他






参考资料


socket必备知识



github-30dayMakeCppServer



TCP 服务端流程

linux一切操作都是操作文件描述符



1:创建socket,网络通信接口

头文件---><sys/socket.h>
int sockfd = socket(AF_INET,SOCK_STREAM,0);
第一个参数:ip类型(AF_INET-->ipv4,AF_INET6--->ipv6)
第二个参数:TCP--->SOCK_STREAM
		  UDP--->SOCK_DGRAM
第三个参数:自动推导第二个参数类型



2:将文件描述符和ip与port绑定(fd和struct绑定)

利用结构体sockaddr_in对ip,port,和family进行封装

头文件---<arpa/inet.h>
struct sock_addr_in ser;
bzero(&ser,sizeof(ser));//填充为0---头文件---<cstring>
ser.sin_family = AF_INET;
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
ser.sin_port = htons(10000);//host to newwork short(port为短整型int)
bind( sockfd, (sockaddr*)&ser, sizeof(ser));//绑定



3.监听listen(服务器堵塞直到accept触发)

listen(sockfd,maxnum);
maxnum ---> 服务器缓冲队列最大客户端数量



4.接受accept

struct sockaddr_in cli;
socklen_t len = sizeof(cli);
bzero (&cli,sizeof(cli));//置空
int clifd = accept(sockfd,(sockaddr*)&cli,len);//客户端文件描述符



5.read | write | close

读写是针对 accept客户机文件描述符 的而不是 网络文件描述符

               char buf[1024];
                bzero(&buf,sizeof(buf));
                ssize_t size = read(clifd,buf,sizeof(buf));//成功返回字节数,失败为-1
                if(size > 0 ){
                cout<<"客户机"<<clifd<<"号说话了:"<<buf<<endl;
                        bzero(&buf,sizeof(buf));
                        cin>>buf;
                        write(clifd,buf,sizeof(buf));//服务器回传数据
                }
                else if(size == -1 ){
                cout<<"socket出错"<<endl;
                close(clifd);//关闭client文件描述符
                }
//如果要关闭通信socket
//close那个文件描述符就是关闭该功能
close(sockfd);



TCP 客户端流程



1:创建socket,网络通信接口

头文件---><sys/socket.h>
int sockfd = socket(AF_INET,SOCK_STREAM,0);



2:将文件描述符和ip与port绑定(fd和struct绑定)

利用结构体sockaddr_in对ip,port,和family进行封装

头文件---<arpa/inet.h>
struct sock_addr_in cli;
bzero(&cli,sizeof(cli));//填充为0---头文件---<cstring>
cli.sin_family = AF_INET;
cli.sin_addr.s_addr = inet_addr("127.0.0.1");
cli.sin_port = htons(10000);//host to newwork short(port为短整型int)



3.connect

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



4.write | read | close

                char buf[1024];
                bzero(&buf,sizeof(buf));
                cin>>buf;
                ssize_t size = write(sockfd,buf,sizeof(buf));
                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 == -1){
                cout<<"socket出错"<<endl;
                close(sockfd);
                }



demo



demo_sereve

#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
int main(){
//创建socket,网络文件描述符
        int sockfd = socket(AF_INET,SOCK_STREAM,0);
//绑定ip和port【需要用结构体完成】
        struct sockaddr_in ser;//创建结构体
        bzero(&ser,sizeof(ser));
        ser.sin_family = AF_INET;
        ser.sin_addr.s_addr = inet_addr("127.0.0.1");
        ser.sin_port = htons(10000);
        //把储存在结构体里的ip和port和套接字进行绑定
        bind(sockfd,(sockaddr*)&ser,sizeof(ser));
//监听端口[后者是同时进去的最大数量,如果大于就要进行等待]
        listen(sockfd,100);
//接受       
         struct sockaddr_in cli;//创建结构体
         socklen_t len = sizeof(cli);
         bzero(&cli,sizeof(cli));
         int clifd = accept(sockfd,(sockaddr*)&cli,&len);
        /*
         cout<<"new Client fd :" <<clifd<<",IP:"<<inet_ntoa(cli.sin_addr)<<",PORT:"<<ntohs(cli.sin_port)<<endl;
        */
//读写数据
        while(1){
 char buf[1024];
                bzero(&buf,sizeof(buf));
                ssize_t size = read(clifd,buf,sizeof(buf));
                if(size > 0 ){
                cout<<"客户机"<<clifd<<"号说话了:"<<buf<<endl;
                        bzero(&buf,sizeof(buf));
                        cin>>buf;
                        write(clifd,buf,sizeof(buf));
                }
                else if(size == 0 ){
cout<<"客户机没有连接"<<endl;
                close(clifd);
                }
                else if(size == -1 ){
                cout<<"socket出错"<<endl;
                close(clifd);
                }
        }
        close(sockfd);
        return 0;
}



demo_client

#include <iostream>
using namespace std;
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
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<<"客户机说:"<<buf<<endl;
                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;
}



demo_res

在这里插入图片描述

在这里插入图片描述



版权声明:本文为weixin_45646601原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。