/* The MIT License (MIT) Copyright © 2020-2025 pacman64 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. */ /* You can build this command-line app by running cc -Wall -s -O2 -o ./tcpecho ./tcpecho.c */ #include #include #include #include #include #include #include #include #include #include #ifdef _WIN32 #include #endif // info is the message shown when this app is given any of its help options const char* info = "" "tcpecho [port] [words...]\n" "\n" "\n" "Start a TCP server which always replies with a plain-text line made from\n" "the arguments given after the port number, joined by single spaces.\n" "\n" "\n" "Options, all of which can start with either 1 or 2 dashes:\n" "\n" "\n" " -h show this help message\n" " -help show this help message\n" ""; // is_help_option simplifies control-flow for func main bool is_help_option(char* s) { return (s[0] == '-') && ( strcmp(s, "-h") == 0 || strcmp(s, "-help") == 0 || strcmp(s, "--h") == 0 || strcmp(s, "--help") == 0 ); } int main(int argc, char** argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); // ensure output lines end in LF instead of CRLF on windows setmode(fileno(stdout), O_BINARY); setmode(fileno(stderr), O_BINARY); #endif // handle any of the help options, if given if (argc > 1 && is_help_option(argv[1])) { puts(info); return 0; } if (argc < 2) { char* msg = "you forgot the port number as the leading argument"; fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); return 1; } char* end; int port = strtol(argv[1], &end, 10); if (*end != 0 || port < 1 || port > 65535) { char* msg; msg = "isn't a valid port number"; fprintf(stderr, "\x1b[31m%s %s\x1b[0m\n", argv[1], msg); msg = "maybe you forgot the port number as the leading argument"; fprintf(stderr, "\x1b[31m%s\x1b[0m\n", msg); return 1; } // count spaces between words size_t msglen = argc - 2; // count word lengths for (size_t i = 2; i < argc; i++) { msglen += strlen(argv[i]); } // remember the final line-feed msglen++; // also remember that strings are ended with an extra null byte char* msg = malloc(msglen + 1); if (msg == NULL) { const char* m = "can't get enough memory for the response message"; fprintf(stderr, "\x1b[31m%s\x1b[0m\n", m); return 1; } for (size_t i = 2, pos = 0; i < argc; i++) { if (i > 2) { msg[pos] = ' '; pos++; } for (size_t j = 0; argv[i][j] != 0; j++) { msg[pos] = argv[i][j]; pos++; } } msg[msglen - 1] = '\n'; msg[msglen] = 0; struct sockaddr_in srv; memset(&srv, 0, sizeof(srv)); int fd = socket(AF_INET, SOCK_STREAM, 0); srv.sin_family = AF_INET; srv.sin_addr.s_addr = htonl(INADDR_ANY); srv.sin_port = htons(port); bind(fd, (struct sockaddr*)&srv, sizeof(srv)); listen(fd, 1); while (true) { int conn = accept(fd, NULL, NULL); send(conn, msg, msglen, 0); close(conn); } free(msg); return 0; }