Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

winsock help

exgeexge Member Posts: 3
i made a class to try to wrap winsock functions .. but i have some problems

[code]
class EXGELIB_API connection
{
public:
~connection();
char *m_url;
unsigned long m_ip;
int m_port;
int m_bufSize;
SOCKET hSock,hClientSocket;
char m_tempBuffer[512];
sockaddr_in m_sockAddr;
char *m_buffer;

void Send(char *data,int bufSize);
void Recieve();
void Connect();
void SetIP(char *cIp,int port);
void SetURL(char *url,int port);
void ServerRecieve();
void StartServer(int port);
};


void CLASSWINSOCKLIB_API Init_Sockets()
{
const int minReq = 2;
WSADATA wsd;

if(WSAStartup(MAKEWORD(minReq,2),&wsd) == 0)
{
// Check if major version is at least iReqWinsockVer
if (LOBYTE(wsd.wVersion) >= minReq)
{
/* ------- Call winsock functions here ------- */
}
else
{
// Required version not available
}

// Cleanup winsock
if (WSACleanup()!=0)
{
// cleanup failed
}
}
else
{
//startup failed
error("Unable to initialize winsock");
}
}

void connection::StartServer(int port)
{
this->m_port = port;
// Set family, port and find IP
m_sockAddr.sin_family = AF_INET;
m_sockAddr.sin_port = htons(this->m_port);
m_sockAddr.sin_addr.S_un.S_addr = INADDR_ANY;

if ((hSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
error("could not create socket!");
}
if (bind(hSock, reinterpret_cast(&m_sockAddr), sizeof(m_sockAddr))!=0)
{
error("could not bind to socket");
}

if (listen(hSock, SOMAXCONN)!=0)
{
error("could not listen...");
}

sockaddr_in clientSockAddr;
int clientSockSize = sizeof(clientSockAddr);

// Accept connection:
hClientSocket = accept(hSock,reinterpret_cast(&clientSockAddr),&clientSockSize);

// Check if accept succeeded
if (hClientSocket==INVALID_SOCKET)
{
error("accept function failed");
}
}
[/code]

i always get INVALID_SOCKET error when i call socket();

pls help thx

Comments

  • anthrax11anthrax11 Member Posts: 511
    Try calling [link=http://msdn2.microsoft.com/en-us/library/ms741580(VS.85).aspx]WSAGetLastError[/link].
  • manucpmanucp Member Posts: 34
    In your code listing it is not clear if you are calling WSAStartup before the call to socket(...). You must ensure you are calling it.

    : i made a class to try to wrap winsock functions .. but i have some
    : problems
    :
    : [code]:
    : class EXGELIB_API connection
    : {
    : public:
    : ~connection();
    : char *m_url;
    : unsigned long m_ip;
    : int m_port;
    : int m_bufSize;
    : SOCKET hSock,hClientSocket;
    : char m_tempBuffer[512];
    : sockaddr_in m_sockAddr;
    : char *m_buffer;
    :
    : void Send(char *data,int bufSize);
    : void Recieve();
    : void Connect();
    : void SetIP(char *cIp,int port);
    : void SetURL(char *url,int port);
    : void ServerRecieve();
    : void StartServer(int port);
    : };
    :
    :
    : void CLASSWINSOCKLIB_API Init_Sockets()
    : {
    : const int minReq = 2;
    : WSADATA wsd;
    :
    : if(WSAStartup(MAKEWORD(minReq,2),&wsd) == 0)
    : {
    : // Check if major version is at least iReqWinsockVer
    : if (LOBYTE(wsd.wVersion) >= minReq)
    : {
    : /* ------- Call winsock functions here ------- */
    : }
    : else
    : {
    : // Required version not available
    : }
    :
    : // Cleanup winsock
    : if (WSACleanup()!=0)
    : {
    : // cleanup failed
    : }
    : }
    : else
    : {
    : //startup failed
    : error("Unable to initialize winsock");
    : }
    : }
    :
    : void connection::StartServer(int port)
    : {
    : this->m_port = port;
    : // Set family, port and find IP
    : m_sockAddr.sin_family = AF_INET;
    : m_sockAddr.sin_port = htons(this->m_port);
    : m_sockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
    :
    : if ((hSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
    : {
    : error("could not create socket!");
    : }
    : if (bind(hSock, reinterpret_cast(&m_sockAddr), sizeof(m_sockAddr))!=0)
    : {
    : error("could not bind to socket");
    : }
    :
    : if (listen(hSock, SOMAXCONN)!=0)
    : {
    : error("could not listen...");
    : }
    :
    : sockaddr_in clientSockAddr;
    : int clientSockSize = sizeof(clientSockAddr);
    :
    : // Accept connection:
    : hClientSocket = accept(hSock,reinterpret_cast(&clientSockAddr),&clientSockSize);
    :
    : // Check if accept succeeded
    : if (hClientSocket==INVALID_SOCKET)
    : {
    : error("accept function failed");
    : }
    : }
    : [/code]:
    :
    : i always get INVALID_SOCKET error when i call socket();
    :
    : pls help thx
Sign In or Register to comment.