Skip to content

Commit

Permalink
Step 3 added to the tutorial
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
  • Loading branch information
sustrik committed Jul 24, 2015
1 parent 59f056a commit 966c951
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ examples/greetserver
tutorial/.dirstamp
tutorial/step1
tutorial/step2
tutorial/step3
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ noinst_PROGRAMS += \

noinst_PROGRAMS += \
tutorial/step1 \
tutorial/step2
tutorial/step2 \
tutorial/step3

################################################################################
# additional packaging-related stuff #
Expand Down
3 changes: 0 additions & 3 deletions tutorial/step2.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,3 @@ int main(int argc, char *argv[]) {
}
}




75 changes: 75 additions & 0 deletions tutorial/step3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright (c) 2015 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

#include "../libmill.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

int port = 5555;
if(argc > 1)
port = atoi(argv[1]);

tcpsock ls = tcplisten(NULL, port);
if(!ls) {
perror("Can't open listening socket");
return 1;
}

while(1) {
tcpsock as = tcpaccept(ls, -1);
if(!as)
continue;

tcpsend(as, "What's your name?\r\n", 19, -1);
if(errno != 0)
goto cleanup;
tcpflush(as, -1);
if(errno != 0)
goto cleanup;

char inbuf[256];
size_t sz = tcprecvuntil(as, inbuf, sizeof(inbuf), '\r', -1);
if(errno != 0)
goto cleanup;

inbuf[sz - 1] = 0;
char outbuf[256];
int rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!\n", inbuf);

sz = tcpsend(as, outbuf, rc, -1);
if(errno != 0)
goto cleanup;
tcpflush(as, -1);
if(errno != 0)
goto cleanup;

cleanup:
tcpclose(as);
}
}

0 comments on commit 966c951

Please sign in to comment.