Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock question

Status
Not open for further replies.

AtomicChip

Programmer
May 15, 2001
622
CA
I'm creating a .dll for direct SMTP services just 'cuz I'm sick and tired of the silly little Outlook box popping up every time I want to send an e-mail. I'm running into difficulty in the connection part of the dll. The socket is initalized, created, and connection info for it is all set just fine, but for some reason whenever I try to connect, I keep getting the 10060 error (Timeout). Here is the code that I have come up with. Any help here would be greatly appreciated, and worth some big starts...

#include <windows.h>
#include <winsock.h>

#define PORTNUM 5000
#define NOMANGLE

int index = 0, iReturn;
char szClientA[100];
TCHAR szClientB[100];
TCHAR szError[100];

SOCKET ServerSock = INVALID_SOCKET;
SOCKADDR_IN destination_sin;
PHOSTENT phostent = NULL;

WSADATA WSAData;

NOMANGLE int _stdcall InitSock();
NOMANGLE int _stdcall CreateSock();
NOMANGLE int _stdcall SetConnectionInfo( LPCTSTR, unsigned int );
NOMANGLE int _stdcall ConnectToServer();

NOMANGLE int _stdcall InitSock()
{
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
{
return -1;
exit(0);
}
return 0;
}

NOMANGLE int _stdcall CreateSock()
{
if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
return -1;
exit(0);
}
return 0;
}

NOMANGLE int _stdcall SetConnectionInfo( LPCTSTR hostName, unsigned int port )
{
destination_sin.sin_family = AF_INET;
if ((phostent = gethostbyname (hostName)) == NULL)
{
closesocket (ServerSock);
return -1;
exit(0);
}

memcpy ((char FAR *)&(destination_sin.sin_addr), phostent->h_addr, phostent->h_length);
destination_sin.sin_port = htons (PORTNUM);

return 0;
}

NOMANGLE int _stdcall ConnectToServer()
{

if (connect (ServerSock, (PSOCKADDR) &destination_sin, sizeof (destination_sin)) == SOCKET_ERROR)
{
closesocket (ServerSock);
return -1;
exit(0);
}
return 0;

} -----------------------------------------------
&quot;The night sky over the planet Krikkit is the least interesting sight in the entire universe.&quot;
-Hitch Hiker's Guide To The Galaxy
 
I think you'll find that the code is timing out due to DNS errors. You'll need to ensure that the h_name of the HOSTENT struct contains a fully qualified domain name.

Or you can create an entry in your hosts file and use that.

e.g.
xyz.xyx.xny.xx MailServer

By using MailServer for the h_name.

HTH
William
Software Engineer
ICQ No. 56047340
 
remember: gethostbyname(char *)

thus:
LPCTSTR ~ CString *

ok so now: if your passing in a pointer to a CString, declare it as that and use this:

if you pass in CString * hostname as parameters use:

gethostbyname(hostname->GetBuffer(0));

and be sure it is a fully qualified domain...

hope that helps

[]D[][]V[][]D
 
It should be noted that the GetBuffer() method of the CString object requires a parameter > than 0 since the value passed is used to determine the size of the string returned and not the starting point.



William
Software Engineer
ICQ No. 56047340
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top