1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/* 05s.c */
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
#if defined (WIN32)
#include <winsock2.h>
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> /* 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 <stdio.h>
#include <stdlib.h>
/* macros ============================================================== */
#define TELNET 23
/* 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));
{
/* 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 ... */
}
else
{
perror ("socket.recv");
err = 1;
}
}
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;
} |
Partager