Windows TCP 通信测试_1

一、单对单通信测试

应用函数 socket、bind、connect、listen、accept、recv、send(win下的函数)等

1、客户端demo client.cpp

#include
#include
#include
#include
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define CONNECT_COUNT_MAX  20
#define CONNECT_INTERVAL_MS 1000
int main()
{
	WORD sockVersion = MAKEWORD(2, 2);
	WSADATA data;
	if(WSAStartup(sockVersion, &data)!=0)
	{
		return 0;
	}
while(true){
        SOCKET sclient = socket(AF_INET, SOCK_STREAM, 0);//IPPROTO_TCP
        if(sclient == INVALID_SOCKET)
        {
            printf("invalid socket!");
            WSACleanup();
            system("pause");
            return 0;
        }
        // 设定客户端协议族、端口、ip信息
        sockaddr_in clientAddr;
        clientAddr.sin_family = AF_INET;
        clientAddr.sin_port = htons(8888);
        clientAddr.sin_addr.S_un.S_addr = inet_addr("127.6.6.2");

        if (bind(sclient,(struct sockaddr *)&clientAddr,sizeof(clientAddr))==0){    //client信息绑定到clientfd上
            printf("client socket bind success\n");
        }; 
        
        // 设定服务端协议族、端口、ip信息
        sockaddr_in serAddr;
        serAddr.sin_family = AF_INET;
        serAddr.sin_port = htons(9999);
        serAddr.sin_addr.S_un.S_addr = inet_addr("127.6.6.254");
    // while(true){
        int connect_flag_ = 0; //TCP connect 结果标志位
        int connect_count_ = 0; //TCP connect 重试次数
        while(connect_count_ <  CONNECT_COUNT_MAX){
            connect_flag_ = connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr));
            if(connect_flag_ == SOCKET_ERROR){  //连接失败 
                printf("connect error, retry time: [%d]!\n", connect_count_);
                connect_count_ ++;
                Sleep(CONNECT_INTERVAL_MS); //休眠一段时间再重试connect
            }else{
                printf("connect success, retry time: [%d]!\n", connect_count_);
                break;
            }
        }
        if(connect_count_ == CONNECT_COUNT_MAX){      //达到了最大connect次数,结束
            printf("connect error ,retry finshed!\n");
            closesocket(sclient);
            WSACleanup();
            system("pause");
            return 0;
        }
        string client_id = "[client_1] : ";
        bool input_flag_ = false;   //选择传入手动输入消息还是默认消息

        // string data;
        const char * sendData;
        int client_send_flag_ = 0;
        if(input_flag_){
            char data[128] = {};
            cin >> data;
            sendData = (client_id + data).c_str();   //string转const char*
            cout << "client msg : " << sendData << endl;
            client_send_flag_ = send(sclient, sendData, strlen(sendData), 0);
        }else{
            char *sendData = "我是北方的狼";
            cout << "client msg : " << sendData << endl;
            client_send_flag_ = send(sclient, sendData, strlen(sendData), 0);
            Sleep(3000);
        };

        //send()用来将数据由指定的socket传给对方主机
        /*
            int send(int s, const void * msg, int len, unsigned int flags)
            s为已建立好连接的socket,msg指向数据内容,len则为数据长度,参数flags一般设0
            成功则返回实际传送出去的字符数,失败返回-1,错误原因存于error 
        */
        if(client_send_flag_ >= 0){   //send 失败或成功提示
            printf(" send msg success!\n");
        }else{
            printf(" send msg fail!!\n");
        }

        char recData[255];
        int ret = recv(sclient, recData, 255, 0);
            /*  函数原型:int recv( SOCKET s, char *buf, int len, int flags) 
                    功能: 用recv函数从TCP连接的另一端接收数据。 
                    参数一:指定接收端套接字描述符; 
                    参数二:指明一个缓冲区,该缓冲区用来存放recv函数接收到的数据; 
                    参数三:指明buf的长度; 
                    参数四 :一般置为0。 
                返回值 : 读出来的字节大小
            */
        if(ret>0){
            recData[ret] = 0x00;
            printf(recData);
        } 
        closesocket(sclient);
    }
    
	// }
	
	WSACleanup();
	return 0;
	
}

特点

1、轮询重试

2、测试时可先于服务端启动

3、发送消息可以选择周期发或者手动输入;(bug:目前手动输入过长字符会有发送异常的情况出现)

2、服务端demo server.cpp

#include   
#include   
#include
#include
#include
#include

// #include

#pragma comment(lib,"ws2_32.lib")  

#define DATA_SIZE 1024
using namespace std;
#if 0
future_status getRecvResult(SOCKET sClientfd, char *revData , int* ret){
    std::future ret1 = std::async(std::launch::async, recv , sClientfd, revData, DATA_SIZE, 0); //异步调用
    std::future_status sts = ret1.wait_for(std::chrono::seconds(1)); //设置超时时间 s'
    *ret = ret1.get();
    return sts;
}
#endif
int main(int argc, char* argv[])  
{  
    //初始化WSA  
    WORD sockVersion = MAKEWORD(2,2);  
    WSADATA wsaData;  
    if(WSAStartup(sockVersion, &wsaData)!=0)  
    {  
        return 0;  
    }  
  
    //创建套接字  
    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  
    if(slisten == INVALID_SOCKET)  
    {  
        printf("socket error !");  
        return 0;  
    }  
  
    //绑定IP和端口  
    sockaddr_in serveraddr;  
    serveraddr.sin_family = AF_INET;  
    serveraddr.sin_port = htons(9999);  
    // serveraddr.sin_addr.S_un.S_addr = INADDR_ANY;   //可以是系统端口 
    serveraddr.sin_addr.S_un.S_addr = inet_addr("127.6.6.254");
    if(bind(slisten, (LPSOCKADDR)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)  
    {  
        printf("bind error !");  
    }  
  
    //开始监听  
    if(listen(slisten, 5) == SOCKET_ERROR)
    {  
        printf("listen error !");  
        return 0;  
    }  
  
    //循环接收数据  
    SOCKET sClientfd;  
    sockaddr_in remoteAddr;  
    int nAddrlen = sizeof(remoteAddr);  
      
    while (true)  
    {  
        printf("等待连接...\n");  
        sClientfd = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
        /*
        listen监听客户端来的链接,accept将客户端的信息绑定到一个socket上,也就是给客户端创建一个socket,通过返回值返回给我们客户端的socket。
        accept函数的第一个参数为服务器的socket描述字,第二个参数为指向struct sockaddr *的指针,用于返回客户端的协议地址,第三个参数为协议地址的长度。
        如果accpet成功,那么其返回值是由内核自动生成的一个全新的描述字,代表与返回客户的TCP连接。
            成功:
                返回值就是给客户端包好的socket; 与客户端通信就靠这个
                
            失败:
                返回INVALIE_SOCKET; 通过WSAGetLastError()得到错误码
        */
    // while (true)  
    // {    
        if(sClientfd <= 0 )  
        {  
            printf("accept error !");
            cout << WSAGetLastError() << endl;
            Sleep(2000);  
            continue;  
        }  
        printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));  
          
        //接收数据 
        // char revData[2] = {}; 
        // int data_size = sizeof(revData);
        string tmp = "";
        
        int ret = 0;
        do    //怕数据太长,一次recv copy不完,为了把client端一次发的的消息拼接、打印
        {
            char revData[DATA_SIZE] = {}; 
            revData[DATA_SIZE] = 0x00;   // 抵消数组后面的字符
            cout << "start " << revData < ret1 = std::async(std::launch::async, recv , sClientfd, revData, DATA_SIZE, 0); //异步调用
                std::future_status sts = ret1.wait_for(std::chrono::seconds(1)); //设置超时时间 s'
            
                if(sts == std::future_status::ready){
                    // ret = ret1.get();
                    printf("超时时间内收到了回复\n");
                }else if(sts == std::future_status::timeout){
                    printf("设定时间内无消息\n");
                    ret = 0;
                    break;      
            }
            */
            ret = recv(sClientfd, revData, DATA_SIZE, 0);  // 在主线程操作,如果recv大小正好是个整数包,一直阻塞在这,不会给该client返回消息;
                //这是上面do while想做的事情可能带来的影响
            cout << "server ret = " << ret << endl;
            if(ret <= 0){
                printf("receive msg error\n");
                // system("pause");
                Sleep(3000);
                break;
            }
            
            if(ret  < DATA_SIZE){
                revData[ret] = 0x00;
                cout << "last phase " << revData <

特点

1、为了一次打印client发送的消息,做了一点愚蠢的特殊处理。。。

2、没有做超时,socket没有消息会一直阻塞;

3、验证

编译:

g++ server.cpp -o server -lwsock32

g++ client.cpp -o client -lwsock32  

Windows TCP 通信测试_1_第1张图片

二、多个客户端通信测试(有bug)

新增加select的使用

1、客户端新增demo client_2.cpp

#include
#include
#include
#include
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define CONNECT_COUNT_MAX  20
#define CONNECT_INTERVAL_MS 1000
#define INPUT_FLAG   true
int main()
{
	WORD sockVersion = MAKEWORD(2, 2);
	WSADATA data;
	if(WSAStartup(sockVersion, &data)!=0)
	{
		return 0;
	}
while(true){
        SOCKET sclient = socket(AF_INET, SOCK_STREAM, 0);//IPPROTO_TCP
        if(sclient == INVALID_SOCKET)
        {
            printf("invalid socket!");
            WSACleanup();
            system("pause");
            return 0;
        }
        // 设定客户端协议族、端口、ip信息
        sockaddr_in clientAddr;
        clientAddr.sin_family = AF_INET;
        clientAddr.sin_port = htons(8888);
        clientAddr.sin_addr.S_un.S_addr = inet_addr("127.6.6.4");

        if (bind(sclient,(struct sockaddr *)&clientAddr,sizeof(clientAddr))==0){    //client信息绑定到clientfd上
            printf("client socket bind success\n");
        }; 
        
        // 设定服务端协议族、端口、ip信息
        sockaddr_in serAddr;
        serAddr.sin_family = AF_INET;
        serAddr.sin_port = htons(9999);
        serAddr.sin_addr.S_un.S_addr = inet_addr("127.6.6.254");
    // while(true){
        int connect_flag_ = 0; //TCP connect 结果标志位
        int connect_count_ = 0; //TCP connect 重试次数
        while(connect_count_ <  CONNECT_COUNT_MAX){
            connect_flag_ = connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr));
            if(connect_flag_ == SOCKET_ERROR){  //连接失败 
                printf("connect error, retry time: [%d]!\n", connect_count_);
                connect_count_ ++;
                Sleep(CONNECT_INTERVAL_MS); //休眠一段时间再重试connect
            }else{
                printf("connect success, retry time: [%d]!\n", connect_count_);
                break;
            }
        }
        if(connect_count_ == CONNECT_COUNT_MAX){      //达到了最大connect次数,结束
            printf("connect error ,retry finshed!\n");
            closesocket(sclient);
            WSACleanup();
            system("pause");
            return 0;
        }
        string client_id = "[client_1] : ";
        bool input_flag_ = INPUT_FLAG;   //选择传入手动输入消息还是默认消息

        // string data;
        const char * sendData;
        int client_send_flag_ = 0;
        if(input_flag_){
            char data[128] = {};
            cin >> data;
            sendData = (client_id + data).c_str();   //string转const char*
            cout << "client msg : " << sendData << endl;
            client_send_flag_ = send(sclient, sendData, strlen(sendData), 0);
        }else{
            char *sendData = "我是北方的羊";
            cout << "client msg : " << sendData << endl;
            client_send_flag_ = send(sclient, sendData, strlen(sendData), 0);
            Sleep(3000);
        };

        //send()用来将数据由指定的socket传给对方主机
        /*
            int send(int s, const void * msg, int len, unsigned int flags)
            s为已建立好连接的socket,msg指向数据内容,len则为数据长度,参数flags一般设0
            成功则返回实际传送出去的字符数,失败返回-1,错误原因存于error 
        */
        if(client_send_flag_ >= 0){   //send 失败或成功提示
            printf(" send msg success!\n");
        }else{
            printf(" send msg fail!!\n");
        }

        char recData[255];
        int ret = recv(sclient, recData, 255, 0);
            /*  函数原型:int recv( SOCKET s, char *buf, int len, int flags) 
                    功能: 用recv函数从TCP连接的另一端接收数据。 
                    参数一:指定接收端套接字描述符; 
                    参数二:指明一个缓冲区,该缓冲区用来存放recv函数接收到的数据; 
                    参数三:指明buf的长度; 
                    参数四 :一般置为0。 
                返回值 : 读出来的字节大小
            */
        if(ret>0){
            recData[ret] = 0x00;
            printf(recData);
        } 
        closesocket(sclient);
    }
    
	// }
	
	
	WSACleanup();
	return 0;
	
}

2、服务端demo server_select.cpp

#include   
#include   
#include
#include
#include
#include

// #include

#pragma comment(lib,"ws2_32.lib")  

#define DATA_SIZE 1024
using namespace std;
#if 0
future_status getRecvResult(SOCKET sClientfd, char *revData , int* ret){
    std::future ret1 = std::async(std::launch::async, recv , sClientfd, revData, DATA_SIZE, 0); //异步调用
    std::future_status sts = ret1.wait_for(std::chrono::seconds(1)); //设置超时时间 s'
    *ret = ret1.get();
    return sts;
}
#endif
int main(int argc, char* argv[])  
{  
    //初始化WSA  
    WORD sockVersion = MAKEWORD(2,2);  
    WSADATA wsaData;  
    if(WSAStartup(sockVersion, &wsaData)!=0)  
    {  
        return 0;  
    }  
  
    //创建套接字  
    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  
    if(slisten == INVALID_SOCKET)  
    {  
        printf("socket error !");  
        return 0;  
    }  
  
    //绑定IP和端口  
    sockaddr_in serveraddr;  
    serveraddr.sin_family = AF_INET;  
    serveraddr.sin_port = htons(9999);  
    // serveraddr.sin_addr.S_un.S_addr = INADDR_ANY;   //可以是系统端口 
    serveraddr.sin_addr.S_un.S_addr = inet_addr("127.6.6.254");
    if(bind(slisten, (LPSOCKADDR)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)  
    {  
        printf("bind error !");  
    }  
  
    //开始监听  
    if(listen(slisten, 5) == SOCKET_ERROR)  
    {  
        printf("listen error !");  
        return 0;  
    }  
  
    //循环接收数据  
    SOCKET sClientfd;  
    sockaddr_in remoteAddr;  
    int nAddrlen = sizeof(remoteAddr);  
    // select 相关设置
    fd_set rfds, readset, wfds, writeset;

    SOCKET Clientfd[1024]; 
    int maxfd = slisten; 
	FD_ZERO(&rfds);
    FD_ZERO(&wfds);
	FD_SET(slisten, &rfds);
    while (true)  
    {
        printf("等待连接...\n");
        int selectRet = select(maxfd+1,&rfds,NULL,NULL,NULL);//select()调用返回处于就绪状态并且已经包含在fd_set结构中的描述字总数;如果超时则返回0;
                                                                //否则的话,返回SOCKET_ERROR错误,应用程序可通过WSAGetLastError获取相应错误代码。
        if(selectRet <= 0){
            printf("select error [%d]\n", selectRet);       // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            Sleep(3000);
            // continue;
        }
        cout << "selectRet = " << selectRet << endl;
        cout << "maxfd = " << maxfd << endl;
        cout << "slisten = " << slisten << endl;
        for (int fd = 0; fd <= maxfd; fd++){
            if(FD_ISSET(fd,&rfds)){
                if(fd == slisten){
                    sClientfd = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);  //如果监听的socket有消息,就接受client的socket
                    FD_SET(sClientfd, &rfds);
                    maxfd = sClientfd > maxfd ? sClientfd : maxfd;      // 更新maxfd
                    break;
                }else{
                    break;
                }
            }
            else{
                // cout << "fd = " << fd << endl;
            }
        }
        // if(FD_ISSET(slisten,&rfds)){
        //     sClientfd = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
        // }
        
        /*
        listen监听客户端来的链接,accept将客户端的信息绑定到一个socket上,也就是给客户端创建一个socket,通过返回值返回给我们客户端的socket。
        accept函数的第一个参数为服务器的socket描述字,第二个参数为指向struct sockaddr *的指针,用于返回客户端的协议地址,第三个参数为协议地址的长度。
        如果accpet成功,那么其返回值是由内核自动生成的一个全新的描述字,代表与返回客户的TCP连接。
            成功:
                返回值就是给客户端包好的socket; 与客户端通信就靠这个
                
            失败:
                返回INVALIE_SOCKET; 通过WSAGetLastError()得到错误码
        */
    // while (true)  
    // {    
        if(sClientfd <= 0 )  
        {  
            printf("accept error !");
            cout << WSAGetLastError() << endl;
            Sleep(2000);  
            continue;  
        }  
        printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));  
          
        //接收数据 
        // char revData[2] = {}; 
        // int data_size = sizeof(revData);
        string tmp = "";
        
        int ret = 0;
        do    //怕数据太长,一次recv copy不完,为了把client端一次发的的消息拼接、打印
        {
            char revData[DATA_SIZE] = {}; 
            revData[DATA_SIZE] = 0x00;   // 抵消数组后面的字符
            cout << "start " << revData < ret1 = std::async(std::launch::async, recv , sClientfd, revData, DATA_SIZE, 0); //异步调用
                std::future_status sts = ret1.wait_for(std::chrono::seconds(1)); //设置超时时间 s'
            
                if(sts == std::future_status::ready){
                    // ret = ret1.get();
                    printf("超时时间内收到了回复\n");
                }else if(sts == std::future_status::timeout){
                    printf("设定时间内无消息\n");
                    ret = 0;
                    break;      
            }
            */
            ret = recv(sClientfd, revData, DATA_SIZE, 0);  // 在主线程操作,如果recv大小正好是个整数包,一直阻塞在这,不会给该client返回消息;
                //这是上面do while想做的事情可能带来的影响
            cout << "server ret = " << ret << endl;
            if(ret <= 0){
                printf("receive msg error\n");
                // system("pause");
                Sleep(3000);
                break;
            }
            
            if(ret  < DATA_SIZE){
                revData[ret] = 0x00;
                cout << "last phase " << revData <

特点

目前也没超时,不能很好的处理多个客户端的互相影响 

3、验证

编译执行,用到上面所有的client

Windows TCP 通信测试_1_第2张图片

手动输入的client会把服务端的进程阻塞住,导致另一个client的消息也进不来,手动输入消息后,会暂时解除当前阻塞,问题不小,小白太难了,后面再搞

你可能感兴趣的:(windows,tcp/ip,网络协议)