Thread: Socket Server
View Single Post
Old May 22nd, 2006, 7:07 PM   #17
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by hbe02
Please feel free to give any feedback or suggestions..
Have not looked at it extensively but I got to go to bed now (tired), Will help more tomorrow (if not solved by then).
#include <iostream>
#include <winsock2.h>

using namespace std;

class Mysocket
{

private:

	void WinConnect();
	void GetInfo(sockaddr_in &, int);

public:
	int sd;
	Mysocket(sockaddr_in & , int);
	void Bind(sockaddr_in &);
	void Close();
	void Recieve(sockaddr_in & , char * &);
	void Send(sockaddr_in & , char * &);
	void Connect(sockaddr_in & ,char * &,int);
};

Mysocket::Mysocket(sockaddr_in & host , int port)
{
	WinConnect();
	sd = socket(AF_INET, SOCK_DGRAM, 0);

	if (sd == INVALID_SOCKET)
	{
		cout << "ERROR: Could Not Create Socket." << endl;
		WSACleanup();
		exit(0);
	}
	GetInfo(host , port);
}

void Mysocket::WinConnect()
{
	WSADATA w;
	if (WSAStartup(0x0101, &w) != 0)
	{
		cout<< "ERROR: Could Not Open Windows connection."<<endl;
		exit(0);
	}
}

void Mysocket::GetInfo(sockaddr_in & host , int port)
{
	memset(&host, 0, sizeof(host));

	host.sin_family = AF_INET;
	host.sin_port = htons(port);

	char * host_name = new char[50];

	gethostname(host_name, strlen(host_name));

	hostent * hp;
	hp = gethostbyname(host_name);

	if (hp == NULL)
	{
		cout << "ERROR: Could Not Get Host Name." << endl;
		closesocket(sd);
		WSACleanup();
		exit(0);
	}
	cout<<"Host Name is: "<<host_name<<endl;

	host.sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0];
	host.sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1];
	host.sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2];
	host.sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];

	cout<<"Server Adress is: ";
	cout<<static_cast<int>(host.sin_addr.S_un.S_un_b.s_b1)<<".";
	cout<<static_cast<int>(host.sin_addr.S_un.S_un_b.s_b2)<<".";
	cout<<static_cast<int>(host.sin_addr.S_un.S_un_b.s_b3)<<".";
	cout<<static_cast<int>(host.sin_addr.S_un.S_un_b.s_b4)<<endl;
}

void Mysocket::Bind(sockaddr_in & host)
{
	if(bind(sd, (struct sockaddr *)&host, sizeof(struct sockaddr_in)) == -1)
	{
		cout << "ERROR: Could Not Bind To Socket."<< endl;
		WSACleanup();
		closesocket(sd);
		exit(0);
	}
}

void Mysocket::Close()
{
	cout << "Closing Socket with Descriptor " << sd << endl;
	WSACleanup();
	closesocket(sd);
	exit(0);
}

void Mysocket::Recieve(sockaddr_in & client, char * & buffer)
{
	cout<<"Listening on Socket "<<sd<<"...."<<endl;
	listen(sd,1);

	accept(sd,0,0);

	char * temp = new char[25000];
	int clen = sizeof (sockaddr_in);
	int size = 	recvfrom(sd,temp,strlen(temp),0,(struct sockaddr *) &client,&clen);

	for (int i = 0 ; i <size ; i++)
		buffer[i] = temp[i];

	// i is out of scope here
	// did you mean NULL or the null character '\0'?
	buffer[i] = NULL;

	delete [] temp; // add [] ?
}

void Mysocket::Send(sockaddr_in & host , char * & buffer)
{
	if(	sendto(sd, buffer, (int)strlen(buffer),0, (struct sockaddr *)&host, sizeof(struct sockaddr_in)) != -1)
	{
		cout<<"Message Sent Successfully" << endl;
	}
	else
	{
		cout << "ERROR: Message Send Failed" << endl;
	}
}

void Mysocket::Connect(sockaddr_in & sendhost , char * & host_name , int port )
{
	memset(&sendhost, 0, sizeof(sendhost));

	sendhost.sin_family = AF_INET;
	sendhost.sin_port = htons(port);

	hostent * hp;
	hp = gethostbyname(host_name);

	if (hp == NULL)
	{
		cout << "ERROR: Could Not Get Host Name." << endl;
		closesocket(sd);
		WSACleanup();
		exit(0);
	}

	sendhost.sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0];
	sendhost.sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1];
	sendhost.sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2];
	sendhost.sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];

	if (::connect(sd,(struct sockaddr *)&sendhost, sizeof (struct sockaddr_in)))
	{
		cout << "ERROR: Cannot Connect to Host" << endl;
		WSACleanup();
		closesocket(sd);
		exit(0);
	}
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote