M
Marty
Hi,
I'm still playing with this managed/unmanaged code within my VC++
project. I'm sure it's gonna work, need just some tuning that you could
help me to find out.
Here is below the .h and .cpp I use.
socketListener.cpp configuration properties:
c/c++ - General : /clr
c/c++ - Precompiled headers : Not using precompiled headers.
project property page:
General - Use managed extensions : No
c/c++ - Precompiled headers : Automatically Generate (/YX)
Then I include my socketListener.h in the main .cpp file:
#include "stdafx.h"
#include "socketListener.h"
At this time, I did not instantiate the class yet.
Even with all those settings, I still get the error :
fatal error C1190: managed targeted code requires '#using
<mscorlib.dll>' and '/clr' option
What can I do more?
Marty
socketListener.h
-----------------------------------------------
#ifndef CLS_SOCKET_LISTENER
#define CLS_SOCKET_LISTENER
#endif
#using <mscorlib.dll>
#using <System.dll>
#include <vcclr.h>
using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
class socketListener
{
public:
socketListener();
socketListener(int);
~socketListener();
void startListening(String*, int);
private:
int intPort;
};
--------------------------------------------
socketListener.cpp
----------------------------------------------
#include "socketListener.h"
#pragma managed
socketListener::socketListener()
{}
#pragma managed
socketListener::socketListener(int intPortTemp)
{
intPort = intPortTemp;
}
#pragma managed
socketListener::~socketListener()
{}
#pragma managed
void socketListener::startListening(String* strIP, int intPort)
{
try
{
// Set the TcpListener on port 13000.
int port = intPort;
IPAddress* localAddr = IPAddress:
arse(strIP); //IPAddress*
localAddr = IPAddress:
arse(S"127.0.0.1");
// TcpListener* server = new TcpListener(port);
TcpListener* server = new TcpListener(localAddr, port);
// Start listening for client requests.
server->Start();
// Buffer for reading data
Byte bytes[] = new Byte[256];
String* data = 0;
// Enter the listening loop.
while (true) {
Console::Write(S"Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient* client = server->AcceptTcpClient();
Console::WriteLine(S"Connected!");
data = 0;
// Get a stream Object* for reading and writing
NetworkStream* stream = client->GetStream();
Int32 i;
// Loop to receive all the data sent by the client.
while (i = stream->Read(bytes, 0, bytes->Length)) {
// Translate data bytes to a ASCII String*.
data = Text::Encoding::ASCII->GetString(bytes, 0, i);
Console::WriteLine(String::Format(S"Received: {0}", data));
// Process the data sent by the client.
data = data->ToUpper();
Byte msg[] = Text::Encoding::ASCII->GetBytes(data);
// Send back a response.
stream->Write(msg, 0, msg->Length);
Console::WriteLine(String::Format(S"Sent: {0}", data));
}
// Shutdown and end connection
client->Close();
}
}
catch (SocketException* e)
{
Console::WriteLine(S"SocketException: {0}", e);
}
Console::WriteLine(S"\nHit enter to continue...");
Console::Read();
}
-----------------------------------------------
I'm still playing with this managed/unmanaged code within my VC++
project. I'm sure it's gonna work, need just some tuning that you could
help me to find out.
Here is below the .h and .cpp I use.
socketListener.cpp configuration properties:
c/c++ - General : /clr
c/c++ - Precompiled headers : Not using precompiled headers.
project property page:
General - Use managed extensions : No
c/c++ - Precompiled headers : Automatically Generate (/YX)
Then I include my socketListener.h in the main .cpp file:
#include "stdafx.h"
#include "socketListener.h"
At this time, I did not instantiate the class yet.
Even with all those settings, I still get the error :
fatal error C1190: managed targeted code requires '#using
<mscorlib.dll>' and '/clr' option
What can I do more?
Marty
socketListener.h
-----------------------------------------------
#ifndef CLS_SOCKET_LISTENER
#define CLS_SOCKET_LISTENER
#endif
#using <mscorlib.dll>
#using <System.dll>
#include <vcclr.h>
using namespace System;
using namespace System::Net;
using namespace System::Net::Sockets;
class socketListener
{
public:
socketListener();
socketListener(int);
~socketListener();
void startListening(String*, int);
private:
int intPort;
};
--------------------------------------------
socketListener.cpp
----------------------------------------------
#include "socketListener.h"
#pragma managed
socketListener::socketListener()
{}
#pragma managed
socketListener::socketListener(int intPortTemp)
{
intPort = intPortTemp;
}
#pragma managed
socketListener::~socketListener()
{}
#pragma managed
void socketListener::startListening(String* strIP, int intPort)
{
try
{
// Set the TcpListener on port 13000.
int port = intPort;
IPAddress* localAddr = IPAddress:

localAddr = IPAddress:

// TcpListener* server = new TcpListener(port);
TcpListener* server = new TcpListener(localAddr, port);
// Start listening for client requests.
server->Start();
// Buffer for reading data
Byte bytes[] = new Byte[256];
String* data = 0;
// Enter the listening loop.
while (true) {
Console::Write(S"Waiting for a connection... ");
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient* client = server->AcceptTcpClient();
Console::WriteLine(S"Connected!");
data = 0;
// Get a stream Object* for reading and writing
NetworkStream* stream = client->GetStream();
Int32 i;
// Loop to receive all the data sent by the client.
while (i = stream->Read(bytes, 0, bytes->Length)) {
// Translate data bytes to a ASCII String*.
data = Text::Encoding::ASCII->GetString(bytes, 0, i);
Console::WriteLine(String::Format(S"Received: {0}", data));
// Process the data sent by the client.
data = data->ToUpper();
Byte msg[] = Text::Encoding::ASCII->GetBytes(data);
// Send back a response.
stream->Write(msg, 0, msg->Length);
Console::WriteLine(String::Format(S"Sent: {0}", data));
}
// Shutdown and end connection
client->Close();
}
}
catch (SocketException* e)
{
Console::WriteLine(S"SocketException: {0}", e);
}
Console::WriteLine(S"\nHit enter to continue...");
Console::Read();
}
-----------------------------------------------