|
Professional Programmer
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 
|
Quote:
|
Originally Posted by hbe02
The link where i learnt sockets programming said :
So why did they say that if i can use sockets without salling the WSAStartup..?
Wont i need the close function to safely close the socket? or when u terminate the program the socket is safely closed automatically.. like a destructor type of thing?
|
Yes, use wsastartup but don't wrap it in another function which is used to simply call the function. That's unnecessary and a waste of precious lines. Remove the WinConnect() function and move the contents of it into one of your more valuable functions, like the constructor for instance, since it will always be called with the constructor.
#include <iostream>
#include <winsock2.h>
using namespace std;
class Mysocket
{
private:
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)
{
WSADATA w;
if (WSAStartup(0x0101, &w) != 0)
{
cout<< "ERROR: Could Not Open Windows connection."<<endl;
exit(0);
}
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::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;
Close();
}
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;
Close();
}
}
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];
buffer[i] = '\0'; // A char array can't be "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));
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;
Close();
}
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;
Close();
}
} This is a little better than what your previous code. The "NULLing" of your character array, removal of non-needed functions, and one of your for loops is now a block(it wasn't before and compilation had errors).
__________________
▄▄▄▄
Quote:
|
Originally Posted by Mohamed Jihad
Durka durka!
|
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.
Download Code::Blocks now!
▄▄▄▄
|