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不是阻塞函数吗,为什么会出现这种没有输入一直当做有输入的情况。
求教,谢谢。