永发信息网

recv一次最大可以收多少字符

答案:4  悬赏:80  手机版
解决时间 2021-02-14 00:17
recv一次最大可以收多少字符
最佳答案
65535ip包的最大值
全部回答
szBuf的长度类型是UINT,结合应用考虑,等于是没有长度上的限制。 (通常根据需要和硬件资源,设定在几K以内) recv函数在未收到指定字节数时阻塞,直到收够指定字节数或超时或对方关闭SOCKET; 如果指定接收数目小于传过来的数目,则剩余字节在系统的SOCKET缓冲区中(当系统缓冲区满时,对方发送线程自动阻塞,停止发送),可以再次调用recv读取,(如果SOCKET关闭,则未接收数据丢失)。
windows: 1.简单服务器 //#include //#pragma comment(lib,"ws2_32.lib") wsadata wsd; static uint port=%%1; uint listen(lpvoid pparam) { socket sserver,sclient; char buf[1024]; int retval; if(wsastartup(makeword(2,2),&wsd)!=0) { return -1;//失败 } sserver=socket(af_inet,sock_stream,ipproto_tcp); if(invalid_socket==sserver) { wsacleanup(); return -1;//创建套接字失败 } sockaddr_in addrserv; addrserv.sin_family=af_inet; addrserv.sin_port=htons((short)pparam); addrserv.sin_addr.s_addr=inaddr_any; retval=bind(sserver,(lpsockaddr)&addrserv,sizeof(sockaddr_in)); if(socket_error==retval) { closesocket(sserver); wsacleanup(); return -1;//绑定套接字失败 } retval=listen(sserver,1); if(socket_error==retval) { closesocket(sserver); wsacleanup(); return -1;//开始监听失败 } sockaddr_in addrclient; int addrclientlen=sizeof(addrclient); sclient=accept(sserver,(sockaddr far*)&addrclient,&addrclientlen); if(invalid_socket==sclient) { closesocket(sserver); wsacleanup(); return -1;//开始接受客户端连接失败 } zeromemory(buf,sizeof(buf)); retval=recv(sclient,buf,sizeof(buf),0); if(socket_error==retval) { closesocket(sserver); closesocket(sclient); wsacleanup(); return -1;//接收数据失败 } cstring %%2(buf); closesocket(sserver); closesocket(sclient); wsacleanup(); return 0; } cwinthread *pthread=afxbeginthread(listen,&port); 2.简单客户端 //#include //#pragma comment(lib,"ws2_32.lib") wsadata wsd; socket shost; sockaddr_in servaddr; char buf[1024]; int retval; if(wsastartup(makeword(2,2),&wsd)!=0) { return -1;//失败 } shost=socket(af_inet,sock_stream,ipproto_tcp); if(invalid_socket==shost) { wsacleanup(); return -1;//创建套接字失败 } servaddr.sin_family=af_inet; servaddr.sin_addr.s_addr=inet_addr(%%3); servaddr.sin_port=htons((short)%%2); int nservaddlen=sizeof(servaddr); retval=connect(shost,(lpsockaddr)&servaddr,sizeof(servaddr)); if(socket_error==retval) { closesocket(shost); wsacleanup(); return -1;//连接服务器失败 } zeromemory(buf,sizeof(buf)); strcpy(buf,%%3); retval=send(shost,buf,sizeof(buf),0); if(socket_error==retval) { closesocket(shost); wsacleanup(); return -1;//向服务器发送数据失败 } closesocket(shost); wsacleanup(); 3.获得本机ip //#include //#pragma comment(lib,"ws2_32.lib") wsadata wsd; if(wsastartup(makeword(2,2),&wsd)!=0) { return -1;//失败 } char szhostname[100],szhostaddress[200]; if(gethostname(szhostname,sizeof(szhostname))!=socket_error) { hostent *phostent=gethostbyname(szhostname); if(phostent!=null){ sprintf(szhostaddress,"%d.%d.%d.%d", ( phostent->h_addr_list[0][0]&0x00ff ), ( phostent->h_addr_list[0][1]&0x00ff ), ( phostent->h_addr_list[0][2]&0x00ff ), ( phostent->h_addr_list[0][3]&0x00ff )); } } else return; cstring %%1(szhostaddress); unix: 1.简单客户端 if((sockfd=socket(af_inet,sock_stream,0))==-1) { fprintf(stderr,"socket error:%s\a\n",strerror(errno)); exit(1); } bzero(&server_addr,sizeof(server_addr)); server_addr.sin_family=af_inet; server_addr.sin_port=htons(portnumber); server_addr.sin_addr=*((struct in_addr *)host-?h_addr); if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"connect error:%s\a\n",strerror(errno)); exit(1); } if((nbytes=read(sockfd,buffer,1024))==-1) { fprintf(stderr,"read error:%s\n",strerror(errno)); exit(1); } buffer[nbytes]=’\0′; printf("i have received:%s\n",buffer); close(sockfd); exit(0); } */ int sock_connect(char *domain,int port) { int white_sock; struct hostent * site; struct sockaddr_in me; site = gethostbyname(domain); if (site==null) return -2; white_sock = socket(af_inet,sock_stream,0); if (white_sock?0) return -1; memset(&me,0,sizeof(struct sockaddr_in)); memcpy(&me.sin_addr,site-?h_addr_list[0],site-?h_length); me.sin_family = af_inet; me.sin_port = htons(port); return (connect(white_sock,(struct sockaddr *)&me,sizeof(struct sockaddr))?0) ? -1 : white_sock; } 2.简单服务器端 #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello[]="hello! are you fine?\n"; if(argc!=2) { fprintf(stderr,"usage:%s portnumber\a\n",argv[0]); exit(1); } if((portnumber=atoi(argv[1]))?0) { fprintf(stderr,"usage:%s portnumber\a\n",argv[0]); exit(1); } if((sockfd=socket(af_inet,sock_stream,0))==-1) { fprintf(stderr,"socket error:%s\n\a",strerror(errno)); exit(1); } bzero(&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family=af_inet; server_addr.sin_addr.s_addr=htonl(inaddr_any); server_addr.sin_port=htons(portnumber); if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"bind error:%s\n\a",strerror(errno)); exit(1); } if(listen(sockfd,5)==-1) { fprintf(stderr,"listen error:%s\n\a",strerror(errno)); exit(1); } while(1) { sin_size=sizeof(struct sockaddr_in); if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1) { fprintf(stderr,"accept error:%s\n\a",strerror(errno)); exit(1); } fprintf(stderr,"server get connection from %s\n", inet_ntoa(client_addr.sin_addr)); if(write(new_fd,hello,strlen(hello))==-1) { fprintf(stderr,"write error:%s\n",strerror(errno)); exit(1); } close(new_fd); } close(sockfd); exit(0); } 客户端程序 */ int daemonsocket; struct sockaddr_in daemonaddr; int bindsocket(void) { daemonsocket = socket(af_inet,sock_stream,0); if (daemonsocket==-1) return 0; daemonaddr.sin_family = af_inet; daemonaddr.sin_port = htons(daemon_port); if (bind(daemonsocket,&daemonaddr,sizeof(daemonaddr))?0) { printf("can not bind!\n"); return 0; } if (listen(daemonsocket,1024)!=0) { printf("can not listen!\n"); return 0; } return 1; } //要查看是否有连线进来,可用以下方式: int incoming_call(void) { fd_set sock; struct timeval tv; int t; fd_zero(&sock); fd_set(daemonsocket,&sock); tv.tv_sec = 60; tv.tv_usec = 0; t = select(daemonsocket + 1,&sock,null,null,&tv); if (t?=0||!fd_isset(daemonsocket,&sock)) return 0; printf("incoming…\n"); return 1; } //connect client //当我们确认有人进来要求服务时,会需要accept connection,可用以下方式 int connectclient(void) { int socksize=sizeof(hostaddr); unsigned char * addr; clientsocket = accept(daemonsocket,(struct sockaddr*)&hostaddr,&socksize); if (clientsocket?0) return 0; addr = (unsigned char *)&hostaddr.sin_addr.s_addr; printf("incoming address:%d.%d.%d.%d\n",addr[0],addr[1],addr[2],addr[3]); return 1; }
不同操作系统不同,由TCP协议定,比如 Linux 2.6.6 :/proc/sys/net/core/rmem_max: 4194304 //4M 查看/proc/sys/net/core/wmem_max: 8388608 //8M 所以,能设置的接收缓冲区的最大值是8M,发送缓冲区的最大值是16M。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
优信二手车(青岛市北区萍乡路16-1)怎么去啊,
通电螺线管的导线是漆包线还是裸导线,和变压
以弱胜强 成语
如图,已知AB平行于CD,BD平分角ABC,CE平分角DC
中班科学教育认识海鸥活动反思
御捷电动汽车(青岛平度市)地址在什么地方,我
3.9层高,楼梯怎么设计
我与老公离婚了,现在儿子归我抚养,我想把儿
远洋汽车销售有限公司(青岛胶州市)怎么去啊,
西门子plc与变频器dp通讯pke ind pwe1 pwe2
河北省省直事业单位考试,省高速公路专技岗位
宜良县九乡法律服务所地址在什么地方,想过去
扶余国是什么民族
带星月菩提有什么忌讳
世界上存在使钢铁变软的化学物质吗?
推荐资讯
万宁药店地址有知道的么?有点事想过去
10吨满载卡车后轮陷入泥潭,怎么样让发动机动
无毒蛇还是毒蛇多?
对组成细胞的有机物描述正确的是BA. 动物乳汁
月经不来吃什么可以催
建政村地址好找么,我有些事要过去
点焊机粘针如何调机
五星火机是哪个国家的?
蒸馒头合面时错把酒曲当泡打粉用后又加入泡打
西部小镇Bar地址在什么地方,想过去办事
坎西湖村地址在哪,我要去那里办事
张家港市港盛木业有限公司地址在哪,我要去那
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?