diff --git a/README.md b/README.md index 6523bec..bb26b6e 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ -fimuni-c-cpp-examples -===================== +FI MUNI - C and C++ examples +============================ Examples related to talks at FI MUNI. +Georgik's blog: http://georgik.sinusgear.com Allegro ------- +Allegro is library for interactive applications like games written in C. + Three examples for Allegro 5.0 library. - example-01 - display smiley - example-02 - display smiley when pressed space @@ -25,6 +28,9 @@ How to run: GTK+ ---- +GTK+ is library for GUI applications written in C. +It has also bindings for many other languages. + Helloworld example from http://en.wikipedia.org/wiki/Gtk. Requires: GTK+-3 Package for Debian: libgtk-3-dev @@ -34,3 +40,27 @@ How to run: cd gtk make ./helloworld + +uv +-- + +libuv is library which has support for event loops, networking and many other +features required by cloud computing. This library is core part of Node.js. + +Requires: libuv (part of Node.js) +Package for Debian: none, you need to build it from sources. You can use Node.js package. + +How to run hello example: + + cd libuv/hello + make + ./hello + +How to run echo_server example: + + cd libuv/echo_server + make + ./echo_server + +Note: Do not forget to set LD_LIBRARY_PATH so the linker will be able to locate libuv + E.g.: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib diff --git a/libuv/echo_server/Makefile b/libuv/echo_server/Makefile new file mode 100644 index 0000000..db80ae9 --- /dev/null +++ b/libuv/echo_server/Makefile @@ -0,0 +1,13 @@ +PACKAGE = echo_server +OBJECTS = echo_server.o +CFLAGS = -I/usr/local/include/uv +LDADD = -luv + +hello: $(OBJECTS) + ${CC} -o ${PACKAGE} ${OBJECTS} ${LDADD} + +.c.o: + ${CC} ${CFLAGS} -c $< + +clean: + rm -f hello *.o diff --git a/libuv/echo_server/echo_server.c b/libuv/echo_server/echo_server.c new file mode 100644 index 0000000..80bcb97 --- /dev/null +++ b/libuv/echo_server/echo_server.c @@ -0,0 +1,59 @@ +#include +#include + +uv_loop_t *loop; + +uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) { + return uv_buf_init((char*) malloc(suggested_size), suggested_size); +} + +static void close_cb(uv_handle_t* handle) { + printf("Connection closed\n"); +} + +void echo_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) { + uv_err_t err = uv_last_error(uv_default_loop()); + + if (nread > 0) { + printf("%.*s", nread, buf.base); + } else if (nread < 0) { + if (err.code == UV_EOF) { + uv_close((uv_handle_t*)tcp, close_cb); + } + } +} + +void on_new_connection(uv_stream_t *server, int status) { + uv_tcp_t *client; + if (status == -1) { + // error! + return; + } + + client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t)); + uv_tcp_init(loop, client); + if (uv_accept(server, (uv_stream_t*) client) == 0) { + uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read); + } + else { + uv_close((uv_handle_t*) client, NULL); + } +} + +int main() { + + uv_tcp_t server; + int r; + struct sockaddr_in bind_addr; + loop = uv_default_loop(); + uv_tcp_init(loop, &server); + + bind_addr = uv_ip4_addr("0.0.0.0", 7000); + uv_tcp_bind(&server, bind_addr); + r = uv_listen((uv_stream_t*) &server, 128, on_new_connection); + if (r) { + fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop))); + return 1; + } + return uv_run(loop); +} \ No newline at end of file diff --git a/libuv/hello/Makefile b/libuv/hello/Makefile new file mode 100644 index 0000000..2cd3c26 --- /dev/null +++ b/libuv/hello/Makefile @@ -0,0 +1,13 @@ +PACKAGE = hello +OBJECTS = hello.o +CFLAGS = -I/usr/local/include/uv +LDADD = -luv + +hello: $(OBJECTS) + ${CC} -o ${PACKAGE} ${OBJECTS} ${LDADD} + +.c.o: + ${CC} ${CFLAGS} -c $< + +clean: + rm -f hello *.o diff --git a/libuv/hello/hello.c b/libuv/hello/hello.c new file mode 100644 index 0000000..721fa5b --- /dev/null +++ b/libuv/hello/hello.c @@ -0,0 +1,11 @@ +#include +#include + +int main() { + uv_loop_t *loop = uv_loop_new(); + + printf("Now quitting.\n"); + uv_run(loop); + + return 0; +}