voici le code du serveur :
#include <stdio.h>
#include <stdlib.h>
#include "ircd.h"
#include <unistd.h>
int main(int ac, char **av)
{
struct sockaddr_in sock;
int fd_sock;
int size;
int err;
printf("%s%s\n", BONUS, BONUX);
WORD wVersionRequested;
WSADATA wsaData;
int err_ini;
wVersionRequested = MAKEWORD( 2, 2 );
err_ini = WSAStartup( wVersionRequested, &wsaData );
if (err_ini != 0)
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Aucune librairie Winsock trouvee.\n" );
return (-1);
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2)
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
printf("Aucune librairie Winsock trouvee.\n" );
return (-1);
}
if ((fd_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
err = WSAGetLastError();
printf("error : %d, %d\n", err, fd_sock);
}
sock.sin_family = AF_INET;
sock.sin_port = htons(atoi(av[1]));
sock.sin_addr.s_addr = INADDR_ANY;
size = sizeof (struct sockaddr_in);
if (bind(fd_sock, (struct sockaddr*) &sock, size) == -1)
{
err = WSAGetLastError();
printf("error bind : %d, %d\n", err, fd_sock);
}
if (listen(fd_sock, 5) == -1)
{
err = WSAGetLastError();
printf("error listen: %d, %d\n", err, fd_sock);
}
loop(fd_sock, size);
return (0);
}
void loop(int fd_sock, int size)
{
fd_set fd_read;
int max;
t_client *list;
t_client *tmp;
struct sockaddr_in client;
max = fd_sock + 1;
list = init(&fd_read, fd_sock);
while (1)
{
clean_struct(list, &fd_read, max);
tmp = list;
while (tmp)
{
if (tmp->fd != fd_sock)
treat_msg(&list, tmp, &fd_read);
else
if (FD_ISSET(fd_sock, &fd_read))
max = add_client(fd_sock, max, &client, &list);
tmp = tmp->next;
}
}
}
void clean_struct(t_client *list, fd_set *fd_read, int max)
{
t_client *tmp;
struct timeval time;
time.tv_sec = 0;
time.tv_usec = 0;
FD_ZERO(fd_read);
tmp = list;
while (tmp)
{
FD_SET(tmp->fd, fd_read);
tmp = tmp->next;
}
select(max, fd_read, 0, 0, &time);
}
t_client *init(fd_set *fd_read, int fd_sock)
{
t_client *list;
list = 0;
FD_ZERO(fd_read);
FD_SET(fd_sock, fd_read);
my_put_in_list(&list, fd_sock);
return (list);
}
et voici celui du client :
#include <stdlib.h>
#include "client.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <winsock2.h>
void check(fd_set *fd_read, int fd_sock, char *buf);
int main(int ac, char **av)
{
struct sockaddr_in sock;
int fd_sock;
int size;
struct hostent *host;
fd_set fd_read;
char buf[1024];
int err;
WORD wVersionRequested;
WSADATA wsaData;
int err_ini;
size = sizeof (struct sockaddr_in);
wVersionRequested = MAKEWORD( 2, 2 );
err_ini = WSAStartup( wVersionRequested, &wsaData );
if (err_ini != 0)
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Aucune librairie Winsock trouvee.\n" );
return (-1);
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2)
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
printf("Aucune librairie Winsock trouvee.\n" );
return (-1);
}
if ((fd_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
err = WSAGetLastError();
printf("error socket: %d, %d\n", err, fd_sock);
exit(-1);
}
if (!(host = gethostbyname(av[1])))
{
err = WSAGetLastError();
printf("error gethostbyname: %d, %d\n", err, fd_sock);
exit(-1);
}
sock.sin_family = AF_INET;
sock.sin_port = htons(atoi("13357" ));//atoi(av[2]));
memcpy(host->h_addr, &(sock.sin_addr.s_addr), host->h_length);
printf("prout\n" );
if (-1 == connect(fd_sock, &sock, size))
{
err = WSAGetLastError();
printf("error connect: %d\n", err);
exit(-1);
}
printf("prout\n" );
MY_WRITE(1, PROMPT);
while (1)
{
FD_ZERO(&fd_read);
FD_SET(0, &fd_read);
FD_SET(fd_sock, &fd_read);
if (-1 == select(fd_sock + 1, &fd_read, 0, 0, 0))
{
err = WSAGetLastError();
printf("error select: %d\n", err);
exit(-1);
}
else
check(&fd_read, fd_sock, buf);
}
return (0);
}
int loop(int fd_sock)
{
char buf[1024];
int lu;
lu = read(0, buf, 1024);
if (lu > 1)
write(fd_sock, buf, lu);
else
MY_WRITE(1, PROMPT);
return (0);
}
void check(fd_set *fd_read, int fd_sock, char *buf)
{
if (FD_ISSET(0, fd_read))
loop(fd_sock);
if (FD_ISSET(fd_sock, fd_read))
{
memset(buf, 0, 1024);
if (0 > read(fd_sock, buf, 1024))
{
printf("%s\n", ENDCON);
exit(0);
}
MY_WRITE(1, buf);
}
}
voilou, je vois po ou ca peut merder
ps : tout ca marche tres bien sous BSD....