![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#11 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
As part of my first sockets program.. i created a sockets class.. where you can create a socket.. and send or recieve through it...
Please feel free to give any feedback or suggestions.. #ifndef SOCKET_H
#define SOCKET_H
#include<WinSock2.h>
#include<iostream>
using namespace std;
class Mysocket{
private:
void WinConnect();
void GetInfo(sockaddr_in &, int); //assign port and automatically obtain address
public:
int sd; //socket descriptor
Mysocket(sockaddr_in & , int); //constructor
void Bind(sockaddr_in &); //Bind host to socket
void Close();
void Recieve(sockaddr_in & , char * &); //Recieve from a client
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); //creating socket and setting the descriptor
if (sd == INVALID_SOCKET)
{
cout<< "ERROR: Could Not Create Socket."<<endl;
WSACleanup();
exit(0);
}
GetInfo(host , port); //assign port and automatically obtain address
}
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)); //clears out our host struct
host.sin_family = AF_INET; //set host's family.. *as socket*
host.sin_port = htons(port); // set server's port number
char * host_name = new char[50];
gethostname(host_name, strlen(host_name)); //obtains computer's hostname
hostent * hp; //hp is a struct of type hostent which contains address returned by gethostbyname()
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]; //assigning address
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)
{
//bind server adress to the socket created
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); //listen to created socket for connection (1 connection max)
accept(sd,0,0); //accept connection on socket
char* temp = new char[25000];
int clen = sizeof (sockaddr_in);
int size = recvfrom(sd,temp,strlen(temp),0,(struct sockaddr *) &client,&clen); //get recieved input and put in temp
for (int i = 0 ; i <size ; i++)
buffer[i] = temp[i];
buffer[i] = NULL;
delete temp;
}
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)); //clears out our host struct
sendhost.sin_family = AF_INET; //set host's family.. *as socket*
sendhost.sin_port = htons(port); // set server's port number
hostent * hp; //hp is a struct of type hostent which contains address returned by gethostbyname()
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]; //assigning address
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);
}
}
#endif |
|
|
|
| Bookmarks |