aboutsummaryrefslogtreecommitdiffstats
path: root/example/server.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-08-22 15:22:41 +0000
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-08-22 15:22:41 +0000
commit494fbd91e4e5574a4cf8dbe69b3f80a08e97e85b (patch)
treebdcd9d84aecda89d3af602c8ffccb9ffa5518561 /example/server.c
parent7f53c3f7484679c1e38607f59542d43be1387650 (diff)
example client
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@958 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'example/server.c')
-rw-r--r--example/server.c30
1 files changed, 6 insertions, 24 deletions
diff --git a/example/server.c b/example/server.c
index a671f4cb..04f88f0f 100644
--- a/example/server.c
+++ b/example/server.c
@@ -21,28 +21,7 @@
#include <pb_decode.h>
#include "fileproto.h"
-
-bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
-{
- int fd = *(int*)stream->state;
- return send(fd, buf, count, 0) == count;
-}
-
-bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
-{
- int fd = *(int*)stream->state;
-
- if (buf == NULL)
- {
- /* Well, this is a really inefficient way to skip input. */
- /* It is only used when there are unknown fields. */
- char dummy;
- while (count-- && recv(fd, &dummy, 1, 0) == 1);
- return count == 0;
- }
-
- return recv(fd, buf, count, MSG_WAITALL) == count;
-}
+#include "common.h"
bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
{
@@ -70,8 +49,8 @@ void handle_connection(int connfd)
{
ListFilesRequest request;
ListFilesResponse response;
- pb_istream_t input = {&read_callback, &connfd, SIZE_MAX};
- pb_ostream_t output = {&write_callback, &connfd, SIZE_MAX, 0};
+ pb_istream_t input = pb_istream_from_socket(connfd);
+ pb_ostream_t output = pb_ostream_from_socket(connfd);
DIR *directory;
if (!pb_decode(&input, ListFilesRequest_fields, &request))
@@ -109,9 +88,12 @@ int main(int argc, char **argv)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
+ int reuse = 1;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
+ setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
+
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);