From: Luke Kenneth Casson Leighton Date: Thu, 8 Apr 2021 12:41:40 +0000 (+0100) Subject: add totally unsophisticated socket listen/accept function X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=44b5736a099a551fcc15b7f9c45c30478cb7c3b7;p=soc-cxxrtl-sim.git add totally unsophisticated socket listen/accept function --- diff --git a/small_jtag_test/main.cpp b/small_jtag_test/main.cpp index 63c9cf8..73dd45b 100644 --- a/small_jtag_test/main.cpp +++ b/small_jtag_test/main.cpp @@ -1,3 +1,10 @@ +#include +#include +#include +#include +#include +#include +#include #include #include "add.cpp" @@ -5,11 +12,6 @@ using namespace std; extern "C" { -#include -#include -#include -#include -#include /* reads from a socket if it is ready (a single byte) and returns 1 if success */ @@ -38,13 +40,32 @@ int read_handler(int fdread, char *buffer) } } -/* -static void event_handler(int fd, short event, void *arg) +/* nothing sophisticated: wait for an incoming connection + */ +int get_connection() { - if (event & EV_READ) - read_handler(fd, event, arg); + int listenfd = 0, connfd = 0; + struct sockaddr_in serv_addr; + + listenfd = socket(AF_INET, SOCK_STREAM, 0); + memset(&serv_addr, '0', sizeof(serv_addr)); + + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + serv_addr.sin_port = htons(5000); + + bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); + + listen(listenfd, 1); + + connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); + close(listenfd); + + return connfd; } +/* + static void accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *ctx) { struct session_s *s = (struct session_s*)ctx;