Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (124 sloc) 3.088 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
// ===================================
// File : CWAbsTcpSocket.cpp
// author : MARTINS Steven
// mail : steven.martins@epitech.eu
// created : jeudi 16 décembre 2010
// LastEdit : jeudi 16 décembre 2010
// ===================================

#include "CWAbsTcpSocket.h"
#include <iostream>

#ifdef _WIN32

CWAbsTcpSocket::CWAbsTcpSocket(void) : _socket(-1)
{
}


CWAbsTcpSocket::~CWAbsTcpSocket(void)
{
}

void CWAbsTcpSocket::Connect(std::string ip, unsigned short port)
{
if (WSAStartup(MAKEWORD(2, 0), &_wsadata) != NO_ERROR)
throw "Probleme avec WSAStartup";
_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
if (_socket == INVALID_SOCKET)
throw "Impossible de creer une socket";
SOCKADDR_IN sin;
sin.sin_addr.s_addr = inet_addr(ip.c_str());
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
if (connect(_socket, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
std::cout << "connect :" << WSAGetLastError() << std::endl;
throw "Probleme avec Connect";
}
}

void CWAbsTcpSocket::Listen(unsigned short port)
{
if (WSAStartup(MAKEWORD(2, 0), &_wsadata) != NO_ERROR)
throw "Probleme avec WSAStartup";
_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
if (_socket == INVALID_SOCKET)
throw "Impossible de creer une socket";
SOCKADDR_IN sin;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
if (bind(_socket, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
std::cout << "bind :" << WSAGetLastError() << std::endl;
throw "Probleme avec Bind";
}
if (listen(_socket, 10) == SOCKET_ERROR)
{
std::cout << "listen :" << WSAGetLastError() << std::endl;
throw "Probleme avec listen";
}


std::cout << "Socket val : " << _socket << std::endl;
}

SOCKET *CWAbsTcpSocket::getSocket()
{
return &_socket;
}

bool CWAbsTcpSocket::isValid() const
{
if (_socket > 0)
return true;
return false;
}

int CWAbsTcpSocket::Read(char *buf, int len)
{
if (_socket < 0)
return -1;
return recv(_socket, buf, len, 0);
}

void CWAbsTcpSocket::Write(const char *buf, int len)
{
if (_socket < 0)
return ;
if (send(_socket, buf, len, 0) < 0)
_socket = -1;
}

IAbsTcpSocket *CWAbsTcpSocket::accept()
{
int len = sizeof(_addr);

if (_socket < 0)
return NULL;
SOCKET soock = ::accept(_socket, (sockaddr *)&_addr, &len);
if (soock < 0)
{
OutputDebugString(TEXT("Problem with accept"));
return NULL;
}
std::cout << "Accept from :" << inet_ntoa(_addr.sin_addr) << std::endl;

CWAbsTcpSocket *sock = new CWAbsTcpSocket();
sock->_socket = soock;
sock->setIp(inet_ntoa(_addr.sin_addr));

//SO_NOSIGPIPE
return sock;
}

void CWAbsTcpSocket::close()
{
if (_socket < 0)
return ;
//shutdown(_socket, SD_BOTH);
closesocket(_socket);
_socket = -1;
// WSACleanup();
}

unsigned int CWAbsTcpSocket::getBytesAvailables()
{
u_long avail = -1;

if (_socket < 0)
return 0;
ioctlsocket(_socket, FIONREAD, &avail);

return (unsigned int)avail;
}

const std::string &CWAbsTcpSocket::getIp() const
{
return (_ip);
}

void CWAbsTcpSocket::setIp(char *ip)
{
_ip.clear();
_ip.assign(ip);
}

#endif
Something went wrong with that request. Please try again.