Correct and error in recv() and recvfrom() return value

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4402 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2012-02-18 18:13:30 +00:00
parent 5c75c3edeb
commit ba73620d39
16 changed files with 92 additions and 52 deletions

View File

@ -188,7 +188,15 @@ int ftpd_daemon(int s_argc, char **s_argv)
*/ */
ret = ftpd_session(handle, 5000); ret = ftpd_session(handle, 5000);
printf("FTP daemon [%d] ftpd_session returned %d\n", g_ftpdglob.pid, ret);
/* If any interesting happened (i.e., any thing other than a timeout),
* then report the interesting event.
*/
if (ret != -ETIMEDOUT)
{
printf("FTP daemon [%d] ftpd_session returned %d\n", g_ftpdglob.pid, ret);
}
} }
/* Close the FTPD server and exit (we can get here only if /* Close the FTPD server and exit (we can get here only if

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* examples/nettest/nettest-client.c * examples/nettest/nettest-client.c
* *
* Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -169,6 +169,11 @@ void send_client(void)
message("client: recv failed: %d\n", errno); message("client: recv failed: %d\n", errno);
goto errout_with_socket; goto errout_with_socket;
} }
else if (nbytesrecvd == 0)
{
message("client: The server closed the connection\n");
goto errout_with_socket;
}
totalbytesrecvd += nbytesrecvd; totalbytesrecvd += nbytesrecvd;
message("client: Received %d of %d bytes\n", totalbytesrecvd, SENDSIZE); message("client: Received %d of %d bytes\n", totalbytesrecvd, SENDSIZE);
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* examples/nettest/nettest-server.c * examples/nettest/nettest-server.c
* *
* Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -149,11 +149,16 @@ void recv_server(void)
for (;;) for (;;)
{ {
nbytesread = recv(acceptsd, buffer, 2*SENDSIZE, 0); nbytesread = recv(acceptsd, buffer, 2*SENDSIZE, 0);
if (nbytesread <= 0) if (nbytesread < 0)
{ {
message("server: recv failed: %d\n", errno); message("server: recv failed: %d\n", errno);
goto errout_with_acceptsd; goto errout_with_acceptsd;
} }
else if (nbytesread == 0)
{
message("server: The client broke the connection\n");
goto errout_with_acceptsd;
}
message("Received %d bytes\n", nbytesread); message("Received %d bytes\n", nbytesread);
} }
#else #else
@ -164,11 +169,16 @@ void recv_server(void)
{ {
message("server: Reading...\n"); message("server: Reading...\n");
nbytesread = recv(acceptsd, &buffer[totalbytesread], 2*SENDSIZE - totalbytesread, 0); nbytesread = recv(acceptsd, &buffer[totalbytesread], 2*SENDSIZE - totalbytesread, 0);
if (nbytesread <= 0) if (nbytesread < 0)
{ {
message("server: recv failed: %d\n", errno); message("server: recv failed: %d\n", errno);
goto errout_with_acceptsd; goto errout_with_acceptsd;
} }
else if (nbytesread == 0)
{
message("server: The client broke the connection\n");
goto errout_with_acceptsd;
}
totalbytesread += nbytesread; totalbytesread += nbytesread;
message("server: Received %d of %d bytes\n", totalbytesread, SENDSIZE); message("server: Received %d of %d bytes\n", totalbytesread, SENDSIZE);

View File

@ -1,8 +1,8 @@
/**************************************************************************** /****************************************************************************
* examples/poll/host.c * examples/poll/host.c
* *
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -137,6 +137,11 @@ int main(int argc, char **argv, char **envp)
message("client: recv failed: %d\n", errno); message("client: recv failed: %d\n", errno);
goto errout_with_socket; goto errout_with_socket;
} }
else if (nbytesrecvd == 0)
{
message("client: The server broke the connections\n");
goto errout_with_socket;
}
inbuf[nbytesrecvd] = '\0'; inbuf[nbytesrecvd] = '\0';
message("client: Received '%s' (%d bytes)\n", inbuf, nbytesrecvd); message("client: Received '%s' (%d bytes)\n", inbuf, nbytesrecvd);

View File

@ -1,8 +1,8 @@
/**************************************************************************** /****************************************************************************
* examples/poll/net_listener.c * examples/poll/net_listener.c
* *
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions

View File

@ -1,8 +1,8 @@
/**************************************************************************** /****************************************************************************
* examples/poll/net_reader.c * examples/poll/net_reader.c
* *
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions

View File

@ -1,8 +1,8 @@
/**************************************************************************** /****************************************************************************
* examples/udp/udp-server.c * examples/udp/udp-server.c
* *
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2009, 2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions

View File

@ -1,8 +1,8 @@
/**************************************************************************** /****************************************************************************
* netutils/dhcpc/dhcpc.c * netutils/dhcpc/dhcpc.c
* *
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Based heavily on portions of uIP: * Based heavily on portions of uIP:
* *

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* netutils/dhcpd/dhcpd.c * netutils/dhcpd/dhcpd.c
* *
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -796,7 +796,7 @@ static int ftpd_rxpoll(int sd, int timeout)
if (ret == 0) if (ret == 0)
{ {
nvdbg("poll() timed out\n"); //nvdbg("poll() timed out\n");
return -ETIMEDOUT; return -ETIMEDOUT;
} }
else if (ret < 0) else if (ret < 0)
@ -870,7 +870,14 @@ static int ftpd_accept(int sd, FAR void *addr, FAR socklen_t *addrlen,
ret = ftpd_rxpoll(sd, timeout); ret = ftpd_rxpoll(sd, timeout);
if (ret < 0) if (ret < 0)
{ {
nvdbg("ftpd_rxpoll() failed: %d\n", ret); /* Only report interesting, infrequent errors (not the common timeout) */
#ifdef CONFIG_DEBUG_NET
if (ret != -ETIMEDOUT)
{
ndbg("ftpd_rxpoll() failed: %d\n", ret);
}
#endif
return ret; return ret;
} }
} }
@ -909,34 +916,18 @@ static ssize_t ftpd_recv(int sd, FAR void *data, size_t size, int timeout)
} }
} }
/* Receive the data... waiting if necessary */ /* Receive the data... waiting if necessary. The client side will break the
* connection after the file has been sent. Zero (end-of-file) should be
* received in this case.
*/
ret = recv(sd, data, size, 0); ret = recv(sd, data, size, 0);
if (ret < 0) if (ret < 0)
{ {
int errval = errno; int errval = errno;
/* Special case some TCP read errors. The client side will break the ndbg("recv() failed: %d\n", errval);
* connection after the file has been sent. return -errval;
*/
#warning FIXME
/* When the client breaks the connection, the NuttX socket layer will
* return an error with errno == ENOTCONN. This is wrong! It should
* return 0 (end-of-file) in that case! We work around the bug and
* report end-of-file for that case here. This needs to be fixed
* someday.
*/
if (errval == ENOTCONN)
{
nvdbg("Connection lost, returning end-of-file\n");
ret = 0;
}
else
{
ndbg("recv() failed: %d\n", errval);
return -errval;
}
} }
return ret; return ret;
@ -4342,7 +4333,14 @@ int ftpd_session(FTPD_SESSION handle, int timeout)
&session->cmd.addrlen, timeout); &session->cmd.addrlen, timeout);
if (session->cmd.sd < 0) if (session->cmd.sd < 0)
{ {
ndbg("ftpd_accept() failed: %d\n", session->cmd.sd); /* Only report interesting, infrequent errors (not the common timeout) */
#ifdef CONFIG_DEBUG_NET
if (session->cmd.sd != -ETIMEDOUT)
{
ndbg("ftpd_accept() failed: %d\n", session->cmd.sd);
}
#endif
ret = session->cmd.sd; ret = session->cmd.sd;
goto errout_with_session; goto errout_with_session;
} }

View File

@ -12,8 +12,8 @@
* the resolver code calls a callback function called resolv_found() * the resolver code calls a callback function called resolv_found()
* that must be implemented by the module that uses the resolver. * that must be implemented by the module that uses the resolver.
* *
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2009, 2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Based heavily on portions of uIP: * Based heavily on portions of uIP:
* *

View File

@ -234,7 +234,7 @@ int tftpget(const char *remote, const char *local, in_addr_t addr, bool binary)
/* Check if anything valid was received */ /* Check if anything valid was received */
if (nbytesrecvd >= 0) if (nbytesrecvd > 0)
{ {
/* Verify the sender address and port number */ /* Verify the sender address and port number */

View File

@ -1,8 +1,8 @@
/**************************************************************************** /****************************************************************************
* netuils/tftp/tftpc_packets.c * netuils/tftp/tftpc_packets.c
* *
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions

View File

@ -217,9 +217,17 @@ static int tftp_rcvack(int sd, uint8_t *packet, struct sockaddr_in *server,
{ {
/* Failed to receive a good packet */ /* Failed to receive a good packet */
if (nbytes >= 0) if (nbytes == 0)
{ {
ndbg("tftp_recvfrom short packet: %d bytes\n", nbytes); ndbg("Connection lost: %d bytes\n", nbytes);
}
else if (nbytes > 0)
{
ndbg("Short packet: %d bytes\n", nbytes);
}
else
{
ndbg("Recveid failure\n");
} }
/* Break out to bump up the retry count */ /* Break out to bump up the retry count */

View File

@ -2,8 +2,8 @@
* netutils/webclient/webclient.c * netutils/webclient/webclient.c
* Implementation of the HTTP client. * Implementation of the HTTP client.
* *
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2007, 2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Based on uIP which also has a BSD style license: * Based on uIP which also has a BSD style license:
* *
@ -526,6 +526,7 @@ int wget(FAR const char *url, FAR char *buffer, int buflen,
} }
else if (ret == 0) else if (ret == 0)
{ {
nvdbg("Connection lost\n");
close(sockfd); close(sockfd);
break; break;
} }

View File

@ -2,8 +2,8 @@
* netutils/webserver/httpd.c * netutils/webserver/httpd.c
* httpd Web server * httpd Web server
* *
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* This is a leverage of similar logic from uIP: * This is a leverage of similar logic from uIP:
* *
@ -380,6 +380,11 @@ static inline int httpd_cmd(struct httpd_state *pstate)
ndbg("[%d] recv failed: %d\n", pstate->ht_sockfd, errno); ndbg("[%d] recv failed: %d\n", pstate->ht_sockfd, errno);
return ERROR; return ERROR;
} }
lese if (recvlen == 0)
{
ndbg("[%d] connection lost\n", pstate->ht_sockfd);
return ERROR;
}
httpd_dumpbuffer("Incoming buffer", pstate->ht_buffer, recvlen); httpd_dumpbuffer("Incoming buffer", pstate->ht_buffer, recvlen);
/* We will handle only GET */ /* We will handle only GET */