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
|
#include "stdafx.h"
#include <winsock2.h>
typedef int socklen_t;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 23
#define LongMax 200
#define ClientMax 4
#include <pthread.h>
int erreur ;
int n°client ;
int i = 0 ;
SOCKET sock ;
void* sk = &sock ;
SOCKADDR_IN sin ;
SOCKET csock ;
SOCKADDR_IN csin ;
char bufferE[4096] ;
char bufferR[4096] ;
int sock_err ;
socklen_t recsize = sizeof(csin) ;
pthread_t thread_accept ;
FILE* logo = NULL;
char ligne[LongMax];
struct client {
char pseudo[50];
SOCKET csock;
SOCKADDR_IN csin ;
pthread_t thread_client;
bool utilisation ; /*0= slot disponible, 1= slot indisponible*/
};
struct client liste_client[ClientMax];
void lecture_Text(void)
{
logo = fopen("logo_Serveur.txt", "r") ;
if (logo != NULL)
{
while (fgets(ligne, LongMax, logo) != NULL)
{
printf("%s\r", ligne) ;
}
fclose(logo);
}
else (printf("erreur \n")) ;
printf("\n") ;
}
void emission(void)
{
do
{
gets(bufferE);
sock_err = send(liste_client[i].csock, bufferE, 4096, 0);
if(sock_err != SOCKET_ERROR)
printf("%s\n", bufferE);
else
{
printf("Erreur de transmission\n") ;
liste_client[i].utilisation = 1 ;
}
}
while (strcmp(bufferE, "/quit") != 0);
}
void* reception(void* sk)
{
do {
if(recv(csock, bufferR, 4096, 0) != SOCKET_ERROR)
printf("Recu : %s\n", bufferR);
else printf("Erreur dans la reception\n");
}
while (strcmp(bufferE, "/quit") != 0);
return NULL;
}
void initialisation(void)
{
system("color 0F") ;
lecture_Text();
printf(" Bienvenue dans le chat Felix version 3.0 \n");
printf(" Veuillez taper sur une touche \n");
getchar();
system("cls");
WSADATA WSAData;
erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
for (n°client = 0; n°client <= ClientMax; n°client++)
{
liste_client[n°client].utilisation = 0;
}
}
int slot_libre(void)
{
n°client = 0 ;
while (liste_client[n°client].utilisation = 1)
{
n°client++;
}
return n°client;
}
void* scan(void*)
{
liste_client[slot_libre()].csock = accept(sock, (SOCKADDR*)&csin, &recsize);
if (liste_client[slot_libre()].csock != INVALID_SOCKET)
{
liste_client[slot_libre()].utilisation = 1;
printf("%d", liste_client[slot_libre()].csock);
}
else
{
printf("Erreur dans l'acceptation");
}
return NULL;
}
int main(void)
{
initialisation();
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock != INVALID_SOCKET)
{
printf("La socket %d est maintenant ouverte en mode TCP/IP\n", sock);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sock_err = bind(sock, (SOCKADDR*)&sin, sizeof(sin));
if(sock_err != SOCKET_ERROR)
{
sock_err = listen(sock, 5);
printf("Mise en ecoute de la socket sur le port %d...\n", PORT);
if(sock_err != SOCKET_ERROR)
{
printf("Patientez pendant que les clients se connectent sur le port %d...\n", PORT);
pthread_create(&thread_accept, NULL, scan, NULL);
getchar();
/*pthread_create(&liste_client[0].thread_client, NULL, reception, sk); parametre= socket serveur
emission(); /*parametre= socket client*/
/*if (pthread_cancel(liste_client[0]thread_client)== 0)
{
printf("Conversation terminée") ;
}
else printf("Erreur du thread") ;*/
shutdown(sock, 2);
}
}
printf("Fermeture de la socket...\n");
closesocket(sock);
printf("Fermeture du serveur terminee\n");
}
WSACleanup();
getchar();
return EXIT_SUCCESS;
} |
Partager