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
|
/* 01.c */
#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 ============================================================== */
#define T_HASH 128
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
typedef struct
{
int sockfd;
char *hashm;
}
download_t;
/* private data ======================================================== */
/* private functions =================================================== */
static void error (char const *msg, int a)
{
printf ("ERR : %d : %s\n", a, msg);
}
static void *getmetadata_process (void)
{
return 0;
}
static int server (void)
{
SOCKET sockfd = 0;
SOCKET newsockfd;
SOCKADDR_IN cli_addr = { 0 };
int clilen = sizeof cli_addr;
char bcm[256] = "";
while ((newsockfd =
accept (sockfd, (struct sockaddr *) &cli_addr, &clilen)) > 0)
{
pthread_t getmetadata;
download_t *download = (download_t *) malloc (sizeof (download_t));
download->sockfd = newsockfd;
download->hashm = (char *) malloc (T_HASH + 1);
memcpy (download->hashm, (char *) (bcm + 2), (T_HASH + 1));
{
int thread_err =
pthread_create (&getmetadata, NULL, getmetadata_process,
(void *) download);
if (thread_err != 0)
{
error ("Impossible de créer le thread connection\n", 0);
closesocket (newsockfd);
free (download->hashm);
free (download);
}
else
{
printf ("passif_process:une nouvelle connexion a été crée \n");
}
}
}
return 0;
}
/* 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;
#endif
server ();
#if defined (WIN32) || defined (_WIN32)
WSACleanup ();
}
#endif
if (err)
{
ret = EXIT_FAILURE;
}
else
{
ret = EXIT_SUCCESS;
}
return ret;
} |
Partager