C、 TCP,recvAll块没有接收任何内容
我对C和socket编程都是新手,所以请耐心听我说。下面的代码主要来自Beej网络指南,但有一些改动。我附加了接收器代码(在本例中是TCP服务器),用于侦听多个TCP连接。我有一个发送器(客户端),它不断地向这个接收器发送固定的数据块。如果我只调用recv(),而不是调用recv_all函数,那么这段代码(我清理并删除了一些与我的问题无关的函数定义)就可以工作。但问题是,我需要对接收到的每个数据块进行处理,所以我需要整个数据块。所以我想我应该使用recv_all() 现在的问题是它在recv_all()中陷入了无限循环,因为n总是0。我真的很感谢你的帮助C、 TCP,recvAll块没有接收任何内容,c,sockets,tcp,recv,C,Sockets,Tcp,Recv,我对C和socket编程都是新手,所以请耐心听我说。下面的代码主要来自Beej网络指南,但有一些改动。我附加了接收器代码(在本例中是TCP服务器),用于侦听多个TCP连接。我有一个发送器(客户端),它不断地向这个接收器发送固定的数据块。如果我只调用recv(),而不是调用recv_all函数,那么这段代码(我清理并删除了一些与我的问题无关的函数定义)就可以工作。但问题是,我需要对接收到的每个数据块进行处理,所以我需要整个数据块。所以我想我应该使用recv_all() 现在的问题是它在recv_a
#define PORT "3490" // the port users will be connecting to
#define BACKLOG 20 // how many pending connections queue will hold
#define MAXDATASIZE 801 // max number of bytes we can get at once
int recv_all(int socket, char *buffer, int *length)
{
int total = 0; // how many bytes we've sent
int bytesleft = *length; // how many we have left to send
int n;
while(total < *length) {
n = recv(socket, buffer+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*length = total; // return number actually received here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
int main(void)
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;
double buf[MAXDATASIZE];
int lenRecv;
struct sockaddr_in local_addr; // For the new addition to bind it to an interface
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
freeaddrinfo(servinfo); // all done with this structure
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
printf("server: waiting for connections...\n");
while(1) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
printf("server: got connection from %s\n", s);
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
for (int i=0; i<1000000; i++) {
int rowInfoSize;
//if ((numbytes = recv(sockfd, buf, sizeof (buf), 0)) == -1) {
//if ((numbytes = recv(new_fd, buf, sizeof (buf), MSG_WAITALL)) == -1) { // I THINK THE BETTER WAY IS to CHECK THE OUTPUT AND LOOP UNTIL COMPLETE.
lenRecv = sizeof (buf);
//if (recv_all(new_fd, (char *)buf, &lenRecv) == -1) {
if (recv_all(new_fd, buf, &lenRecv) == -1) {
perror("sendall");
printf("We only sent %d bytes because of the error!\n", lenRecv);
}
}
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}
return 0;
}
#定义端口“3490”//端口用户将连接到的端口
#定义BACKLOG 20//队列将容纳多少个挂起的连接
#定义MAXDATASIZE 801//一次可以获取的最大字节数
int recv_all(int套接字、字符*缓冲区、int*长度)
{
int total=0;//我们发送了多少字节
int bytesleet=*length;//我们还有多少要发送
int n;
while(总长度<*长度){
n=recv(套接字,缓冲区+总计,字节左,0);
如果(n==-1){break;}
总+=n;
字节左-=n;
}
*length=total;//此处实际收到的返回号
return n==-1?-1:0;//失败时返回-1,成功时返回0
}
内部主(空)
{
int sockfd,new\u fd;//在sock\u fd上侦听,在new\u fd上新建连接
结构addrinfo提示,*servinfo,*p;
struct sockaddr\u存储它们的地址;//连接器的地址信息
袜子的大小;
struct-sigaction-sa;
int yes=1;
字符s[INET6_ADDRSTRLEN];
int-rv;
双buf[MAXDATASIZE];
int lenRecv;
struct sockaddr\u in local_addr;//用于将新添加绑定到接口
memset(提示和提示,0,提示大小);
hits.ai_family=AF_unsec;
hits.ai_socktype=SOCK_流;
hits.ai_flags=ai_PASSIVE;//使用我的IP
if((rv=getaddrinfo(NULL、端口、提示和服务信息))!=0){
fprintf(标准,“getaddrinfo:%s\n”,gai_strerror(rv));
返回1;
}
//循环遍历所有结果并绑定到第一个结果
for(p=servinfo;p!=NULL;p=p->ai_next){
if((sockfd=socket(p->ai_系列,p->ai_socktype,p->ai_协议))=-1){
perror(“服务器:套接字”);
继续;
}
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))=-1){
perror(“setsockopt”);
出口(1);
}
如果(绑定(sockfd,p->ai_addr,p->ai_addrlen)=-1){
关闭(sockfd);
perror(“服务器:绑定”);
继续;
}
打破
}
freeaddrinfo(servinfo);//所有操作都使用此结构完成
如果(侦听(sockfd,BACKLOG)=-1){
佩罗尔(“倾听”);
出口(1);
}
sa.sa_handler=sigchld_handler;//获取所有死进程
sigemptyset(和sa.sa_面具);
sa.sa_标志=sa_重启;
if(sigaction(SIGCHLD,&sa,NULL)=-1){
佩罗尔(“sigaction”);
出口(1);
}
printf(“服务器:正在等待连接…\n”);
while(1){//main accept()循环
sin_size=其地址的大小;
新地址=接受(sockfd,(结构sockaddr*)及其地址和大小);
如果(新的_fd==-1){
佩罗(“接受”);
继续;
}
inet_ntop(他们的_addr.ss_系列,get_in_addr((struct sockaddr*)和他们的_addr),s,sizeof s);
printf(“服务器:已从%s获得连接\n”,s);
如果(!fork()){//这是子进程
close(sockfd);//孩子不需要侦听器
对于(int i=0;i给recv_all一个超时值
int recv_all(int socket, char *buffer, int *length, int timeout)
并使用select BEFOR recv()
看起来像这样
int recv_all(int socket, char *buffer, int *length)
{
int total = 0; // how many bytes we've sent
int bytesleft = *length; // how many we have left to send
int n = -1;
struct timeval timeout;
fd_set rset;
int rc;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while(total < *length) {
FD_ZERO(&rset);
FD_SET(socket, &rset);
rc = select(socket + 1, &rset, NULL, NULL, &timeout);
if(rc < 0)
{
// errno
n = -1;
break;
}
if(0 == rc)
{
// timeout
n = -1;
break;
}
if(FD_ISSET(socket, &rset))
{
n = recv(socket, buffer+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
}
}
*length = total; // return number actually received here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
int recv\u all(int套接字、字符*缓冲区、int*长度)
{
int total=0;//我们发送了多少字节
int bytesleet=*length;//我们还有多少要发送
int n=-1;
结构timeval超时;
fd_集rset;
int rc;
timeout.tv_sec=1;
timeout.tv_usec=0;
while(总长度<*长度){
FD_零和rset;
FD_装置(插座和rset);
rc=select(套接字+1,&rset,NULL,NULL,&timeout);
if(rc<0)
{
//厄尔诺
n=-1;
打破
}
如果(0==rc)
{
//超时
n=-1;
打破
}
if(FD_ISSET(插座和rset))
{
n=recv(套接字,缓冲区+总计,字节左,0);
如果(n==-1){break;}
总+=n;
字节左-=n;
timeout.tv_sec=1;
timeout.tv_usec=0;
}
}
*length=total;//此处实际收到的返回号
return n==-1?-1:0;//失败时返回-1,成功时返回0
}
您正在忽略流的结尾。如果n==0
对等方已断开连接。您的代码将永远循环
扔掉它,使用recv()
使用MSG_WAITALL
选项。那么您的数据块在任何情况下都是801字节长吗?recv_all
的目的是接收特定数量的字节。如果您的数据小于此值,您将无法等待更多的数据。^^^^^^^^^ aaarrghhhhhhhhhhhh!@Ari因为您的recv_all()函数不处理recv()返回0时的情况(这意味着客户端已断开连接)…您需要添加代码来处理该情况,就像recv()返回时一样-1@Arirecv()函数是一个没有bug的标准函数您发布的函数有bug,这就是您遇到问题的原因。它试图在您传递给它时接收尽可能多的字节,然后,如果客户端在发送尽可能多的数据之前断开连接,那么它就会变得异常
int recv_all(int socket, char *buffer, int *length)
{
int total = 0; // how many bytes we've sent
int bytesleft = *length; // how many we have left to send
int n = -1;
struct timeval timeout;
fd_set rset;
int rc;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while(total < *length) {
FD_ZERO(&rset);
FD_SET(socket, &rset);
rc = select(socket + 1, &rset, NULL, NULL, &timeout);
if(rc < 0)
{
// errno
n = -1;
break;
}
if(0 == rc)
{
// timeout
n = -1;
break;
}
if(FD_ISSET(socket, &rset))
{
n = recv(socket, buffer+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
}
}
*length = total; // return number actually received here
return n==-1?-1:0; // return -1 on failure, 0 on success
}