mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 16:50:55 +08:00
Improve send/close performance
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@410 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -56,9 +56,9 @@
|
||||
void send_client(void)
|
||||
{
|
||||
struct sockaddr_in myaddr;
|
||||
char outbuf[SENDSIZE];
|
||||
char *outbuf;
|
||||
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
|
||||
char inbuf[SENDSIZE];
|
||||
char *inbuf;
|
||||
#endif
|
||||
int sockfd;
|
||||
int nbytessent;
|
||||
@@ -69,13 +69,28 @@ void send_client(void)
|
||||
int ch;
|
||||
int i;
|
||||
|
||||
/* Allocate buffers */
|
||||
|
||||
outbuf = (char*)malloc(SENDSIZE);
|
||||
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
|
||||
inbuf = (char*)malloc(SENDSIZE);
|
||||
if (!outbuf || !inbuf)
|
||||
#else
|
||||
if (!outbuf)
|
||||
#endif
|
||||
{
|
||||
message("client: failed to allocate buffers\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
/* Create a new TCP socket */
|
||||
|
||||
sockfd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0)
|
||||
{
|
||||
message("client socket failure %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_buffers;
|
||||
}
|
||||
|
||||
/* Connect the socket to the server */
|
||||
@@ -92,7 +107,7 @@ void send_client(void)
|
||||
if (connect( sockfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
|
||||
{
|
||||
message("client: connect failure: %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
message("client: Connected\n");
|
||||
|
||||
@@ -117,14 +132,12 @@ void send_client(void)
|
||||
if (nbytessent < 0)
|
||||
{
|
||||
message("client: send failed: %d\n", errno);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
else if (nbytessent != 512)
|
||||
{
|
||||
message("client: Bad send length=%d: %d\n", nbytessent);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
}
|
||||
#else
|
||||
@@ -137,14 +150,12 @@ void send_client(void)
|
||||
if (nbytessent < 0)
|
||||
{
|
||||
message("client: send failed: %d\n", errno);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
else if (nbytessent != SENDSIZE)
|
||||
{
|
||||
message("client: Bad send length: %d Expected: %d\n", nbytessent, SENDSIZE);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
totalbytesrecvd = 0;
|
||||
@@ -156,8 +167,7 @@ void send_client(void)
|
||||
if (nbytesrecvd < 0)
|
||||
{
|
||||
message("client: recv failed: %d\n", errno);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
totalbytesrecvd += nbytesrecvd;
|
||||
message("client: Received %d of %d bytes\n", totalbytesrecvd, SENDSIZE);
|
||||
@@ -167,16 +177,29 @@ void send_client(void)
|
||||
if (totalbytesrecvd != SENDSIZE)
|
||||
{
|
||||
message("client: Bad recv length: %d Expected: %d\n", totalbytesrecvd, SENDSIZE);
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
else if (memcmp(inbuf, outbuf, SENDSIZE) != 0)
|
||||
{
|
||||
message("client: Received buffer does not match sent buffer\n");
|
||||
close(sockfd);
|
||||
exit(-1);
|
||||
goto errout_with_socket;
|
||||
}
|
||||
|
||||
close(sockfd);
|
||||
free(outbuf);
|
||||
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
|
||||
free(inbuf);
|
||||
#endif
|
||||
return;
|
||||
#endif
|
||||
|
||||
errout_with_socket:
|
||||
close(sockfd);
|
||||
|
||||
errout_with_buffers:
|
||||
free(outbuf);
|
||||
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
|
||||
free(inbuf);
|
||||
#endif
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ void recv_server(void)
|
||||
#ifdef NETTEST_HAVE_SOLINGER
|
||||
struct linger ling;
|
||||
#endif
|
||||
char buffer[1024];
|
||||
char *buffer;
|
||||
int listensd;
|
||||
int acceptsd;
|
||||
socklen_t addrlen;
|
||||
@@ -71,13 +71,23 @@ void recv_server(void)
|
||||
#endif
|
||||
int optval;
|
||||
|
||||
/* Allocate a BIG buffer */
|
||||
|
||||
buffer = (char*)malloc(2*SENDSIZE);
|
||||
if (!buffer)
|
||||
{
|
||||
message("server: failed to allocate buffer\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
/* Create a new TCP socket */
|
||||
|
||||
listensd = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (listensd < 0)
|
||||
{
|
||||
message("server: socket failure: %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_buffer;
|
||||
}
|
||||
|
||||
/* Set socket to reuse address */
|
||||
@@ -86,7 +96,7 @@ void recv_server(void)
|
||||
if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
||||
{
|
||||
message("server: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_listensd;
|
||||
}
|
||||
|
||||
/* Bind the socket to a local address */
|
||||
@@ -98,7 +108,7 @@ void recv_server(void)
|
||||
if (bind(listensd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
|
||||
{
|
||||
message("server: bind failure: %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_listensd;
|
||||
}
|
||||
|
||||
/* Listen for connections on the bound TCP socket */
|
||||
@@ -106,7 +116,7 @@ void recv_server(void)
|
||||
if (listen(listensd, 5) < 0)
|
||||
{
|
||||
message("server: listen failure %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_listensd;
|
||||
}
|
||||
|
||||
/* Accept only one connection */
|
||||
@@ -117,7 +127,7 @@ void recv_server(void)
|
||||
if (acceptsd < 0)
|
||||
{
|
||||
message("server: accept failure: %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_listensd;
|
||||
}
|
||||
message("server: Connection accepted -- receiving\n");
|
||||
|
||||
@@ -129,7 +139,7 @@ void recv_server(void)
|
||||
if (setsockopt(acceptsd, SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger)) < 0)
|
||||
{
|
||||
message("server: setsockopt SO_LINGER failure: %d\n", errno);
|
||||
exit(1);
|
||||
goto errout_with_acceptsd;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -138,13 +148,11 @@ void recv_server(void)
|
||||
|
||||
for (;;)
|
||||
{
|
||||
nbytesread = recv(acceptsd, buffer, 1024, 0);
|
||||
nbytesread = recv(acceptsd, buffer, 2*SENDSIZE, 0);
|
||||
if (nbytesread <= 0)
|
||||
{
|
||||
message("server: recv failed: %d\n", errno);
|
||||
close(listensd);
|
||||
close(acceptsd);
|
||||
exit(-1);
|
||||
goto errout_with_acceptsd;
|
||||
}
|
||||
}
|
||||
#else
|
||||
@@ -154,13 +162,11 @@ void recv_server(void)
|
||||
while (totalbytesread < SENDSIZE)
|
||||
{
|
||||
message("server: Reading...\n");
|
||||
nbytesread = recv(acceptsd, &buffer[totalbytesread], 1024 - totalbytesread, 0);
|
||||
nbytesread = recv(acceptsd, &buffer[totalbytesread], 2*SENDSIZE - totalbytesread, 0);
|
||||
if (nbytesread <= 0)
|
||||
{
|
||||
message("server: recv failed: %d\n", errno);
|
||||
close(listensd);
|
||||
close(acceptsd);
|
||||
exit(-1);
|
||||
goto errout_with_acceptsd;
|
||||
}
|
||||
|
||||
totalbytesread += nbytesread;
|
||||
@@ -172,9 +178,7 @@ void recv_server(void)
|
||||
if (totalbytesread != SENDSIZE)
|
||||
{
|
||||
message("server: Received %d / Expected %d bytes\n", totalbytesread, SENDSIZE);
|
||||
close(listensd);
|
||||
close(acceptsd);
|
||||
exit(-1);
|
||||
goto errout_with_acceptsd;
|
||||
}
|
||||
|
||||
ch = 0x20;
|
||||
@@ -183,9 +187,7 @@ void recv_server(void)
|
||||
if (buffer[i] != ch)
|
||||
{
|
||||
message("server: Byte %d is %02x / Expected %02x\n", i, buffer[i], ch);
|
||||
close(listensd);
|
||||
close(acceptsd);
|
||||
exit(-1);
|
||||
goto errout_with_acceptsd;
|
||||
}
|
||||
|
||||
if (++ch > 0x7e)
|
||||
@@ -201,9 +203,7 @@ void recv_server(void)
|
||||
if (nbytessent <= 0)
|
||||
{
|
||||
message("server: send failed: %d\n", errno);
|
||||
close(listensd);
|
||||
close(acceptsd);
|
||||
exit(-1);
|
||||
goto errout_with_acceptsd;
|
||||
}
|
||||
message("server: Sent %d bytes\n", nbytessent);
|
||||
|
||||
@@ -218,5 +218,17 @@ void recv_server(void)
|
||||
|
||||
close(listensd);
|
||||
close(acceptsd);
|
||||
free(buffer);
|
||||
return;
|
||||
#endif
|
||||
|
||||
errout_with_acceptsd:
|
||||
close(acceptsd);
|
||||
|
||||
errout_with_listensd:
|
||||
close(listensd);
|
||||
|
||||
errout_with_buffer:
|
||||
free(buffer);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@
|
||||
#endif
|
||||
|
||||
#define PORTNO 5471
|
||||
#define SENDSIZE 512
|
||||
#define SENDSIZE 4096
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
|
||||
Reference in New Issue
Block a user