Pb de socket sous Windows [C] - C++ - Programmation
Marsh Posté le 16-05-2002 à 15:59:39
Cette erreur vient du fait que tu n'as pas appelé la fonction WSAStartup avant de créer ton socket. Cette fonction est nécessaire pour initialiser la librairie winsock. 
 
Voir ici pour des infos sur cette fonction 
 
http://msdn.microsoft.com/library/ [...] f_1v8y.asp 
[jfdsdjhfuetppo]--Message édité par Harkonnen le 16-05-2002 à 16:05:22--[/jfdsdjhfuetppo]
Marsh Posté le 16-05-2002 à 17:12:17
je penses que je peux dire que tu es mon sauveur, encore merci mille fois!! 
 
Marsh Posté le 16-05-2002 à 17:13:46
 
 
Marsh Posté le 16-05-2002 à 17:39:21
Je post ici, car il s'agit aussi des sockets : 
 
j ai une application (un petit jeu en reseau), qui utilise les des socket tcp / blockant. 
 
sous unix / windows ca marche impec  (me suis debrouillé pour faire qq chose de portable) 
 
 
mais sous solaris : pas moyen de creer une socket ! 
(AF_INET, SOCK_STREAM, IPPORTO_TCP)  ((((
(((( 
(ca compile, j ai include les libs socket et nsl (sais meme pas ce que c!) ) 
 
en gros, sous solaris, j ai les lib, mais j arrive pas a faire marche le :  "socket( , , )" 
Marsh Posté le 16-05-2002 à 20:25:53
bon la socket est bien cree, mais j ai un autre pb now : c est avec connect 
 
le prog (client)arrive jusqu a la ligne ou y a connect, puis 30 sec plus tard, il pete une erreur(10060) 
 
le serveur qui ecoute fonctionne, car je l ai lance pendant 10 min, et aucune erreur (je fais un select sur les connexions arrivantes, et j ai bien l  entree standard qui se rajoute ds la file), mais il ne voit evidemment pas la demande de connexion du client!! 
 
 
alors je me demandai si, comme pour socket, il fo pas d abord appeller une fonction de windows pour que le connect marche?????????????? 
 
a vous de me dire, car sur msdn, j ai po trouve, snif! 
Marsh Posté le 16-05-2002 à 20:51:30
erreur 10060 = connection timeout 
 
le serveur n'a pas répondu à la demande de ton client. te branches tu sur le bon port ? utilises tu un firewall ? 
 
essaie de poster ton code si tu peux 
Marsh Posté le 17-05-2002 à 08:42:15
bah si c est timeout c est chelou 
j essaie bien evidemment sur le bon port (!!), mais j ai un firewall 
c est sur la meme machine que se trouvent client et serveur 
Marsh Posté le 17-05-2002 à 11:09:02
ben oui mais bon, je vois pas quoi te dire de plus... 
 
rééssaie en désactivant ton firewall, ou sur une autre machine ou alors poste ton source, peut etre qu'on y verra plus clair 
Marsh Posté le 17-05-2002 à 15:32:51
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.... 
Marsh Posté le 19-05-2002 à 22:05:34
bon je vois que mon code ne vous a pas plu!! 
 
 
bon, j ai trouve des infos, nautement sur le fait que socket sous windows renvoie une var de type SOCKET, alors que sous unix c est un int (descripteur de la socket) 
 
le pb, c est que ce int j en ai besoin pour select!!!! 
et je sais po ou le trouver!!! 
 
 
 
 
 
 
voila, donc je suis tjs ds l impace, merci de m aider! 
Marsh Posté le 16-05-2002 à 14:49:33
j essaye de creer une socket avec la lib winsock2, mais la fonction WSAGetLastError me renvoie tjs le meme code 10093 (???)
je sais po ce aue c est comme erreur, et je vois pas pk ca marche pas surtout....
donc s il y a des pros du C sous windows, qu ils me donnent la ligne pour juste creer une socket, merci