| hbe02 |
Oct 6th, 2006 8:20 PM |
.NET Socket Class
This is not much of a project, rather a completed part of other project to be used freely later on.
i have simple created a Class NetSocket() which handles Simple Client server Socket Communication in .NET .. this is first time i work with it..
One thing that could be up for debate, is with my recieving function.
what do you think about the code in comments? what i was trying to do is keep recieving until no more, but it didnt work since the PC just hangs in the loop if the other host/client/server doesnt send any data.. i guess a timer would solve the problem.. is what i tried to with recieving data good?
Please feel free to comment and critique.
here is the my NetSocket Class
:
#pragma once
#using <mscorlib.dll>
#using <System.dll>
#include <stdlib.h>
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
public __gc class NetSocket
{
private:
Socket * s;
Byte SendBytes[];
Byte RecvBytes[];
Int32 Port;
Encoding *ASCII;
public:
NetSocket(Int32 ); //Constructor takes port as parameter and creates socket
NetSocket(Int32,Socket*);//Constructor takes port and accepted socket
bool Bind(); //bind server socket to a port
void Listen(Int32); //Listen For incoming Connections
Socket * Accept(); //accept incoming client connections, returns socket*
bool Connect(String*); //connect take IP address as a parameter to connect to
void Send(String * ); //sends a string*
String * Recieve(); //recieves and returns a string*
void Close(); //closes Socket
};
NetSocket::NetSocket(Int32 port):Port(port) //Constructor assigns port and creates socket
{
try
{
s = new Socket(AddressFamily::InterNetwork, SocketType::Stream, ProtocolType::Tcp );
SendBytes = new Byte[1024];
RecvBytes = new Byte[1024];
ASCII = Encoding::ASCII;
}
catch(...)
{
}
}
NetSocket::NetSocket(Int32 port, Socket * sd):Port(port) //Constructor assigns port and creates socket
{
try
{
s = sd; // this is used when server recieves form client
SendBytes = new Byte[1024];
RecvBytes = new Byte[1024];
ASCII = Encoding::ASCII;
}
catch(...)
{}
}
bool NetSocket::Bind()
{
try
{
IPAddress *PCAddress = NULL;
PCAddress = PCAddress->Any; //Set IP address to any, its okay when binding
IPEndPoint *PCEndPoint = new IPEndPoint(PCAddress, Port);//Set Host EndPoint by IP and Port
s->Bind(PCEndPoint);
}
catch (...)
{
return false;
}
return true;
}
void NetSocket::Listen(Int32 UserCount)
{
s->Listen(UserCount);
}
Socket * NetSocket::Accept()
{
return s->Accept();
}
bool NetSocket::Connect(String * ServerIP)
{
try
{
IPEndPoint *ServerEndPoint;
IPAddress *ServerAddress = NULL;
IPHostEntry *ServerInfo = Dns::GetHostByAddress(ServerIP);
ServerAddress = dynamic_cast<IPAddress *>(ServerInfo->AddressList->get_Item(0)); //Set host IP Address
ServerEndPoint = new IPEndPoint(ServerAddress, Port);//Set Host EndPoint by IP and Port
s->Connect(ServerEndPoint); //Connect to Host/Server
}
catch(...)
{
return false;
}
if (!s->Connected)
return false;
return true;
}
void NetSocket::Send(String * SendData)
{
try
{
SendBytes = ASCII->GetBytes(SendData); //Convert to Bytes in ASCII
s->Send(SendBytes, SendBytes->Length, SocketFlags::None);
}
catch(...)
{}
}
String * NetSocket::Recieve()
{
String * RecvData ;
try
{
Int32 RecvSize = s->Receive(RecvBytes, RecvBytes->Length, SocketFlags::None);
RecvData = ASCII->GetString(RecvBytes, 0, RecvSize);
//while (RecvSize > 0)
//{
// RecvSize = s->Receive(RecvBytes, RecvBytes->Length, SocketFlags::None);
// RecvData = String::Concat(RecvData,ASCII->GetString(RecvBytes, 0, RecvSize));
//}
}
catch(...)
{}
return RecvData;
}
void NetSocket::Close()
{
s->Close();
}
AND here is a sample server
:
#using <mscorlib.dll>
#using <System.dll>
#include <stdlib.h>
#include "C:\Documents and Settings\HiDo\Desktop\AUB\Programming Related\Visual Studio Projects\NETSocket\NetSocket.h"
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
int main()
{
Console::WriteLine(S"Testing NetSocket Class");
NetSocket __gc * com = new NetSocket(80);
com->Bind();
com->Listen(10);
Socket * ClientSocket = com->Accept();
NetSocket __gc * incoming = new NetSocket(80,ClientSocket); //create client socket to communicate with client
Console::WriteLine(S"RECIEVING");
Console::WriteLine(incoming->Recieve());
incoming->Send(S"HELLO CLIENT");
incoming->Close();
return 0;
}
AND here is a sample client
:
#using <mscorlib.dll>
#using <System.dll>
#include <stdlib.h>
#include "C:\Documents and Settings\HiDo\Desktop\AUB\Programming Related\Visual Studio Projects\NETSocket\NetSocket.h"
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
int main()
{
Console::WriteLine(S"Testing NetSocket Class");
NetSocket __gc * com = new NetSocket(80);
if (com->Connect("192.168.141.107"))
{
com->Send(S"HI THERE");
Console::WriteLine(S"****RECIEVED****");
Console::WriteLine(com->Recieve());
com->Close();
}
return 0;
}
hope you find it nice simple and pretty..
|