/* 06s.c */ #ifdef __cplusplus #error Be sure you are using a C compiler... #endif #if defined (WIN32) #include #elif defined (linux) #include #include #include #include #include /* close */ #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close (s) typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; #else #error not defined for this platform #endif #include #include /* macros ============================================================== */ #define TELNET 23 #define ESC 27 /* constants =========================================================== */ /* types =============================================================== */ /* structures ========================================================== */ /* private data ======================================================== */ /* private functions =================================================== */ /* --------------------------------------------------------------------- --------------------------------------------------------------------- */ static int app (void) { int err = 0; /* open a socket in TCP/IP mode. */ SOCKET sock = socket (AF_INET, SOCK_STREAM, 0); if (sock != INVALID_SOCKET) { printf ("socket %d is now opened in TCP/IP mode\n", sock); /* we want to listen on the TELNET port */ { int sock_err; /* assign the listening port */ SOCKADDR_IN sin = {0}; /* automatic IP address */ sin.sin_addr.s_addr = htonl (INADDR_ANY); /* protocol family (IP) */ sin.sin_family = AF_INET; /* listening port */ sin.sin_port = htons (TELNET); /* bind */ sock_err = bind (sock, (SOCKADDR *) & sin, sizeof sin); if (sock_err != SOCKET_ERROR) { /* start listening (server mode) */ sock_err = listen (sock, 5); printf ("listening on port %d...\n", TELNET); if (sock_err != SOCKET_ERROR) { /* wait for a client connection */ printf ("waiting for a client connection on port %d...\n", TELNET); { SOCKADDR_IN csin = {0}; int recsize = (int) sizeof csin; SOCKET csock = accept (sock, (SOCKADDR *) & csin, &recsize); if (csock != INVALID_SOCKET) { printf ("client connected with socket %d from %s:%d\n" ,csock ,inet_ntoa (csin.sin_addr) ,htons (csin.sin_port)); { int end = 0; do { /* wait for the receive of a data block (string expected, hence + 1) */ unsigned char data[128 + 1]; sock_err = recv (csock, data, (int) sizeof data - 1, 0); if (sock_err != SOCKET_ERROR) { size_t nb_rec = (size_t) sock_err; /* convert to string */ data[nb_rec] = 0; printf ("%u byte%s received:\n%s\n" ,(unsigned) nb_rec ,nb_rec > 1 ? "s" : "" ,data); fflush (stdout); /* to be continued ... */ if (data[0] == ESC) { end = 1; } } else { perror ("socket.recv"); err = 1; end = 1; } } while (!end); } shutdown (csock, 2); printf ("closing client socket %d...\n", csock); closesocket (csock), csock = INVALID_SOCKET; } else { perror ("socket.accept"); err = 1; } } } else { perror ("socket.listen"); err = 1; } } else { perror ("socket.bind"); err = 1; } printf ("closing socket %d...\n", sock); /* close the socket. */ sock_err = closesocket (sock), sock = INVALID_SOCKET; printf ("the socket is now closed\n"); if (sock_err) { perror ("socket.close"); err = 1; } } } else { perror ("socket.open"); err = 1; } return err; } /* entry point ========================================================= */ /* --------------------------------------------------------------------- --------------------------------------------------------------------- */ int main (void) { int ret; #if defined (WIN32) WSADATA wsa_data; int err = WSAStartup (MAKEWORD (2, 2), &wsa_data); if (!err) { puts ("WIN: winsock2: OK"); #else int err; #endif err = app (); #if defined (WIN32) WSACleanup (); } #endif if (err) { ret = EXIT_FAILURE; } else { ret = EXIT_SUCCESS; } return ret; }