Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CSocket.Create() Fails

153 views
Skip to first unread message

Amit Batra

unread,
Dec 8, 1999, 3:00:00 AM12/8/99
to
Hi Everybody,

I have developed my first Windows sockets program using MFC classes
CAsyncSocket and CSocket classes. I have been programming sockets for a long
time in UNIX, but still I am unable to solve this problem. The call
CAsyncSocket.Create () and CSocket.Create () fail no matter what argument I
give to them. I am using WinNT 4.0 workstation with fix pack 5. I have tried
many values for the port numbers (above 1024) and even tried using the
default arguments, but nothing works.

Here is my program. Anyone wishing to test it, please copy/paste and try:

/*
Program coded in Programming Language Visual C++ by Amit Batra
to act as an Echo Server for the Echo Client. Demonstrates the
usage of the classes: CSocket, CSocketFile and CArchive.
*/

#include <afxsock.h>
#include <iostream.h>

#define SERVER_PORT 2000U
#define BUFFER_SIZE 256

int main (void)
{
CSocket serverSocket;

if (! serverSocket.Create (SERVER_PORT))
{
cerr << "Unable to Create a Socket on Port Number " << SERVER_PORT << '.'
<< endl;
cerr << "Error Number: " << serverSocket.GetLastError () << endl;
return 1;
}

if (! serverSocket.Listen ())
{
cerr << "Unable to Listen on Port Number " << SERVER_PORT << '.' << endl;
cerr << "Error Number: " << serverSocket.GetLastError () << endl;
serverSocket.Close ();
return 1;
}

CSocket anotherSocket;

if (! serverSocket.Accept (anotherSocket))
{
cerr << "Unable to Accept on Port Number " << SERVER_PORT << '.' << endl;
cerr << "Error Number: " << serverSocket.GetLastError () << endl;
serverSocket.Close ();
return 1;
}

CSocketFile file (&anotherSocket);
CArchive* inArchive;
CArchive* outArchive;

try
{
inArchive = new CArchive (&file, CArchive::load);
outArchive = new CArchive (&file, CArchive::store);
}
catch (CException& exception)
{
char errorMessage [256];

exception.GetErrorMessage (errorMessage, 256U);

cerr << "Unable to Create Archive" << endl;
cerr << errorMessage << endl;

exception.Delete ();
return 1;
}

CArchive& archiveIn = *inArchive;
CArchive& archiveOut = *outArchive;

int numBytes = 0;
char* buffer = new char [numBytes];

do
{
archiveIn >> numBytes;
if (numBytes > 0)
{
delete [] buffer;
buffer = new char [numBytes + 1];
archiveIn.ReadString (buffer, numBytes + 1);
archiveOut << buffer;
}
} while (numBytes > 0);

delete [] buffer;

anotherSocket.Close ();
serverSocket.Close ();

return 0;
}

Thanks in advance.

Amit

Georges Schumacher

unread,
Dec 8, 1999, 3:00:00 AM12/8/99
to
Hi

I never used the MFC socket classes, but I know that in Windows, before
using the sockets, you have to call the WSAStartup function. I guess that
you have to call the function AfxSocketInit for the same reason with the MFC.

Hope this helps.

Regards.

Georges Schumacher

Amit Batra

unread,
Dec 9, 1999, 3:00:00 AM12/9/99
to
Hi Georges,

Thanks for your prompt advice.I tried using AfxSocketInit () as you
instructed. But the problem is that mine is a CUI based application.
Therefore inside AfxSocketInit (), the assertion
ASSERT(afxCurrentInstanceHandle != NULL) causes my application to crash. How
do I work around this problem. My program starts like:

int main (void)
{
if (! AfxSocketInit ())
{
cerr << "Unable to Initialize the Socket Library" << endl
<< "Terminating..." << endl;
return 1;
}
...
...
...
}
Amit

Georges Schumacher wrote in message <384E0AF4...@ilog.fr>...

0 new messages