Click to See Complete Forum and Search --> : Winsock problem


crazycheetah
July 29th, 2003, 08:00 PM
hi.
ok, so I've been learning C++ and recently got into sockets. I was able to successfully find out how to work sockets in Linux, and decided to go onto figuring it out in Windows. To start things off, here's my code so far:

#include <winsock.h>


CSocket::CSocket(){
}
CSocket::~CSocket(){
}

int hSock, Fdbk;

int CSocket::Connect(char *Address, int Port)
{
struct sockaddr_in srvrAddress;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData))
{
return 1;
}
memset(&srvrAddress, 0, sizeof(srvrAddress));
srvrAddress.sin_family = AF_INET;
srvrAddress.sin_addr.s_addr = inet_addr(Address);
srvrAddress.sin_port = htons(Port);
hSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSock < 0)
{
return 1;
}
Fdbk = connect(hSock, (struct sockaddr *) &srvrAddress, sizeof(srvrAddress));
if (Fdbk<0)
{
return 1;
}
return 0;
}
int CSocket::SendData(char *Data, int Len)
{
Fdbk = send(hSock, Data, Len + 1, 0);
return !Fdbk;
}
int CSocket::GetData(char *buffer)
{
memset(buffer, 0, strlen(buffer) + 1);
recv(hSock, buffer, sizeof(buffer), 0);
if (sizeof(buffer))
{
return 0;
}
else
{
return 1;
}
}
int CSocket::Close()
{
closesocket(hSock);
WSACleanup();
return 0;
}


Now, the problem i'm having is that the connect() is returning -1 and not connecting to the server. Would anyone happen to know what is wrong with this?

thanks a bunch!

Lee Peart
July 29th, 2003, 08:45 PM
Hi,

Use WSAGetLastError() to retrieve the winsock error code. Post the value here and we can go from there.

Lee

crazycheetah
July 29th, 2003, 09:17 PM
heh, ok... this is wierd for me.

i did went and put that in my code, and suddenly it worked... i tried taking out and it still worked.... maybe i accidentally changed something else and didn't realise it... but i don't see any changes :rolleyes: well... thanks anyway :)

desprical
July 29th, 2003, 10:02 PM
For people that want help with windows or unix type sockets, I found this guy's guide very helpful in the past:

Beej's Guide to Network Programming (http://www.ecst.csuchico.edu/~beej/guide/net/)