David S. Anderson

Operating system: Windows XP Home Ed. w/SP2
Technology: C++/CLR | .NET 2.0

I have created my own TcpListener class that is inherited from the TcpListener class from the System::Net::Sockets namespace. this class is contained in an assembly (type of dll).

Here is the code of my assembly:
// tcp.h

#pragma once

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Net;
using namespace System::Net::Sockets;

namespace Tcp {

public ref class Host : TcpListener
{
public:
property String ^Name;
property IPAddress ^IP;
property int Port;

Host(void) : TcpListener(IP, Port)
{
}
};
}

Now in the same solution I have another assembly (of type executable, and is a console application) that I am using to test my custom TcpListener class.

Here is the code:
// TcpTest.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace Tcp;

int main(array<System::String ^> ^args)
{
Host^ host = gcnew Host();
host->IP::set(IPAddress::Parse("127.0.0.1"));
host->Port::set(8767);
host->Start();
return 0;
}

Now when the Start() function is called, I get this error:
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: localaddr
at System.Net.Sockets.TcpListener..ctor(IPAddress localaddr, Int32 port)
at Tcp.Host..ctor() in c:\documents and settings\dave s. anderson\my document
s\visual studio 2005\projects\tcp\tcp\tcp.h:line 21
at main(String[] args) in c:\documents and settings\dave s. anderson\my docum
ents\visual studio 2005\projects\tcp\tcptest\tcptest.cpp:line 12
at mainCRTStartupStrArray(String[] arguments) in f:\rtm\vctools\crt_bld\self_
x86\crt\src\mcrtexe.cpp:line 324
Press any key to continue . . .

Now I know that this is because the values IP and Port are not being set properly in the object, so how can I do this within my custom Tcp class to simplify things on the client side (My goal is to make connections with Tcp easier with this assembly)



Re: Visual C++ General Custom TcpListener class problem.

Viorel.

Why do not add two arguments to the constructor of Host For example:

public ref class Host : TcpListener

{

public:

    property String ^ Name;

    property IPAddress ^ IP;

    property int Port;

 

    Host(String ^ name, int port)

        : TcpListener((IP = IPAddress::Parse(name), IP), port)

    {

        Name = name;

        Port = port;

    }

};

Usage:

Tcp::Host ^ host = gcnew Tcp::Host("127.0.0.1", 8767);

host->Start();

I hope this makes sense.

 





Re: Visual C++ General Custom TcpListener class problem.

Dav? S. A???????

Thank you, that helped me understand a little better too.




Re: Visual C++ General Custom TcpListener class problem.

Dav? S. A???????

I have another problem now. I got my class working the way I want to, and I have create my own custom ConnectEx() function inside the class. But when I call on it, I get a null reference exception.

*note that this is a TcpClient class and not a TcpListener class this time.

/////////////////////////////////////////////////////////////CLASS CODE////////////////////////////////////////////////////////////
// Tcp.h

#pragma once

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Runtime::InteropServices;

namespace Tcp {

[DllImport("User32.dll")]
extern int MessageBox(IntPtr ^hWnd, String ^text, String ^Caption, int Style);

public ref class Client : public TcpClient
{
public:
property String ^Hostname;
property int Port;

Client(String ^hostname, int port) : TcpClient(Hostname, Port)
{
Hostname = hostname;
Port = port;
}

bool ConnectEx()
{
bool success_state = false;

try
{
Client::Connect(Hostname, Port);

success_state = true;
return success_state;
}
catch (Exception ^ex)
{
String ^ErrorData = ex->InnerException->ToString();
MessageBox(nullptr, ErrorData, "Socket Error", 15);

success_state = false;
return success_state;
}

return success_state;
}
};
}

////////////////////////////////////////////////////////EXECUTABLE CODE/////////////////////////////////////////////////////
// TestExe.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace Tcp;

int main(array<System::String ^> ^args)
{
Client ^client = gcnew Client("127.0.0.1", 8767);
client->ConnectEx();
return 0;
}

///////////////////////////////////////////////////////////////ERROR DATA////////////////////////////////////////////////////////
Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: hostname
at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
at Tcp.Client..ctor(String hostname, Int32 port) in c:\documents and settings
\dave s. anderson\my documents\visual studio 2005\projects\tcp\tcp.h:line 24
at main(String[] args) in c:\documents and settings\dave s. anderson\my docum
ents\visual studio 2005\projects\testexe\testexe.cpp:line 10
at mainCRTStartupStrArray(String[] arguments) in f:\rtm\vctools\crt_bld\self_
x86\crt\src\mcrtexe.cpp:line 324
Press any key to continue . . .

Everything looks like it is correct and should work, so I am kind of lost here.





Re: Visual C++ General Custom TcpListener class problem.

Viorel.

In my opinion instead of this:

Client(String ^ hostname, int port) : TcpClient(Hostname, Port)

{

. . .

}

you should try this:

Client(String ^ hostname, int port) : TcpClient(hostname, port)

{

. . .

}

I hope it works now.





Re: Visual C++ General Custom TcpListener class problem.

Dav? S. A???????

I will try this when I get home from school, I will post back and tell you how it works out.