#using <mscorlib.dll>
#using <System.dll>
#include "stdlib.h"
#include "stdio.h"
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
void Connect(String* server, Int32 port, String* message)
{
try {
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
//Int32 port = 13000;
TcpClient* client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte
array.
Byte data[] = Text::Encoding::ASCII->GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client->GetStream();
NetworkStream* stream = client->GetStream();
// Send the message to the connected TcpServer.
stream->Write(data, 0, data->Length);
Console::WriteLine(S"Sent: {0}", message);
// Receive the TcpServer::response.
// Buffer to store the response bytes. data = new Byte[256];
// String to store the response ASCII representation.
String* responseData = String::Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream->Read(data, 0, data->Length);
responseData = Text::Encoding::ASCII->GetString(data, 0, bytes);
Console::WriteLine(S"Received (Response Data): {0}", responseData);
// Close everything.
client->Close();
} catch (ArgumentNullException* e) {
Console::WriteLine(S"ArgumentNullException: {0}", e);
} catch (SocketException* e) {
Console::WriteLine(S"SocketException: {0}", e);
}
Console::WriteLine(S"\nSocket closed!");
Console::WriteLine(S"\n Press Enter to EXIT.");
Console::Read();
}
int main()
{
system("TITLE Client Conection System.");
String* NameServer;
Console::WriteLine(S"Name of Server to conect to: ");
String __gc * input = Console::ReadLine();
NameServer = input->ToString(0);
redo:
Int32 InPort;
Console::WriteLine(S"\nPort (Usal port is 1300): ");
String __gc * input1 = Console::ReadLine();
InPort = input1->ToInt32(0);
if(InPort=0)
{
Console::WriteLine(S"\nERROR: Incorect port number!");
goto redo;
}
String* InMessage;
Console::WriteLine(S"\nMessage to send: ");
String __gc * input2 = Console::ReadLine();
InMessage = input2->ToString(0);
Connect(NameServer, InPort, InMessage);
return 0;
}
/* You have to include -clr in the compiler option to compile this.
THX!
*/
--
Message posted via http://www.dotnetmonster.com