CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    23

    How can i make this multithreaded?

    Hi,

    I am creating a windows socket application whereby client can connect to the server and the server responds with a series of messages. This is a test application, and i have got it working, but only one client can connect. I want to enable multiple connections. I need to change the code here:-

    Code:
    #ifndef __TCPSERVER_H
    #define __TCPSERVER_H
    
    #include "Sockets.h"
    #include <string>
    
    using namespace std;
    
    class TCPServer {	
    protected:
    	ListeningSocket	*listener;
    	int				backlog;
    	sockaddr_in		server_addr; 
    	size_t			localPort;
    public:
    	TCPServer(size_t port, int backLog = 1);
    	virtual ~TCPServer();
    	string receive(TCPSocket *connSocket, int len);
      	bool send(TCPSocket *connSocket, string sent_str);
    
    	void processLoop();
    	virtual bool run(TCPSocket *connSocket, 
    		           	  sockaddr_in *client_addr) = 0;
    };
    
    #endif //__TCPSERVER_H
    Is there a way i can do this? I think i need to check if the thread is running first and then enable multiple connections but dont really know how to do it.

    Hope someone can help..

    If you needs the Sockets.h file i will provide this.

    Regards
    Billy

  2. #2
    Join Date
    Jan 2010
    Posts
    12

    Re: How can i make this multithreaded?

    you don't really need to make it multithreaded, although you could create a worker thread for each client that connects (waste of time and resources though)

    Usually you listen on a port, on an incoming connection, accept, and shunt it to a different port, then create another socket listening on your server port. You could have a (worker) thread that just listens for connections and then creates client sockets for your main thread to issue data to.

  3. #3
    Join Date
    Oct 2009
    Posts
    23

    Re: How can i make this multithreaded?

    well i was thinking i would need to make it multi-threaded to enable multiple connections?

  4. #4
    Join Date
    Jan 2010
    Posts
    10

    Re: How can i make this multithreaded?

    http://www.madwizard.org/programming/tutorials/netcpp/

    Here you will find a very good tutorial about sockets.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured