Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 6th, 2006, 8:20 PM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
.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..
hbe02 is offline   Reply With Quote
Old Oct 7th, 2006, 12:00 PM   #2
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 4 MBirchmeier is on a distinguished road
Generally what I do is a new thread is created for a listener, which updates a buffer. That buffer has been made (via reference) to both the listening thread and the main thread. This means that wherever you are in the main thread you have access to the latest value in the string. (Note: You must either use a threadsafe class or make usage of mutexes.)

Using a timer won't help, because you're still hanging on the recieve, which means your thread will generally be stuck in the 'timer' code while not recieving.

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 2:35 AM
URL class Eric the Red Java 5 Jun 24th, 2006 10:01 PM
New to OO need advice to create the classes weeb0 C++ 15 Jan 31st, 2006 8:49 PM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 8:40 AM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 4:41 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC