Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding CSocket functionality

Status
Not open for further replies.

redoctober

Programmer
Oct 25, 2000
37
CA
Hi there,
I have to add network functionaly to the existing project which was originally created without Windows Sockets support. I've programmed networking before, but it seems that that some settings or something being turned on when you create project with win socket support. I'm having hard time figuring out what i have to do with the project so it would start understanding CSockets. What i've done so far to add CSocket is:
added line #include <afxsock.h> to Stdafx.h file
and
added Windows sockets component from Project->Add to Project->Components and Controls->Windows sockets

But it still seems to be not enough. Program compiles, but when i run it, it crashes when i try to create CSocketFile (line 3):
1. m_CSoc = new CClientSocket();
2. m_CSoc->Connect(myip, myport);
3. file = new CSocketFile(m_CSoc);
4. m_CSoc->arIn = new CArchive(file,CArchive::load);
5. m_CSoc->arOut = new CArchive(file,CArchive::store);

Exact same code worked fine in another program.
Can anyone see if i'm missing any initialization?
thanks for any help.
 
Sorry, guys. Disregard the question. Stupid me forgot one more line of code. It should look like this:

1. m_CSoc = new CClientSocket();
2. m_Csoc->Create();
3. m_CSoc->Connect(myip, myport);
4. file = new CSocketFile(m_CSoc);
5. m_CSoc->arIn = new CArchive(file,CArchive::load);
6. m_CSoc->arOut = new CArchive(file,CArchive::store);

Now it works.
 
Are you sure you have initialized sockets with the DLL before trying to use sockets?

Here's some code that does this:

[tt]WSADATA wsaData;
WORD wVersion = MAKEWORD(1, 1);
WSAStartup(wVersion, &wsaData);[/tt]

When you have finished with the sockets you need to call:

[tt]WSACleanup()[/tt]

tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
When you add Windows sockets component to the project it adds the following code to the BOOL CTestApp::InitInstance()

if (!AfxSocketInit())
{
AfxMessageBox(CG_IDS_SOCKETS_INIT_FAILED);
return FALSE;
}

I would assume all necessary initialization with dll are done there. But it's only a guess. In any case my program works now, so i'd say it's a reasonable guess :)
thanks for the suggestion, though.
 
I would have thought so but I can never get sockets to work unless I specifically add the init code - even when I add the socket component to a new project.

[cry]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top