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. Sockets trouble in Visual Studio 2008

Sockets trouble in Visual Studio 2008

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiotutorialcsharphelp
13 Posts 4 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' m a learner in visual c++ developing environment. Here's m problem . . . ------------------------> I am using a tutorial to make a simple program to send messages to another program using CAsyncSocket class. I wrote complete application's code as given in the example(in visual studio 6 IDE). I compiled the application and the program run successfully without any errors. :) Then I compiled same program in visual studio 2008(Professional Edition) and It also run as expected. :) But When I wrote the same program from scratch in visual studio 2008 it showed some misbehavior while receiving data.:confused: The main problem with the code given below is that it sends data(as shown in the code below) but on the receiving application only first byte is received(When project is written from scratch in VS 2008 Pro). But if application is written in Visual studio 6 then there is no such problem. I' m totally confused what to do? where is the problem actually residing? Please send any suggestion. -->I have VC++ 2008 FeaturePack installed. -->Create an MFC Application named "Sockets" and link MFC as statically. -->then use files below to make the application. The individual codes of my complete application's source files is given here:-

    //---------------------------------------------------------------------------------------------------
    // MySocket.h
    #pragma once

    // CMySocket command target

    class CMySocket : public CAsyncSocket
    {
    public:
    CMySocket();
    virtual ~CMySocket();
    private:
    CDialog* m_pWnd;
    public:
    void SetParent(CDialog* pWnd);
    protected:
    virtual void OnAccept(int nErrorCode);
    virtual void OnConnect(int nErrorCode);
    virtual void OnClose(int nErrorCode);
    virtual void OnReceive(int nErrorCode);
    virtual void OnSend(int nErrorCode);
    };
    //---------------------------------------------------------------------------------------------------

    //---------------------------------------------------------------------------------------------------
    // MySocket.cpp : implementation file
    //

    #include "stdafx.h"
    #include "Sockets.h"
    #include "MySocket.h"

    #include "SocketsDlg.h"

    // CMySocket

    CMySocket::CMySocket()
    : m_pWnd(NULL)
    {
    }

    CMySocket::~CMySocket()
    {
    }

    // CMySocket member functions

    void CMySocket::SetParent(CDialog* pWnd)
    {
    m_pWnd = pWnd;
    }

    void CMySocket::OnAccept(int nErrorCode)
    {
    if(nErrorCode == 0)
    ((CSocketsDlg*)m_pWnd)->OnAccept();
    }

    void CMySocket::OnConnect(int nErrorCode)
    {
    if(nErrorCod

    _ S 2 Replies Last reply
    0
    • M Manmohan29

      I' m a learner in visual c++ developing environment. Here's m problem . . . ------------------------> I am using a tutorial to make a simple program to send messages to another program using CAsyncSocket class. I wrote complete application's code as given in the example(in visual studio 6 IDE). I compiled the application and the program run successfully without any errors. :) Then I compiled same program in visual studio 2008(Professional Edition) and It also run as expected. :) But When I wrote the same program from scratch in visual studio 2008 it showed some misbehavior while receiving data.:confused: The main problem with the code given below is that it sends data(as shown in the code below) but on the receiving application only first byte is received(When project is written from scratch in VS 2008 Pro). But if application is written in Visual studio 6 then there is no such problem. I' m totally confused what to do? where is the problem actually residing? Please send any suggestion. -->I have VC++ 2008 FeaturePack installed. -->Create an MFC Application named "Sockets" and link MFC as statically. -->then use files below to make the application. The individual codes of my complete application's source files is given here:-

      //---------------------------------------------------------------------------------------------------
      // MySocket.h
      #pragma once

      // CMySocket command target

      class CMySocket : public CAsyncSocket
      {
      public:
      CMySocket();
      virtual ~CMySocket();
      private:
      CDialog* m_pWnd;
      public:
      void SetParent(CDialog* pWnd);
      protected:
      virtual void OnAccept(int nErrorCode);
      virtual void OnConnect(int nErrorCode);
      virtual void OnClose(int nErrorCode);
      virtual void OnReceive(int nErrorCode);
      virtual void OnSend(int nErrorCode);
      };
      //---------------------------------------------------------------------------------------------------

      //---------------------------------------------------------------------------------------------------
      // MySocket.cpp : implementation file
      //

      #include "stdafx.h"
      #include "Sockets.h"
      #include "MySocket.h"

      #include "SocketsDlg.h"

      // CMySocket

      CMySocket::CMySocket()
      : m_pWnd(NULL)
      {
      }

      CMySocket::~CMySocket()
      {
      }

      // CMySocket member functions

      void CMySocket::SetParent(CDialog* pWnd)
      {
      m_pWnd = pWnd;
      }

      void CMySocket::OnAccept(int nErrorCode)
      {
      if(nErrorCode == 0)
      ((CSocketsDlg*)m_pWnd)->OnAccept();
      }

      void CMySocket::OnConnect(int nErrorCode)
      {
      if(nErrorCod

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      :wtf:

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • M Manmohan29

        I' m a learner in visual c++ developing environment. Here's m problem . . . ------------------------> I am using a tutorial to make a simple program to send messages to another program using CAsyncSocket class. I wrote complete application's code as given in the example(in visual studio 6 IDE). I compiled the application and the program run successfully without any errors. :) Then I compiled same program in visual studio 2008(Professional Edition) and It also run as expected. :) But When I wrote the same program from scratch in visual studio 2008 it showed some misbehavior while receiving data.:confused: The main problem with the code given below is that it sends data(as shown in the code below) but on the receiving application only first byte is received(When project is written from scratch in VS 2008 Pro). But if application is written in Visual studio 6 then there is no such problem. I' m totally confused what to do? where is the problem actually residing? Please send any suggestion. -->I have VC++ 2008 FeaturePack installed. -->Create an MFC Application named "Sockets" and link MFC as statically. -->then use files below to make the application. The individual codes of my complete application's source files is given here:-

        //---------------------------------------------------------------------------------------------------
        // MySocket.h
        #pragma once

        // CMySocket command target

        class CMySocket : public CAsyncSocket
        {
        public:
        CMySocket();
        virtual ~CMySocket();
        private:
        CDialog* m_pWnd;
        public:
        void SetParent(CDialog* pWnd);
        protected:
        virtual void OnAccept(int nErrorCode);
        virtual void OnConnect(int nErrorCode);
        virtual void OnClose(int nErrorCode);
        virtual void OnReceive(int nErrorCode);
        virtual void OnSend(int nErrorCode);
        };
        //---------------------------------------------------------------------------------------------------

        //---------------------------------------------------------------------------------------------------
        // MySocket.cpp : implementation file
        //

        #include "stdafx.h"
        #include "Sockets.h"
        #include "MySocket.h"

        #include "SocketsDlg.h"

        // CMySocket

        CMySocket::CMySocket()
        : m_pWnd(NULL)
        {
        }

        CMySocket::~CMySocket()
        {
        }

        // CMySocket member functions

        void CMySocket::SetParent(CDialog* pWnd)
        {
        m_pWnd = pWnd;
        }

        void CMySocket::OnAccept(int nErrorCode)
        {
        if(nErrorCode == 0)
        ((CSocketsDlg*)m_pWnd)->OnAccept();
        }

        void CMySocket::OnConnect(int nErrorCode)
        {
        if(nErrorCod

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        My guess would be that the 'from scratch in VS2008' version uses Unicode, the 'upgraded from VS6' one uses ASCII. I can see various places where you are (incorrectly) assuming ASCII, but the killer is in CSocketsDlg::OnReceive. Where you read a number of bytes and then append to a CString. The string you've received will have '\0' characters in it, because it's a wchar_t string that you've interpreted as char, so when you append it to a CString, the rlevant method will use strlen to find the string length, see the zero byte after the first character and say 'Aha, that's the end of the string!' - but not where you a) wanted, or b) expected. So - fix the ASCII assumptions, man!!!! Or turn off Unicode

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M 1 Reply Last reply
        0
        • S Stuart Dootson

          My guess would be that the 'from scratch in VS2008' version uses Unicode, the 'upgraded from VS6' one uses ASCII. I can see various places where you are (incorrectly) assuming ASCII, but the killer is in CSocketsDlg::OnReceive. Where you read a number of bytes and then append to a CString. The string you've received will have '\0' characters in it, because it's a wchar_t string that you've interpreted as char, so when you append it to a CString, the rlevant method will use strlen to find the string length, see the zero byte after the first character and say 'Aha, that's the end of the string!' - but not where you a) wanted, or b) expected. So - fix the ASCII assumptions, man!!!! Or turn off Unicode

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

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

          Here's some data sent and received b/w both applications and their versions->

          ( Visual studio 6 ) ( VS 2008 Pro )

          sent recvd | sent recvd

          123456789 | 123456789

                         1                                 123456789
          

          I hope this will give u a clear understanding about my BUG. If u compile these applications then u should get these results.

          Manmohan Bishnoi

          S 1 Reply Last reply
          0
          • M Manmohan29

            Here's some data sent and received b/w both applications and their versions->

            ( Visual studio 6 ) ( VS 2008 Pro )

            sent recvd | sent recvd

            123456789 | 123456789

                           1                                 123456789
            

            I hope this will give u a clear understanding about my BUG. If u compile these applications then u should get these results.

            Manmohan Bishnoi

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            monu_biosman wrote:

            I hope this will give u a clear understanding about my BUG.

            I already understand what your bug is, well enough that I told you how to fix it - either turn off Unicode in your app developed from scratch with VS2008, or make your usage of Unicode/ASCII strings consistent.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            M 1 Reply Last reply
            0
            • S Stuart Dootson

              monu_biosman wrote:

              I hope this will give u a clear understanding about my BUG.

              I already understand what your bug is, well enough that I told you how to fix it - either turn off Unicode in your app developed from scratch with VS2008, or make your usage of Unicode/ASCII strings consistent.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

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

              how to turnoff Unicode in current project in VS 2008 Pro

              Manmohan Bishnoi

              S 1 Reply Last reply
              0
              • M Manmohan29

                how to turnoff Unicode in current project in VS 2008 Pro

                Manmohan Bishnoi

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                Select the project in solution explorer, right-click on it and select Properties from the context menu. Now go to the Configuration Properties->General property page and set the Character Set property to 'Use Multi-Byte Character Set'

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                M 1 Reply Last reply
                0
                • S Stuart Dootson

                  Select the project in solution explorer, right-click on it and select Properties from the context menu. Now go to the Configuration Properties->General property page and set the Character Set property to 'Use Multi-Byte Character Set'

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

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

                  Debug version is working ok but not having xp common controls look. Release version not working ok but having xp common controls manifest.

                  Manmohan Bishnoi

                  S 2 Replies Last reply
                  0
                  • M Manmohan29

                    Debug version is working ok but not having xp common controls look. Release version not working ok but having xp common controls manifest.

                    Manmohan Bishnoi

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #9

                    Did you change the property for Debug AND Release configurations - the properties are (unfortunately, in many ways) set separately.

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    1 Reply Last reply
                    0
                    • M Manmohan29

                      Debug version is working ok but not having xp common controls look. Release version not working ok but having xp common controls manifest.

                      Manmohan Bishnoi

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #10

                      And the manifest file...ummm - is the project property Configuration Properties->Linker->Manifest File->Generate Manifest set to Yes in all cases? Although it may be that XP themes need Unicode.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      M 1 Reply Last reply
                      0
                      • S Stuart Dootson

                        And the manifest file...ummm - is the project property Configuration Properties->Linker->Manifest File->Generate Manifest set to Yes in all cases? Although it may be that XP themes need Unicode.

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

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

                        done as u told me to do. But no use. Anyway atleast debug version got somewhat debugged.

                        Manmohan Bishnoi

                        S 1 Reply Last reply
                        0
                        • M Manmohan29

                          done as u told me to do. But no use. Anyway atleast debug version got somewhat debugged.

                          Manmohan Bishnoi

                          S Offline
                          S Offline
                          SquiZZlo
                          wrote on last edited by
                          #12

                          Hi. I'm stuck with the same problem. Whenever my program receives data from any tcp/ip server, it displays it correctly, but when it sends data back to the server, the latter receives only the first character. Turning off Unicode is not an option, becouse the tcp/ip communication is only a small part of the project, and this change generates lots of other errors. I'm also using the tutorial from David Chapman's "Teach yourself Visual C++ 6...", and Visual Studio 2k8, so my code is almost exactly the same as monu_biosman's. I'd like to ask Stuart, or anyone else for that matter, what exactly I need to change to make it work. I already made lots of tests, but none seem to work.

                          //original code: (m_strMessage is a CString)
                          iLen = m_strMessage.GetLength();
                          iSent = m_sConnectSocket.Send(LPCTSTR(m_strMessage), iLen);

                          //I've been changing the iLen, and it's not the problem. Then I've tried:
                          iSent = m_sConnectSocket.Send("blablabla", 9);

                          //and it works!!!. But then I've tried the seemingly same code:
                          CString buffer = "blablabla";
                          iSent = m_sConnectSocket.Send(buffer, 9);

                          //and it also sends only one character (b to be exact). So the problem can propably be solved by adding //some type of conversion. So far i've tried this:
                          char[1024] buffer;
                          wsprintf(buffer,"%s",m_strMessage);
                          iSent = m_sConnectSocket.Send(m_strMessage, 1024);
                          //and.. it does not compile, C2664 error, "'wsprintfW' : cannot convert parameter 1 from 'char [1024]' to 'LPWSTR'"

                          I'm running out of ideas, or maybe I'm a retard i don't know. Any help would be greatly appreciated. Thanks in advance. SquiZZlo

                          S 1 Reply Last reply
                          0
                          • S SquiZZlo

                            Hi. I'm stuck with the same problem. Whenever my program receives data from any tcp/ip server, it displays it correctly, but when it sends data back to the server, the latter receives only the first character. Turning off Unicode is not an option, becouse the tcp/ip communication is only a small part of the project, and this change generates lots of other errors. I'm also using the tutorial from David Chapman's "Teach yourself Visual C++ 6...", and Visual Studio 2k8, so my code is almost exactly the same as monu_biosman's. I'd like to ask Stuart, or anyone else for that matter, what exactly I need to change to make it work. I already made lots of tests, but none seem to work.

                            //original code: (m_strMessage is a CString)
                            iLen = m_strMessage.GetLength();
                            iSent = m_sConnectSocket.Send(LPCTSTR(m_strMessage), iLen);

                            //I've been changing the iLen, and it's not the problem. Then I've tried:
                            iSent = m_sConnectSocket.Send("blablabla", 9);

                            //and it works!!!. But then I've tried the seemingly same code:
                            CString buffer = "blablabla";
                            iSent = m_sConnectSocket.Send(buffer, 9);

                            //and it also sends only one character (b to be exact). So the problem can propably be solved by adding //some type of conversion. So far i've tried this:
                            char[1024] buffer;
                            wsprintf(buffer,"%s",m_strMessage);
                            iSent = m_sConnectSocket.Send(m_strMessage, 1024);
                            //and.. it does not compile, C2664 error, "'wsprintfW' : cannot convert parameter 1 from 'char [1024]' to 'LPWSTR'"

                            I'm running out of ideas, or maybe I'm a retard i don't know. Any help would be greatly appreciated. Thanks in advance. SquiZZlo

                            S Offline
                            S Offline
                            SquiZZlo
                            wrote on last edited by
                            #13

                            Ok, I've found the answer to my problem on another forum:

                              iLen = m\_strMessage.GetLength();
                              CStringA ansiString(m\_strMessage);//one freaking conversion!
                              iSent = m\_sConnectSocket.Send(ansiString, iLen); 
                            

                            Works prefectly! :-D

                            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