/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: test-sock.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file test-sock.c The test-sock module. */ #line 10 "test-sock.ctr" #define TEST_SOCK_USE_APP 1 #define TEST_SOCK_REMOTE_SERVER "s-ka.e-technik.fh-schmalkalden.de" #define TEST_SOCK_REMOTE_PORT 80 #define TEST_SOCK_LOCAL_PORT 55555 #define TEST_SOCK_TIMEOUT_SEC 0L #define TEST_SOCK_TIMEOUT_USEC 0L #include "dk4conf.h" #include "dk4app.h" #include "dk4sock.h" #include "dk4socka.h" #include "dk4vers.h" #include "dk4fput.h" #line 28 "test-sock.ctr" static const dkChar server[] = { dkT("s-ka.e-technik.fh-schmalkalden.de") }; static const char request[] = { "GET /index.html HTTP/1.0\r\n\r\n" }; static const dkChar * const help_text[] = { /* 0 */ dkT("Nix Hilfe!"), NULL #line 42 "test-sock.ctr" }; static const dkChar * const license_text[] = { /* 0 */ dkT("BSD"), NULL #line 48 "test-sock.ctr" }; static const dkChar * const kw_def[] = { /* 0 */ dkT("Failed to create socket!"), /* 1 */ dkT("Failed to bring Winsock up!"), /* 2 */ dkT("Failed to send data!"), NULL #line 65 "test-sock.ctr" }; static size_t sz_kw_def = sizeof(kw_def)/sizeof(DK4_PCDKCHAR); /** Main function. @param argc Number of command line arguments. @param argv Command line arguments array. @return 0 on success, all other values indicate errors. */ #if DK4_CHAR_SIZE > 1 int wmain(int argc, wchar_t *argv[]) #else int main(int argc, char *argv[]) #endif { #if TEST_SOCK_USE_APP char buf[512]; dk4_app_t *app = NULL; size_t rql; dk4_socket_t sock; #if DK4_ON_WINDOWS int bytes; #else ssize_t bytes; #endif #line 91 "test-sock.ctr" #line 92 "test-sock.ctr" dk4fput_initialize_stderr(); app = dk4app_open_cmd( argc, argv, NULL, 0, dkT("dktools"), DKT_VERSION_DK, dkT("test-sock.txt"), help_text, license_text ); if (NULL != app) { if (DK4_SOCKET_RESULT_SUCCESS == dk4socket_up_app(app)) { #line 99 "test-sock.ctr" rql = strlen(request); sock = dk4socket_tcp_client_for_host_port_app( server, TEST_SOCK_REMOTE_PORT, TEST_SOCK_LOCAL_PORT, TEST_SOCK_TIMEOUT_SEC, TEST_SOCK_TIMEOUT_USEC, app ); if (INVALID_SOCKET != sock) { #line 105 "test-sock.ctr" bytes = send(sock, request, rql, 0); if (bytes == rql) { #line 107 "test-sock.ctr" do { bytes = recv(sock, buf, sizeof(buf), 0); if (0 < bytes) { fwrite(buf, 1, bytes, stdout); } } while(0 < bytes); fputc('\n', stdout); } else { #line 115 "test-sock.ctr" dk4app_log_1(app, kw_def, sz_kw_def, DK4_LL_ERROR, 2); } dk4socket_close(sock, NULL); } else { #line 119 "test-sock.ctr" dk4app_log_1(app, kw_def, sz_kw_def, DK4_LL_ERROR, 0); } dk4socket_down_app(app); } else { #line 123 "test-sock.ctr" dk4app_log_1(app, kw_def, sz_kw_def, DK4_LL_ERROR, 1); } dk4app_close(app); } fflush(stderr); dk4fput_cleanup_stderr(); #line 130 "test-sock.ctr" #line 131 "test-sock.ctr" exit(0); return 0; #else char buf[512]; size_t rql; dk4_socket_t sock; #if DK4_ON_WINDOWS int bytes; #else ssize_t bytes; #endif #line 142 "test-sock.ctr" #line 143 "test-sock.ctr" rql = strlen(request); if (DK4_SOCKET_RESULT_SUCCESS == dk4socket_up(NULL)) { #line 145 "test-sock.ctr" sock = dk4socket_tcp_client_host_port( server, TEST_SOCK_REMOTE_PORT, TEST_SOCK_LOCAL_PORT, TEST_SOCK_TIMEOUT_SEC, TEST_SOCK_TIMEOUT_USEC, NULL ); if (INVALID_SOCKET != sock) { #line 150 "test-sock.ctr" bytes = send(sock, request, rql, 0); if (bytes == rql) { #line 152 "test-sock.ctr" do { bytes = recv(sock, buf, sizeof(buf), 0); if (0 < bytes) { fwrite(buf, 1, bytes, stdout); } } while(0 < bytes); fputc('\n', stdout); } else { #line 160 "test-sock.ctr" } dk4socket_close(sock, NULL); } else { #line 163 "test-sock.ctr" } dk4socket_down(NULL); } else { #line 166 "test-sock.ctr" } #line 168 "test-sock.ctr" #line 169 "test-sock.ctr" exit(0); return 0; #endif }