Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ socket and thread [modified]

C++ socket and thread [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    canard29
    wrote on last edited by
    #1

    Hi, I have a small programm which listen a socket in a new thread. but I want to restart the porgramm after the first message. Start the thread in main.cpp

    hThread = CreateThread( 
    				NULL,                       
    				0,                           
    				ThreadFunc,                
    				&dwThrdParam,              
    				0,                          
    				&dwThreadId);  
    

    The programm :

    DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
    { 
    			WSAStartup(MAKEWORD(2,2),&initialisation_win32);
    			socket_recevoir=socket(AF_INET,SOCK_DGRAM,0);
    			information_recevoir.sin_family=AF_INET;
    			information_recevoir.sin_addr.s_addr=INADDR_ANY; 
    			information_recevoir.sin_port=htons(25);
    
    			bind(socket_recevoir,(struct sockaddr*)&information_recevoir,sizeof(information_recevoir));
    
    			tempo=sizeof(information_recevoir); 
    			nb_caracters=recvfrom(socket_recevoir,buffer,1515,0,(struct sockaddr*)&information_recevoir,&tempo);
    			buffer[nb_caracters]=0;
    
    			if (nb_caracters >0)
    			{
    				ExecuteCommand(buffer, NULL, 0);
    			}
    	
    		
    			closesocket(socket_recevoir);
    		
    
    	return 0; 
    } 
    

    In main.cpp, I want to call again this programm. Can you help me please ?

    modified on Monday, April 5, 2010 10:42 AM

    M 1 Reply Last reply
    0
    • C canard29

      Hi, I have a small programm which listen a socket in a new thread. but I want to restart the porgramm after the first message. Start the thread in main.cpp

      hThread = CreateThread( 
      				NULL,                       
      				0,                           
      				ThreadFunc,                
      				&dwThrdParam,              
      				0,                          
      				&dwThreadId);  
      

      The programm :

      DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
      { 
      			WSAStartup(MAKEWORD(2,2),&initialisation_win32);
      			socket_recevoir=socket(AF_INET,SOCK_DGRAM,0);
      			information_recevoir.sin_family=AF_INET;
      			information_recevoir.sin_addr.s_addr=INADDR_ANY; 
      			information_recevoir.sin_port=htons(25);
      
      			bind(socket_recevoir,(struct sockaddr*)&information_recevoir,sizeof(information_recevoir));
      
      			tempo=sizeof(information_recevoir); 
      			nb_caracters=recvfrom(socket_recevoir,buffer,1515,0,(struct sockaddr*)&information_recevoir,&tempo);
      			buffer[nb_caracters]=0;
      
      			if (nb_caracters >0)
      			{
      				ExecuteCommand(buffer, NULL, 0);
      			}
      	
      		
      			closesocket(socket_recevoir);
      		
      
      	return 0; 
      } 
      

      In main.cpp, I want to call again this programm. Can you help me please ?

      modified on Monday, April 5, 2010 10:42 AM

      M Offline
      M Offline
      Moak
      wrote on last edited by
      #2

      Hi, from what I can see the socket is not doing anything or you did not post the full code. The server socket never calls listen or accept. Maybe you could provide us more information? In a nutshell there a three possible designs for a server: your server handles one client at a time (example), uses asynchronous I/O (multiple connections in a single thread context) or uses multi threading (one connection per thread). Here are some links that might come helpful: - Internet / Network - CodeProject[^] - Beej's guide to networking programming[^] - Winsock Programmer's FAQ[^] Last two have working client/server examples in section 6. Hope it helps :) /M

      Webchat in Europe :java: Now with 26% more Twitter

      modified on Monday, April 5, 2010 5:40 AM

      C 1 Reply Last reply
      0
      • M Moak

        Hi, from what I can see the socket is not doing anything or you did not post the full code. The server socket never calls listen or accept. Maybe you could provide us more information? In a nutshell there a three possible designs for a server: your server handles one client at a time (example), uses asynchronous I/O (multiple connections in a single thread context) or uses multi threading (one connection per thread). Here are some links that might come helpful: - Internet / Network - CodeProject[^] - Beej's guide to networking programming[^] - Winsock Programmer's FAQ[^] Last two have working client/server examples in section 6. Hope it helps :) /M

        Webchat in Europe :java: Now with 26% more Twitter

        modified on Monday, April 5, 2010 5:40 AM

        C Offline
        C Offline
        canard29
        wrote on last edited by
        #3

        this application execute this line : ExecuteCommand(buffer, NULL, 0) but only one time. I will check your links

        M 1 Reply Last reply
        0
        • C canard29

          this application execute this line : ExecuteCommand(buffer, NULL, 0) but only one time. I will check your links

          M Offline
          M Offline
          Moak
          wrote on last edited by
          #4

          Could you please format your code with <pre> tag for readability and remove the French comments? It was quite hard to read. Looking again at your code, you have a blocking UDP socket waiting for a connection, which calls ExecuteCommand() and exists the thread? Perhaps all you are looking for is a for loop (and a way to programatically exit the worker thread)? :) /M

          Webchat in Europe :java: Now with 26% more Twitter

          modified on Monday, April 5, 2010 9:10 AM

          C 1 Reply Last reply
          0
          • M Moak

            Could you please format your code with <pre> tag for readability and remove the French comments? It was quite hard to read. Looking again at your code, you have a blocking UDP socket waiting for a connection, which calls ExecuteCommand() and exists the thread? Perhaps all you are looking for is a for loop (and a way to programatically exit the worker thread)? :) /M

            Webchat in Europe :java: Now with 26% more Twitter

            modified on Monday, April 5, 2010 9:10 AM

            C Offline
            C Offline
            canard29
            wrote on last edited by
            #5

            Think you for your help. I removed the comments and used

            . I hope it's easier to read.
            yes, you're right, I want to add a loop but I don't where in my code.

            Think you

            M 1 Reply Last reply
            0
            • C canard29

              Think you for your help. I removed the comments and used

              . I hope it's easier to read.
              yes, you're right, I want to add a loop but I don't where in my code.

              Think you

              M Offline
              M Offline
              Moak
              wrote on last edited by
              #6

              Okay thanks for the formatting! :) Let my try to help.... put the loop around the blocking part (pseudocode):

              socket();
              bind();
              for(;;)
              {
              recvfrom();
              if(ok) ExecuteCommand();
              }
              closesocket();

              Right now the worker thread has no exit condition, you probably want to add one in order to avoid terminating the thread the hard way at application exit. Also have a look at some other issues, e.g. starting from this line

              nb_caracters=recvfrom(socket_recevoir,buffer,1515,0,(struct sockaddr*)&information_recevoir,&tempo);

              should become something like

              int nb_caracters = recvfrom(socket_recevoir, buffer, sizeof(buffer)-1, 0, (struct sockaddr*)&information_recevoir, &tempo);
              if(nb_caracters > 0) //data was received and no socket error (return value -1)?
              {
              buffer[nb_caracters] = 0; //one byte was reserved for terminating Null
              ExecuteCommand(buffer, NULL, 0); //lets hope it was a complete package and also not a duplicate
              }

              Webchat in Europe :java: Now with 26% more Twitter

              modified on Monday, April 5, 2010 3:34 PM

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups