Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1428 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  Winsock et création de thread

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Winsock et création de thread

n°415650
Imhotep
Le monde est mon huître !!!
Posté le 04-06-2003 à 09:02:02  profilanswer
 

Salut
 
Pour un programme que je dois faire, j'utilise winsock. Pour ce faire j'utilise 2 thread, le premier thread principal pour envoyé des infos sur le réso et un deuxième thread pour recevoir des infos en permanance sur le réso.
 
En faite le premier se connecte sur le port 5001 d'un serveur. Et le deuxième attend une connexion sur le port 5000.  
 
Mon problème est le suivant, quand je n'utilise que le premier thread (pour envoyer des infos), je n'ai pas de problème, tout marche bien. Mais quand j'utilise le deuxième tread, je ne suis plus capable d'envoyer des infos (premier thread) dès que je commance à écouter sur le port 5000 (deuxieme thread). Pourtant la connexion au serveur (par le premier thread) se passe bien. Je pense que mon deuxième thread fonctionne bien, c'est juste quand je fais un listen pour écouter le port 5000 que ca bloque.
 
Est-ce que quelqu'un saurait m'aider ???
 
Un grand merci.

mood
Publicité
Posté le 04-06-2003 à 09:02:02  profilanswer
 

n°415652
LetoII
Le dormeur doit se réveiller
Posté le 04-06-2003 à 09:03:48  profilanswer
 

Avec ton code peut être.


---------------
Le Tyran
n°415656
Imhotep
Le monde est mon huître !!!
Posté le 04-06-2003 à 09:07:30  profilanswer
 

LetoII a écrit :

Avec ton code peut être.


Ok
 
Une partie du main
 

Code :
  1. port = 5001;
  2. initSock ();
  3. test = createSocket();
  4. connexionError = connect(test, "x.x.x.x", 5001);
  5. //if (connexionError == 0)
  6.  threadStart(5000);
  7. //else
  8. // threadStart(5001);


 
Voici mon fichier pour le réso.
 
H

Code :
  1. void initSock (void);
  2. SOCKET createSocket(void);
  3. void assignPort(SOCKET s, int port);
  4. void listen(SOCKET s);
  5. int connect(SOCKET s, char *adress, int port);
  6. SOCKET acceptConnexion(SOCKET s);
  7. void write(SOCKET s, char *string);
  8. int read(SOCKET s, char *buffer);
  9. void close(SOCKET s);


CPP

Code :
  1. #include <winsock2.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #define BUFFER_SIZE 256
  5. void initSock (void){
  6. // Must be done at the beginning of every WinSock program
  7. WSADATA w;    // used to store information about WinSock version
  8. int error = WSAStartup (0x0202, &w);   // Fill in w
  9. if (error)
  10. { // there was an error
  11.  return;
  12. }
  13. if (w.wVersion != 0x0202)
  14. { // wrong WinSock version!
  15.  WSACleanup (); // unload ws2_32.dll
  16.  return;
  17. }
  18. }
  19. SOCKET createSocket(void) {
  20. return socket (AF_INET, SOCK_STREAM, 0); // Create socket
  21. }
  22. void assignPort(SOCKET s, int port) {
  23. // Note that you should only bind server sockets, not client sockets
  24. // SOCKET s is a valid socket
  25. // WSAStartup has been called
  26. sockaddr_in addr; // the address structure for a TCP socket
  27. addr.sin_family = AF_INET;      // Address family Internet
  28. addr.sin_port = htons (port);   // Assign port to this socket
  29. addr.sin_addr.s_addr = htonl (INADDR_ANY);   // No destination
  30. if (bind(s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR)
  31. { // error
  32.  WSACleanup ();  // unload WinSock
  33.  return;         // quit
  34. }
  35. }
  36. void listen(SOCKET s) {
  37. // WSAStartup () has been called
  38. // SOCKET s is valid
  39. // s has been bound to a port using sockaddr_in sock
  40. if (listen(s,5)==SOCKET_ERROR)
  41. { // error!  unable to listen
  42.  printf("error!  unable to listen" );
  43.  WSACleanup ();
  44.  return;
  45. }
  46. }
  47. int connect(SOCKET s, char *adress, int port){
  48. // WSAStartup () has been called
  49. // SOCKET s is valid
  50. // s has been bound to a port using sockaddr_in sock
  51. sockaddr_in target;
  52. target.sin_family = AF_INET;           // address family Internet
  53. target.sin_port = htons (port);        // set server?s port number
  54. target.sin_addr.s_addr = inet_addr (adress);  // set server?s IP
  55. if (connect(s, (LPSOCKADDR)&target, sizeof(target)) == SOCKET_ERROR)
  56. { // an error connecting has occurred!
  57.  printf("an error connecting has occurred!" );
  58.  WSACleanup ();
  59.  return -1;
  60. }
  61. return 0;
  62. }
  63. SOCKET acceptConnexion(SOCKET s){
  64. // WSAStartup () has been called
  65. // SOCKET s is valid
  66. // s has been bound to a port using sockaddr_in sock
  67. // s is listening
  68. //#define MAX_CLIENTS 1;             // just used for clearness
  69. int number_of_clients = 0;
  70. SOCKET client[1/*MAX_CLIENTS*/];        // socket handles to clients
  71. sockaddr client_sock[1/*MAX_CLIENTS*/]; // info on client sockets
  72. while (number_of_clients < 1/*MAX_CLIENTS*/) // let MAX_CLIENTS connect
  73. {
  74.  client[number_of_clients] =  // accept a connection
  75.  accept (s, NULL/*client_sock[number_of_clients]*/, NULL/*&addr_size*/);
  76.  if (client[number_of_clients] == INVALID_SOCKET)
  77.  { // error accepting connection
  78.   printf("error accepting connection" );
  79.      WSACleanup ();
  80.   return client[0];
  81.  } else { // client connected successfully
  82.   // start a thread that will communicate with client
  83.   //startThread (client[number_of_clients]);
  84.   number_of_clients++;
  85.   return client[0];
  86.  }
  87. }
  88. }
  89. void write(SOCKET s, char *string) {
  90. // SOCKET s is initialized
  91. char buffer[BUFFER_SIZE];
  92. sprintf (buffer, string);
  93. //buffer[23] = '\0';
  94. send (s, buffer, BUFFER_SIZE, 0);
  95. printf("%s %d\n", buffer, strlen(buffer));
  96. }
  97. int read(SOCKET s, char *buffer){
  98. int toto;
  99. toto = recv (s, buffer, BUFFER_SIZE, 0);
  100. //printf("%s, buffer: %d, string: %d, recv: %d\n", buffer, sizeof(buffer), strlen(buffer), toto);
  101. return toto;
  102. }
  103. void close(SOCKET s) {
  104. shutdown (s, SD_SEND);  // s cannot send anymore
  105. // you should check to see if any last data has arrived here
  106. closesocket (s);   // close
  107. }


Message édité par Imhotep le 04-06-2003 à 10:18:04
n°415658
Imhotep
Le monde est mon huître !!!
Posté le 04-06-2003 à 09:09:05  profilanswer
 

Pour le thread
 

Code :
  1. #include "stdio.h"
  2. #include "process.h"
  3. #include "network2.h"
  4. #define BUFFER_SIZE 256
  5. int port;
  6. float xNet, yNet, zNet;
  7. int tmpFrameNew = -1, part = 0;
  8. // déclaration de la section critique
  9. // code de la première tâche : PremierThread
  10. DWORD WINAPI readThread ( LPVOID IpvThreadParam )
  11. {
  12. SOCKET test;
  13. SOCKET testClient;
  14. char buffer[BUFFER_SIZE];
  15. //int port = 5001;
  16. int rint;
  17. int tempInt, tmpX, tmpY, tmpZ;
  18. /*
  19. if (argc == 2) port = (int)argv[1];
  20. printf("arg: %d.\nport: %d.\n", argc, port);
  21. */
  22. initSock ();
  23. //
  24. test = createSocket();
  25. assignPort(test, port);
  26. listen(test);
  27. testClient = acceptConnexion(test);
  28. while(true){
  29.  rint = read(testClient, buffer);
  30.  if (rint > 0) {
  31.   tempInt = sscanf(buffer,"%d %d %f %f %f", tmpFrameNew, part, tmpX, tmpY, tmpZ);
  32.   if (tempInt == 5 ) {
  33.    xNet = tmpX;
  34.    yNet = tmpY;
  35.    zNet = tmpZ;
  36.   }
  37.  }
  38. }
  39. //printf("%s %d", buffer, strlen(buffer));
  40. close(testClient);
  41. close(test);
  42. return (0);
  43. }
  44. // point d'entrée du programme
  45. void threadStart (int por)
  46. {
  47.   // Initialisation de la section critique
  48. port = por;
  49.   HANDLE hThreads [ 1 ] ;
  50.   DWORD dwThreadId ;
  51.   DWORD dwThreadParam = 1 ;
  52.   // création des Threads
  53.  
  54.   hThreads [ 0 ] = CreateThread ( NULL, NULL, readThread, &dwThreadParam, 0, &dwThreadId ) ;
  55.   // attente de fin d'exécution des Threads
  56.    //WaitForMultipleObjects ( 1, hThreads, TRUE, INFINITE) ;
  57.   // destruction des Threads et de la section critique  
  58.   //CloseHandle ( hThreads [ 0 ] ) ;
  59.   // destruction de la section critique
  60. }
  61. float retourXYZ(int nb) {
  62. if (nb == 1)
  63.  return xNet;
  64. else if (nb == 2)
  65.  return yNet;
  66. else
  67.  return zNet;
  68. }
  69. int retourFrame() {
  70. return tmpFrameNew;
  71. }
  72. int retourPart() {
  73. return part;
  74. }


Message édité par Imhotep le 04-06-2003 à 09:24:33
n°415663
LetoII
Le dormeur doit se réveiller
Posté le 04-06-2003 à 09:12:14  profilanswer
 

tu devrais utiliser les balises de codes c pas top lisible là.


Message édité par LetoII le 04-06-2003 à 10:20:10

---------------
Le Tyran
n°415673
Imhotep
Le monde est mon huître !!!
Posté le 04-06-2003 à 09:23:43  profilanswer
 

LetoII a écrit :

tu devrais utiliser les balises de codes c pas top lisible là.


 
Merci  :D

n°415682
LetoII
Le dormeur doit se réveiller
Posté le 04-06-2003 à 09:38:43  profilanswer
 

Le main ça pourait être sympa aussi :D


Message édité par LetoII le 04-06-2003 à 09:39:10

---------------
Le Tyran
n°415742
Imhotep
Le monde est mon huître !!!
Posté le 04-06-2003 à 10:18:43  profilanswer
 

Vala (voir plus haut)
 :)


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  Winsock et création de thread

 

Sujets relatifs
Exercice PHP - Création de tableaucreation dune toolbar pr ie
winsock ??création d'un site
[PHP] Création de Sous-dossier impossible[QT] thread
compilation de xxx.java et création de xxx$1.java et xxx$2.javacréation de liens hypertextes
[URGENT] [MSDE-SQLServer] Création d'une DB en ligne de commandeCreation de KM sous Patrol
Plus de sujets relatifs à : Winsock et création de thread


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR