• 0

VC6 socket problems


Question

Hi, i'm working on an irc project with vc6, and i've recently come into troubles about the connections..

Here is the code in full:

short WINAPI DLLExport Connect(LPRDATA rdPtr, long param1, long param2)

{

char * p1=(LPSTR)param1;

//Initialize some stuff

WSADATA WsaDat;

if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)

{

errorp = "WSA Initialization failed";

}

//Open a socket

Socket = socket(AF_INET, SOCK_STREAM, 0);

if (Socket == INVALID_SOCKET)

{

errorp = "Socket creation failed";

}

char Name[255];

gethostname(Name, 255);

SockAddr.sin_port = 6667;

HOSTENT *HostInfo;

HostInfo = gethostbyname(p1);

if (HostInfo == NULL)

{

errorp = "DNS could not be resolved";

}

else

{

SockAddr.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)HostInfo->h_addr_list[0][0];

SockAddr.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)HostInfo->h_addr_list[0][1];

SockAddr.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)HostInfo->h_addr_list[0][2];

SockAddr.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)HostInfo->h_addr_list[0][3];

}

//Connect

if (connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) != 0)

{

errorp = "Failed to establish connection with server";

}

else

errorp = "Connected";

return 0;

}

I know for a fact the DNS goes fine, because if you enter a bogus adress it fails, however every time you attempt a connect it will say, "Failed to establish connection with server"

Help would be much much appreciated

Thanks

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

char Name[255];
gethostname(Name, 255);

is the above code getting the user to enter the host to connect to?, if so shoudln't

HostInfo = gethostbyname(p1);

be

HostInfo = gethostbyname(Name);

?

If thats not the case you could try printing the value of p1 to check it is the host entered

Link to comment
Share on other sites

  • 0

ok in that case, i would try printing the value of p1 out to make sure it is being passed ok etc..

apart from that the only difference between an irc bot i coded and yours is that i used:

serverip = gethostbyname(server);
	if (serverip != NULL)
  memcpy((char *) &ircd.sin_addr, serverip->h_addr, serverip->h_length);

for getting the ip of the entered hostname, (server = char[100], serverip = HOSTENT*)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.