// JP opened flex table

Click to See Complete Forum and Search --> : problem with CSocket::Create


Quasimoto
September 24th, 2003, 08:52 PM
Hi all

I'm having problems getting some code to run...

#include "stdafx.h"
#include "file_client.h"

void file_client :: GettingFile()
{
#define PORT 34000 /// Select any free port you wish

AfxSocketInit(NULL);
CSocket sockClient;
sockClient.Create( );

// "127.0.0.1" is the IP to your server, same port
sockClient.Connect("127.0.0.1", PORT);

int dataLength;
sockClient.Receive(&dataLength, 4); //Now we get the File Size first

byte* data = new byte[dataLength];
sockClient.Receive(data, dataLength); //Get the whole thing

CFile destFile("C:\\temp\\CLDMA.LOG", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);

destFile.Write(data, dataLength); // Write it

destFile.Close();

delete data;
sockClient.Close();
}


This was taken almost word for word from a tutorial. It compiles fine but for some reason it dies at sockClient.Create( );

Ahh yeah...i'm working in VC++.net if that makes any difference
Have i done something stupid?

Cheers

Q

souldog
September 24th, 2003, 10:54 PM
Do some error checking. Try this code instead of just Create. What DOes it say

if(!sockClient.Create( ))
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);


::MessageBox(NULL, static_cast<LPTCSTR>(lpMsgBuf), NULL, MB_OK);

LocalFree(lpMsgBuf);
}

souldog
September 24th, 2003, 10:56 PM
You should be calling a function which contains something like this
for all socket calls. Then display it somehow.

Quasimoto
September 24th, 2003, 11:56 PM
thanks for the reply....

it's still crashing at the sockClient.Create() line, so it doesn't get a chance to print the buffer....this is what shows up in the callstack. The output window only tells me that it's an unhandled exception

mp.dll!AfxGetInstanceHandle() Line 23 + 0x1e C++
> mp.dll!AfxRegisterWndClass(unsigned int nClassStyle=0, HICON__ * hCursor=0x00000000, HBRUSH__ * hbrBackground=0x00000000, HICON__ * hIcon=0x00000000) Line 1404 + 0x5 C++
mp.dll!CAsyncSocket::AttachHandle(unsigned int hSocket=2828, CAsyncSocket * pSocket=0x0013ad9c, int bDead=0) Line 419 + 0x22 C++
mp.dll!CAsyncSocket::Socket(int nSocketType=1, long lEvent=63, int nProtocolType=0, int nAddressFormat=2) Line 574 C++
mp.dll!CAsyncSocket::Create(unsigned int nSocketPort=0, int nSocketType=1, long lEvent=63, const char * lpszSocketAddress=0x00000000) Line 107 + 0x14 C++
mp.dll!CSocket::Create(unsigned int nSocketPort=0, int nSocketType=1, const char * lpszSocketAddress=0x00000000) Line 49 + 0x1d C++
mp.dll!file_client::GettingFile() Line 11 + 0xe C++
mp.dll!ClientCommand(edict_s * pEntity=0x05ca671c) Line 501 C++
hl.exe!01d9c054()

I donno if this helps....i'm dumbfounded though

Cheers

Q

souldog
September 25th, 2003, 12:23 AM
Lets push the check back (By the way you are linking to MFC as A DLL?)


if(!AfxSocketInit(NULL))
AfxMessageBox("AfxSocketInit FAILED");

Quasimoto
September 25th, 2003, 01:06 AM
Still dying at the same place =(

yeah i'm using a dll....what i'm trying to do is get filesharing working in a game...hence the need for the dll.....though if there's a way to get around this ... well that'd be good

Q

souldog
September 25th, 2003, 03:39 AM
It seems that your problem is not with the sockets, but
that you are doing this in a DLL. I think you may have problems
with how you set up your DLL or how you are linking to it.

take a look at


AfxGetInstanceHandle
HINSTANCE AfxGetInstanceHandle( );

Return Value

An HINSTANCE to the current instance of the application. If called from within a DLL linked with the USRDLL version of MFC, an HINSTANCE to the DLL is returned.

Remarks

This function allows you to retrieve the instance handle of the current application. AfxGetInstanceHandle always returns the HINSTANCE of your executable file (.EXE) unless it is called from within a DLL linked with the USRDLL version of MFC. In this case, it returns an HINSTANCE to the DLL.

Quasimoto
September 25th, 2003, 08:57 PM
I don't want to seem dense....so please excuse me if i do....but i don't see how this can help. The way i understand it at the moment the problem is something to do whth the socketClient.Create() being called from within a dll...is this right? is there any way to get around it? would i be better off making the socket stuff in a different solution, creating an exe and calling the exe from the dll?

Thanks for all the help

Q

Joseph_R_Thomas
September 25th, 2003, 09:18 PM
Mr.Souldog has taught me to use sockets...and i am ok with it now..and can create NORAML connections....
i'd love to help ya with the skill given to me by Mr.Souldog...but i dunno how come u have a dll in ur sockets...i've never touched a dll...so can't help ya....:(

MikeAThon
September 25th, 2003, 10:01 PM
Originally posted by Quasimoto
I don't want to seem dense....so please excuse me if i do....but i don't see how this can help. The way i understand it at the moment the problem is something to do whth the socketClient.Create() being called from within a dll...is this right? is there any way to get around it? would i be better off making the socket stuff in a different solution, creating an exe and calling the exe from the dll?

Thanks for all the help

Q

I think that what souldog is saying, is that if you are using CSocket in a dll, then you should expect errors on the AfxGetInstanceHanlde() calls since they return the handle of the dll and not the app.

Try inserting the following function at the top of your GettingFile() function

AFX_MANAGE_STATE(AfxGetStaticModuleState( ));


According to MSDN, this should set the module state to that of the static app, and should cause AfxGetInstanceHandle() to return the handle expected by functions down the line from CSocket::Create(). See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_afx_manage_state.asp

-Mike

souldog
September 25th, 2003, 10:23 PM
I am not aware of there being a problem with using CSocket in a DLL. However, I have never tried to do it
Your code is failing when an attempt is made to create a CSocketWnd for the windows messages associated with an
AsyncSelect socket.

Create calls Bind which calls CASyncSocket::AttachHandle

this function contains the code

CSocketWnd* pWnd = new CSocketWnd;
pWnd->m_hWnd = NULL;
if (!pWnd->CreateEx(0, AfxRegisterWndClass(0),
_T("Socket Notification Sink"),
WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL))


notice the call to AfxRegisterWndClass. This in turn contains the code

// generate a synthetic name for this class
HINSTANCE hInst = AfxGetInstanceHandle();
if (hCursor == NULL && hbrBackground == NULL && hIcon == NULL)
wsprintf(lpszName, _T("Afx:%x:%x"), (UINT)hInst, nClassStyle);
else
wsprintf(lpszName, _T("Afx:%x:%x:%x:%x:%x"), (UINT)hInst, nClassStyle,
(UINT)hCursor, (UINT)hbrBackground, (UINT)hIcon);



So it looks like your application is failing when it Ties to create a name for registering the windows class for the CSocketWnd.

My guess is that you have not linked to MFC correctly in your DLL.

Quasimoto
September 28th, 2003, 09:17 PM
urghh ok....so my problem would be fixed by simply selecting
Use MFC in a Shared DLL in the general tab right? i can't believe i overlooked that.

Once i've done that i start getting linking errors between Libcmtd.lib and MSVCRTD.lib no matter which order they are read in. A few examples...

MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _atoi already defined in Libcmtd.lib(atox.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _sprintf already defined in Libcmtd.lib(sprintf.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _isspace already defined in Libcmtd.lib(_ctype.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _isprint already defined in Libcmtd.lib(_ctype.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: __CrtDbgReport already defined in Libcmtd.lib(dbgrpt.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _free already defined in Libcmtd.lib(dbgheap.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _malloc already defined in Libcmtd.lib(dbgheap.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _wcscpy already defined in Libcmtd.lib(wcscat.obj)
MSVCRTD.lib(MSVCR70D.dll) : error LNK2005: _strncmp already defined in Libcmtd.lib(strncmp.obj)

I thought all the 2005 errors could be fixed by forcing the libraries into the correct order.

Any thoughts?

Q

souldog
September 29th, 2003, 01:57 AM
Have you tried MikeAThon's suggestion?

Quasimoto
September 29th, 2003, 05:42 AM
yeah, no difference there....these latest errors are at compile time

Tomcat
September 29th, 2003, 06:45 AM
Just tried it, and your code works in VC++ 6.0 with MFC in a shared library, although it's in desperate need of some error checking. :D

Originally posted by Quasimoto
yeah i'm using a dll....what i'm trying to do is get filesharing working in a game...hence the need for the dll.....
Just to make things clear: Is your project (that contains the code you posted) producing a DLL or an EXE file?

Quasimoto
September 29th, 2003, 07:19 AM
it's producing a dll....unfortunatly most of it isn't my code. I'm adding this stuff to someone elses code....making things more difficult. =(

dirlee
October 12th, 2005, 11:47 PM
seems we have the same problem.
I build a MFC Dll project that use csocket,it works find when the VC application calls it .
But it will display error message when C++ builder calls it,
In the dll debug mode,it also crash at the line csocket.create(),

if skip the error,the problem can still go on ,send date,receive date and so on.

someone can provide any idea to prevent the error message show?

Marc G
October 13th, 2005, 02:49 AM
[ moved thread ]

MikeAThon
October 13th, 2005, 11:10 AM
You're answering a post that's more than two years old.

Please start a new thread, explaining your problem in more detail, and why the solution of this thread did not work.

Mike

//JP added flex table