I made a server and i tryed connecting using a telnet saying "Connection refused" . What am I doing wrong ? Thanks in advanced.
/*
* server2.cc
*
* Created on: Jan 29, 2011
* Author: astanciu
*/
#include <iostream>
#include <winsock2.h>
using namespace std;
class Server {
WSAData wsadata;
int desc;
int remoteSocket;
SOCKADDR_IN server;
public:
Server()
{
if(WSAStartup( MAKEWORD( 1, 1 ), &wsadata ) != 0 )
{
cout << "Error creating socket" << endl;
exit(1);
}
wsadata.wVersion = 5;
}
void Initialize()
{
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY; // Where to start server?
server.sin_port = htons(9898); // Port
}
void Socket()
{
desc = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
void Bind()
{
::bind(desc, (LPSOCKADDR)&server, sizeof(server));
}
void Listen()
{
::listen(desc, 5);
}
void Accept()
{
SOCKADDR_IN sock;
int intsock = sizeof(sock);
remoteSocket = ::accept(desc, (LPSOCKADDR)&sock, &intsock);
HandleConnection(desc, sock);
}
void Send(const char* buffer)
{
::send(remoteSocket, buffer, sizeof(buffer), 0);
}
void Recv(char* buffer)
{
::recv(remoteSocket, buffer, sizeof(buffer), 0);
}
void HandleConnection(int descriptor, SOCKADDR_IN &sockadress)
{
cout << "You are connected !!!" << endl;
}
~Server()
{
WSACleanup();
}
};
int main()
{
Server s;
s.Initialize();
s.Socket();
s.Bind();
s.Listen();
while(1)
{
s.Accept();
}
return 0;
}