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
|
#if defined (WIN32)
#include <winsock2.h>
typedef size_t socklen_t;
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#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;
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ed/inc/sys.h"
#define PORT 23
#define LINESIZE 128
#define VALUE_SOCKETPORT PORT
#define VALUE_SOCKETSERVER IP
#define CfgValue(a) a
#define AscstrToInt(a) a
enum
{
OK,
ERROR_EXIT_SOCKET,
ERROR_DEBUG,
ERROR_INFO,
dummy
};
static void ErrorPrintf (int err, char const *fmt, ...)
{
va_list va;
va_start (va, fmt);
printf ("err #%d: ", err);
vprintf (fmt, va);
printf ("\n");
va_end (va);
}
#define SocketRead(a,b,c) recv (a,b,c,0)
void SocketServer (int (*funcp) (void *, int))
{
/* integers for the socket descriptors */
int sock, i;
#if 0
/* declare socket (file) descriptors sets for the select function below */
fd_set active_fd_set, read_fd_set;
struct in_addr *addr;
#endif
/* structures that will contain sockets addresses information (server, and clients) */
struct sockaddr_in name, clientname;
socklen_t size;
static char buffer[LINESIZE];
int nbbytes;
int port = AscstrToInt (CfgValue (VALUE_SOCKETPORT));
/* Create the listening socket of the server and set it up to accept connections. */
name.sin_family = AF_INET;
name.sin_port = htons (port);
/* automatic IP address */
name.sin_addr.s_addr = htonl (INADDR_ANY);
/* Create the socket. */
sock = socket (PF_INET, SOCK_STREAM, 0);
if (sock < 0)
ErrorPrintf (ERROR_EXIT_SOCKET, "Cannot open socket: %s (%d)",
strerror (errno), errno);
/* Give the socket a name (bind) */
if (bind (sock, (struct sockaddr *) &name, sizeof name) < 0)
ErrorPrintf (ERROR_EXIT_SOCKET, "Cannot bind socket: %s (%d)",
strerror (errno), errno);
/* End of listening socket creation and binding */
if (listen (sock, 1) < 0)
ErrorPrintf (ERROR_EXIT_SOCKET, "Cannot listen to socket");
#if 0
/* -ed- cet algo ne peut pas fonctionner avec Windows.) */
/* Initialize the set of active sockets. */
/* active_fd_set is initialized to be the empty file descriptor set */
FD_ZERO (&active_fd_set);
/* adds "sock" to the socket descriptor set activ_fd_set */
FD_SET (sock, &active_fd_set);
while (1)
{
/* Block until input arrives on one or more active sockets. */
read_fd_set = active_fd_set;
/* returns the number of descriptor in which input arrives */
printf ("before select()\n");
if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
ErrorPrintf (ERROR_EXIT_SOCKET, "Cannot select socket");
/* Service all the sockets with input pending. */
for (i = 0; i < FD_SETSIZE; ++i)
{
if (!(FD_ISSET (i, &read_fd_set)))
/* descriptor i is not a pending connexion */
continue;
if (i == sock)
{
/* Connection request on original socket. */
int new;
size = sizeof (clientname);
new = accept (sock, (struct sockaddr *) &clientname, &size);
if (new < 0)
ErrorPrintf (ERROR_EXIT_SOCKET,
"Cannot accept new connection on soket");
ErrorPrintf (ERROR_INFO,
"Server: receive connect from host %s, port %hd.\n",
inet_ntoa (clientname.sin_addr),
ntohs (clientname.sin_port));
/* adds "new" to the socket descriptor set activ_fd_set */
FD_SET (new, &active_fd_set);
}
else
{
/* Data arriving on an already-connected socket. */
nbbytes = SocketRead (i, buffer, LINESIZE);
/* Nothing is left to reveive on socket i, we close the connexion on i, then we delete from the socket descriptor set */
if (nbbytes <= 0)
{
closesocket (new);
FD_CLR (i, &active_fd_set);
continue;
}
/* to upper layer */
if (funcp)
{
funcp (buffer, nbbytes);
}
}
/* for */
}
/* while */
}
#else
/* faisons simple (mono-client, pour le moment) */
{
int new;
size = sizeof (clientname);
new = accept (sock, (struct sockaddr *) &clientname, &size);
if (new < 0)
ErrorPrintf (ERROR_EXIT_SOCKET,
"Cannot accept new connection on soket");
else
{
ErrorPrintf (ERROR_INFO,
"Server: receive connect from host %s, port %hd.\n",
inet_ntoa (clientname.sin_addr),
ntohs (clientname.sin_port));
int end = 0;
while (!end)
{
/* Data arriving on an already-connected socket. */
nbbytes = SocketRead (new, buffer, LINESIZE - 1);
/* Nothing is left to reveive on socket i, we close the connexion on i, then we delete from the socket descriptor set */
if (nbbytes <= 0)
{
closesocket (new);
end = 1;
}
else
{
/* to upper layer */
if (funcp)
{
funcp (buffer, nbbytes);
}
}
}
}
}
#endif
}
void data_process (void *p, int size)
{
if (p != NULL && size > 0)
{
char *s = p;
s[size] = 0;
printf ("< '%s'\n", s);
}
}
int main (void)
{
#if defined (WIN32)
WSADATA WSAData;
int erreur = WSAStartup (MAKEWORD (2, 0), &WSAData);
#else
int erreur = 0;
#endif
SocketServer (data_process);
#if defined (WIN32)
WSACleanup ();
#endif
system ("pause");
return 0;
} |
Partager