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. multithreaded TCP Server Application(MFC)

multithreaded TCP Server Application(MFC)

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadmintutorialquestion
4 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.
  • M Offline
    M Offline
    Manmohan29
    wrote on last edited by
    #1

    I am writing a multithreaded TCP Server Application(MFC). I have written simple TCP server earlier n it worked fine. The tutorial I used told that for multithreaded server I have to use more than one Socket variable and It also told that the server should spin individual thread for every client. But I think I am writing wrong code. Where am I going wrong? any suggestions please. My Code looks like this:-

    // Declarations of functions and variables.

    class CsockDlg : public CDialog
    {
    // Construction
    public:
    CsockDlg(CWnd* pParent = NULL); // standard constructor

    // Dialog Data
    .

    .
    
    .
    

    private:
    CMySocket m_sListenSocket;
    CMySocket m_sConnectSocket;
    public:
    void OnAccept(void);
    void OnConnect(void);
    void OnClose(void);
    void OnReceive(void);
    void OnSend(void);
    static UINT ThreadServer(LPVOID pParam);
    void TS(void);
    };

    // Definitions of functions of CsockDlg Class.

    BOOL CsockDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.
    
    // IDM\_ABOUTBOX must be in the system command range.
    ASSERT((IDM\_ABOUTBOX & 0xFFF0) == IDM\_ABOUTBOX);
    .
    
    .
    
    .
    
    // TODO: Add extra initialization here
    int m\_iPort = 4000;	// Default port for listening connections
    
    m\_sListenSocket.Create(m\_iPort);	// Create listening socket
    m\_sListenSocket.Listen();	// Start listening on that port
    
    return TRUE;  // return TRUE  unless you set the focus to a control
    

    }

    void CsockDlg::OnAccept(void)
    {
    //m_sListenSocket.Accept(m_sConnectSocket);
    AfxBeginThread(ThreadServer, this); // Spin thread for every client
    }

    UINT CsockDlg::ThreadServer(LPVOID pParam)
    {
    CsockDlg* O = (CsockDlg*)pParam;
    CMySocket c;
    O->m_sListenSocket.Accept(c);
    return 0;
    }

    Future Lies in Present. Manmohan Bishnoi

    M 1 Reply Last reply
    0
    • M Manmohan29

      I am writing a multithreaded TCP Server Application(MFC). I have written simple TCP server earlier n it worked fine. The tutorial I used told that for multithreaded server I have to use more than one Socket variable and It also told that the server should spin individual thread for every client. But I think I am writing wrong code. Where am I going wrong? any suggestions please. My Code looks like this:-

      // Declarations of functions and variables.

      class CsockDlg : public CDialog
      {
      // Construction
      public:
      CsockDlg(CWnd* pParent = NULL); // standard constructor

      // Dialog Data
      .

      .
      
      .
      

      private:
      CMySocket m_sListenSocket;
      CMySocket m_sConnectSocket;
      public:
      void OnAccept(void);
      void OnConnect(void);
      void OnClose(void);
      void OnReceive(void);
      void OnSend(void);
      static UINT ThreadServer(LPVOID pParam);
      void TS(void);
      };

      // Definitions of functions of CsockDlg Class.

      BOOL CsockDlg::OnInitDialog()
      {
      CDialog::OnInitDialog();

      // Add "About..." menu item to system menu.
      
      // IDM\_ABOUTBOX must be in the system command range.
      ASSERT((IDM\_ABOUTBOX & 0xFFF0) == IDM\_ABOUTBOX);
      .
      
      .
      
      .
      
      // TODO: Add extra initialization here
      int m\_iPort = 4000;	// Default port for listening connections
      
      m\_sListenSocket.Create(m\_iPort);	// Create listening socket
      m\_sListenSocket.Listen();	// Start listening on that port
      
      return TRUE;  // return TRUE  unless you set the focus to a control
      

      }

      void CsockDlg::OnAccept(void)
      {
      //m_sListenSocket.Accept(m_sConnectSocket);
      AfxBeginThread(ThreadServer, this); // Spin thread for every client
      }

      UINT CsockDlg::ThreadServer(LPVOID pParam)
      {
      CsockDlg* O = (CsockDlg*)pParam;
      CMySocket c;
      O->m_sListenSocket.Accept(c);
      return 0;
      }

      Future Lies in Present. Manmohan Bishnoi

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

      Question: Where did you get that example from? There are plenty of examples out there that can help setting up a working networking application (with or without multi threading). For some examples using sockets see Winsock FAQ[^]. /M

      Future lies in the future.

      M 1 Reply Last reply
      0
      • M Moak

        Question: Where did you get that example from? There are plenty of examples out there that can help setting up a working networking application (with or without multi threading). For some examples using sockets see Winsock FAQ[^]. /M

        Future lies in the future.

        M Offline
        M Offline
        Manmohan29
        wrote on last edited by
        #3

        Tutorial was only for Simple TCP server. This is my experimental code.

        Future Lies in Present. Manmohan Bishnoi

        M 1 Reply Last reply
        0
        • M Manmohan29

          Tutorial was only for Simple TCP server. This is my experimental code.

          Future Lies in Present. Manmohan Bishnoi

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

          Please have a look at the link I posted, perhaps you can find something that fits your needs. If you want to write a GUI application with networking it could be worth to have a look at event-based examples (e.g. using MFC's CAsyncSocket) it saves you the hassle of using threads and locks. Btw, there are other socket libraries than MFC out there: * Alhem * Boost * SharkEngine * QT * and many others Hope it helps! :)

          The past lies in the future

          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