Tuesday, September 9, 2008

Socket Wrapper class for c++

Most peoples on the internet are writing blog or article on such subject.

This is my first blog, so if i make any mistake then let me know about it.
What ever i am going to provide is a basic idea on writing wrapper class in c++.
Wrapper classes are those, which implements basic functionality in Object Oriented Structure. As name suggest, it is wrapper to some functionality.

In c, socket has some basic library functions using which we can write socket programming. Now below is my Socket class which has basic socket programming functionality has been implemented.



/*
* Socket.h
* Untitled
*
* Created by Manish Patel on 10/09/08.
*
*/

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

/**
* Class Socket
* Created by Manish Patel on 10/09/08.
*/

class Socket
{
private:
int m_Sock;
struct sockaddr_in m_SockAddr;

public:
Socket();
bool Create();
bool Bind(string address, int port);
bool Listen(int que);
bool Accept(Socket &clientSock);
bool Connect(string address, int port);
int Recieve(char *buff, int buffSize);
int Send(const char *buff, int len);
bool Close();
};


This is a Header file for my Socket Class. Now below is my implementation of all the methods of the Socket Class.


/*
* Socket.cpp
* Untitled
*
* Created by Manish Patel on 10/09/08.
* Copyright 2008 as. All rights reserved.
*
*/

#include "Socket.h"

Socket::Socket()
{
m_Sock = -1;
}

bool Socket::Create()
{
if((m_Sock = socket(AF_INET, SOCK_STREAM, 0)) > 0)
return true;
return false;
}

bool Socket::Bind(string address, int port)
{
m_SockAddr.sin_family = AF_INET;
m_SockAddr.sin_port = htons(port);
m_SockAddr.sin_addr.s_addr = inet_addr(address.c_str());

if(bind(m_Sock, (struct sockaddr *)&m_SockAddr, sizeof(struct sockaddr)) == 0)
return true;
return false;
}

bool Socket::Listen(int que)
{
if(listen(m_Sock, que) == 0)
return true;
return false;
}

bool Socket::Accept(Socket &clientSock)
{
int size = sizeof(struct sockaddr);
clientSock.m_Sock = accept(m_Sock,(struct sockaddr *)&clientSock.m_SockAddr, (socklen_t *)&size);
if(clientSock.m_Sock == -1)
return false;
return true;
}

bool Socket::Connect(string address, int port)
{

struct in_addr *addr_ptr;
struct hostent *hostPtr;
string add;
try
{
hostPtr = gethostbyname(address.c_str());
if(hostPtr == NULL)
return false;

// the first address in the list of host addresses
addr_ptr = (struct in_addr *)*hostPtr->h_addr_list;

// changed the address format to the Internet address in standard dot notation
add = inet_ntoa(*addr_ptr);
if(add == "")
return false;
}
catch(int e)
{
return false;
}

struct sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(port);
sockAddr.sin_addr.s_addr = inet_addr(add.c_str());
if(connect(m_Sock, (struct sockaddr *)&sockAddr, sizeof(struct sockaddr)) == 0)
return true;
return false;
}

int Socket::Recieve(char *buff, int buffLen)
{
return recv(m_Sock, buff, remBytes, 0);
}

int Socket::Send(const char *buff, int len)
{
return send(m_Sock, buff, len, 0);
}

bool Socket::Close()
{
if(m_Sock > 0)
{
close(m_Sock);
m_Sock = 0;
return true;
}
return false;
}


Let me know if you have any queries.

Manish Patel.
B.E.-Information Technology.