Sockets trouble in Visual Studio 2008
-
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 -
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:wtf:
«_Superman_» I love work. It gives me something to do between weekends.
-
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(nErrorCodMy 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
-
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
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
-
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
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
-
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
how to turnoff Unicode in current project in VS 2008 Pro
Manmohan Bishnoi
-
how to turnoff Unicode in current project in VS 2008 Pro
Manmohan Bishnoi
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
-
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
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
-
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
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
-
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
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
-
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
done as u told me to do. But no use. Anyway atleast debug version got somewhat debugged.
Manmohan Bishnoi
-
done as u told me to do. But no use. Anyway atleast debug version got somewhat debugged.
Manmohan Bishnoi
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
-
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