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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
#if defined (WIN32) || defined (_WIN32)
#include <winsock2.h>
#elif defined (linux) || defined (_POSIX_VERSION) || defined (_POSIX2_C_VERSION)\
|| defined (_XOPEN_VERSION)
#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>
#include <pthread.h>
/* macros ============================================================== */
/* we want to listen to the 5000 port */
#define PORT 5000
#define ESC 27
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
struct cli
{
pthread_t thread;
SOCKADDR_IN sin;
int recsize;
SOCKET sock;
int err;
};
/* private data ======================================================== */
/* private functions =================================================== */
/* thread client function */
static void *client (void *p_data)
{
struct cli *p_cli = p_data;
if (p_cli != NULL)
{
int end = 0;
do
{
/* wait for the receive of a data block */
unsigned char data[128];
int sock_err = recv (p_cli->sock, data, (sizeof data - 1), 0);
if (sock_err != SOCKET_ERROR)
{
size_t nb_rec = sock_err;
if (nb_rec > 0)
{
/* 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);
if (data[0] == ESC)
{
end = 1;
}
else
{
/* send some YES-TO-ALL answer */
char const response[] = "OK\n";
send (p_cli->sock, response, strlen (response), 0);
}
}
else
{
puts ("client is disconnected");
end = 1;
}
}
else
{
perror ("socket.recv");
p_cli->err = 1;
end = 1;
}
}
while (!end);
shutdown (p_cli->sock, 2);
printf ("closing client socket %d...\n", p_cli->sock);
closesocket (p_cli->sock), p_cli->sock = INVALID_SOCKET;
/* the memory is now under the control of the thread */
free (p_cli), p_cli = NULL;
}
return NULL;
}
static int clients (SOCKET sock)
{
int err = 0;
int end = 0;
do
{
/* wait for a client connection */
printf ("waiting for a client connection on port %d...\n", PORT);
{
/* create a new client context */
struct cli *p_cli = malloc (sizeof *p_cli);
if (p_cli != NULL)
{
p_cli->recsize = (int) sizeof p_cli->sin;
p_cli->sock =
accept (sock, (SOCKADDR *) &p_cli->sin, &p_cli->recsize);
if (p_cli->sock != INVALID_SOCKET)
{
printf
("client connected with socket %d from %s:%d\n",
p_cli->sock, inet_ntoa (p_cli->sin.sin_addr),
htons (p_cli->sin.sin_port));
/* send ...*/
pthread_create (&p_cli->thread, NULL, client, p_cli);
/* ... and forget */
p_cli = NULL;
}
else
{
perror ("socket.accept");
err = 1;
}
}
else
{
fprintf (stderr, "client creation failed : memory error\n");
}
}
}
while (!end);
return err;
}
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
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 (PORT);
/* 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", PORT);
if (sock_err != SOCKET_ERROR)
{
err = clients (sock);
}
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) || defined (_WIN32)
WSADATA wsa_data;
int err = WSAStartup (MAKEWORD (2, 2), &wsa_data);
if (!err)
{
puts ("WIN: winsock2: OK");
#else
int err = 0;
{
#endif
err = app ();
#if defined (WIN32) || defined (_WIN32)
WSACleanup ();
#else
}
#endif
if (err)
{
ret = EXIT_FAILURE;
}
else
{
ret = EXIT_SUCCESS;
}
system ("pause");
return ret;
} |
Partager