Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/unixasm/fndsockserver.c
Views: 11766
/*1* fndsockserver.c2* Copyright 2006 Ramon de Carvalho Valle <[email protected]>3*4* This program is free software; you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation; either version 2 of the License, or7* (at your option) any later version.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*14* You should have received a copy of the GNU General Public License15* along with this program; if not, write to the Free Software16* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA17*18*/1920#include <stdio.h>21#include <stdlib.h>22#include <string.h>23#include <sys/types.h>24#include <sys/socket.h>25#include <arpa/inet.h>26#include <netdb.h>27#include <sys/select.h>28#include <unistd.h>29#include <errno.h>3031#define BACKLOG 53233int34hexdump(char *buf, int len)35{36int i, j;3738for (i=0; i<len; i++) {39for (j=0; j<16; j++) {40if (i+j >= len)41printf("%3s","");42else43printf("%02x ", (unsigned char)buf[i+j]);44}4546printf("%3s","");4748for (j=0; j<16; j++) {49if (i+j >= len)50printf("%1s","");51else52if (buf[i+j]>'\x1f' && buf[i+j]<'\x7f')53printf("%c", buf[i+j]);54else55printf(".");56}5758i += 15;5960printf("\n");61}6263return 0;64}6566int67main(int argc, char **argv)68{69char *addr = "0.0.0.0";70int port = 1234;71int c, s;72int debug = 0, verbose = 0;73struct sockaddr_in sin;74struct hostent *he;7576while ((c = getopt(argc, argv, "a:dp:v")) != -1) {77switch (c) {78case 'a':79addr = optarg;80break;81case 'd':82debug = 1;83break;84case 'p':85port = atoi(optarg);86break;87case 'v':88verbose = 1;89}90}9192if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {93perror("socket");94exit(EXIT_FAILURE);95}9697memset(&sin, 0, sizeof(sin));98sin.sin_family = AF_INET;99sin.sin_port = htons(port);100if ((sin.sin_addr.s_addr = inet_addr(addr)) == -1) {101if ((he = gethostbyname(addr)) == NULL) {102errno = EADDRNOTAVAIL;103perror("gethostbyname");104exit(EXIT_FAILURE);105}106memcpy(&sin.sin_addr.s_addr, he->h_addr, 4);107}108109if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {110perror("bind");111exit(EXIT_FAILURE);112}113114if (listen(s, BACKLOG) == -1) {115perror("listen");116exit(EXIT_FAILURE);117}118119if (debug || verbose)120printf("listening on %s:%d\n", addr, port);121122while (1) {123int tmp;124struct sockaddr_in sin;125socklen_t sin_len = sizeof(sin);126127if((tmp = accept(s, (struct sockaddr *)&sin, &sin_len)) == -1) {128perror("accept");129exit(EXIT_FAILURE);130}131132if (debug || verbose)133printf("accepted connection from %s:%d\n", inet_ntoa(sin.sin_addr),134ntohs(sin.sin_port));135136if (!fork()) {137int count;138char buf[1024];139140count = recv(tmp, buf, sizeof(buf), 0);141142if (debug)143hexdump(buf, count);144145if (debug || verbose)146printf("%d bytes received\n", count);147148sleep(2);149150#if defined(_AIX) || (defined(__linux__) && defined(__powerpc64__))151{152/* fake function descriptor */153unsigned long fdesc[2] = {(unsigned long)buf, 0};154(*(void (*)())fdesc)();155}156#else157(*(void (*)())buf)();158#endif159160exit(EXIT_SUCCESS);161}162}163164exit(EXIT_SUCCESS);165}166167168169