标题:API函数recv突然不是阻塞型的了
只看楼主
lyl930130
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:323
专家分:111
注 册:2013-5-13
结帖率:91.43%
 问题点数:0 回复次数:2 
API函数recv突然不是阻塞型的了
代码如下
server.c:
程序代码:
#include <stdio.h>
#include <winsock2.h>

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

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 sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(8888);
    sin.sin_addr.S_un.S_addr = INADDR_ANY; 
    if(bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
        printf("bind error !");
    }

    //开始监听
    if(listen(slisten, 5) == SOCKET_ERROR)
    {
        printf("listen error !");
        return 0;
    }

    //循环接收数据
    SOCKET sClient;
    sockaddr_in remoteAddr;
    int nAddrlen = sizeof(remoteAddr);
    char *revData; 
    revData=(char *)malloc(sizeof(char)*255);

    //api文件读写创建函数参数
    HANDLE hFileWrite;//文件句柄
    DWORD dwWritedDataSize;//成功写入的数据大小

    while (true)
    {
        printf("等待连接...\n");
        sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
        if(sClient == INVALID_SOCKET)
        {
            printf("accept error !");
            continue;
        }
        printf("接受到一个连接:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
        
        //接收数据
        int ret = recv(sClient, revData, 255, 0);        
        if(ret > 0)
        {
            revData[ret] = 0x00;
//                printf(revData);
        }

        //发送数据
        bool File_flag=false;
        char * sendData = "你好,TCP客户端!\n";    
        ret=send(sClient, sendData, strlen(sendData), 0);
        while(true)//交互信息
        {
            if(File_flag==false)
            {
                ret = recv(sClient, revData, 255, 0);   
                if(ret > 0)
                {
                    revData[ret] = 0x00;
                    puts(revData);
                    if(strcmp(revData,"quit")==0)
                        break;
//                    if((strstr(revData,"CreateFile")-(int)revData)==0)
                    if(strcmp(revData,"CreateFile")==0)
                    {
                        //api创建文件
                        hFileWrite=CreateFile("C:\\Users\\Your_papa\\Desktop\\temp\\server.txt",
                            GENERIC_WRITE,
                            0,
                            NULL,
                            OPEN_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
                        if(hFileWrite==INVALID_HANDLE_VALUE)
                        {
                            printf("create file error:%d\n",GetLastError());
                            continue;
                        }
                        File_flag=true;
                    }
            
                }

            }
            else
            {
                ret = recv(sClient, revData, 255, 0);
                revData[ret]=0x00;
                if(strcmp(revData,"CloseFile")==0)//api关闭文件,跳出
                {
                    CloseHandle(hFileWrite);
                    File_flag=false;
                    break;
                }
                printf("file data:");
                puts(revData);
                //api写文件
                if(!WriteFile(hFileWrite,revData,ret,&dwWritedDataSize,NULL))
                    printf("写文件失败:%d\n",GetLastError());
                else
                    printf("写文件成功,写入%d字节。\n",dwWritedDataSize);
            }
            

        }
        printf("\n\n");
        closesocket(sClient);
    }
    free(revData);
    closesocket(slisten);
    WSACleanup();
    return 0;
}


client.c:
程序代码:
#include <WINSOCK2.H>
#include <STDIO.H>

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


int main(int argc, char* argv[])
{
    WORD sockVersion = MAKEWORD(2,2);
    WSADATA data; 
    if(WSAStartup(sockVersion, &data) != 0)
    {
        return 0;
    }

    SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(sclient == INVALID_SOCKET)
    {
        printf("invalid socket !");
        return 0;
    }

    sockaddr_in serAddr;
    serAddr.sin_family = AF_INET;
    serAddr.sin_port = htons(8888);
    serAddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.104"); 
    if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
    {
        printf("connect error !");
        closesocket(sclient);
        return 0;
    }
    char sendData[1024] = "你好,TCP服务端,我是客户端!\n";
    send(sclient, sendData, strlen(sendData), 0);

    char recData[255];
    int ret = recv(sclient, recData, 255, 0);
    if(ret > 0)
    {
        recData[ret] = 0x00;
        printf(recData);
    }
    while(true)//信息交互
    {
        gets(sendData);
        ret=send(sclient, sendData, strlen(sendData), 0);
        if(strcmp(sendData,"quit")==0)
            break;
    }
    printf("end of data exchange for client\n");
    
    closesocket(sclient);
    WSACleanup();
    system("pause");
    return 0;
}


当客户端输入CreateFile,服务器创建文本后,客户端再输入quit,服务器就一直循环输出。
filedata:
写文件成功,写入0字节

recv不是阻塞函数吗,为什么会出现这种没有输入一直当做有输入的情况。

求教,谢谢。
搜索更多相关主题的帖子: comment 
2016-02-08 21:01
lyl930130
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:323
专家分:111
注 册:2013-5-13
得分:0 
还有就是 recv如何输入换行符。
2016-02-08 21:26
lyl930130
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:323
专家分:111
注 册:2013-5-13
得分:0 
找到原因了。因为客户端断开连接之后,会有反馈给服务端。但是因为服务端缓存没有清空,所以缓存里还是上一次传输的内容。所以会循环输出缓存里的数据。至于为什么会一直循环输出,原因不知道,不过猜测是因为断了连接之后造成的。
2016-02-21 15:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-461646-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.314544 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved