Advertisement

05.07.2008 at 10:36AM PDT, ID: 23383614
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Winsock "connect" still connects (no error) when server responds with ACK,RST
Tags: C/C++
I have a very simple SMTP email program I'm testing.  If I connect to a server with the SMTP service running, it works perfectly and sends a little email message.

If the SMTP service is not running on the server, the server sends back an ACK,RST (reset) packet.  My program receives this but it does not generate an error at the "connect" statement.

Any ideas why?
1:
2:
3:
4:
5:
6:
7:
8:
printf("Connecting socket...");
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
  printf("ERROR: Cannot connect to server\n");
  WSACleanup();
  return(1);
}
printf("Connected\n");
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: ttitus
Solution Provided By: jkr
Participating Experts: 2
Solution Grade: A
Views: 5
Translate:
Loading Advertisement...
05.07.2008 at 10:46AM PDT, ID: 21518695

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 02:40PM PDT, ID: 21520570

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 02:48PM PDT, ID: 21520629

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 05:18PM PDT, ID: 21521421

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 07:30PM PDT, ID: 21521918

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2008 at 11:25PM PDT, ID: 21522670

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 01:52AM PDT, ID: 21523242

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 02:02AM PDT, ID: 21523267

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 08:29AM PDT, ID: 21525563

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 09:27AM PDT, ID: 21526169

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 09:29AM PDT, ID: 21526185

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 09:34AM PDT, ID: 21526235

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 09:35AM PDT, ID: 21526264

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 10:43AM PDT, ID: 21526839

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 04:32PM PDT, ID: 21529332

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.07.2008 at 10:46AM PDT, ID: 21518695

Rank: Genius

>>If the SMTP service is not running on the server, the server sends back an
>>ACK,RST (reset) packet

This behaviour is described here: http://support.microsoft.com/kb/175523 ("INFO: Winsock TCP Connection Performance to Unused Ports")

------------------------------------------->8-------------------------------------

When a process creates a TCP socket in the Microsoft Winsock layer and issues a connect() call to some host and port number, the TCP protocol performs its standard "three-way handshake" of a SYN packet, acknowledged by the target host with ACK/SYN, and a final ACK from the initiating client. However, per the TCP specifications stated in RFC-793 (Transmission Control Protocol), a reset (RST) might be generated if the SYN attempt is addressed to a host which exists but the port is unused. [...]
Upon receiving the ACK/RST client from the target host, the client determines that there is indeed no service listening there. In the Microsoft Winsock implementation of TCP, a pending connection will keep attempting to issue SYN packets until a maximum retry value is reached (set in the registry, this value defaults to 3 extra times). Since an ACK/RST was received from the target host, the TCP layer knows that the target host is indeed reachable and will not double the time-out value in the packet's IP header, as is standard during connection attempts with unacknowledged SYNs. Instead, the pending connection will wait for the base connection's time-out value and reissue another SYN packet to initiate a connection. As long as an ACK/RST packet from an unused port is received, the time-out value will not increase and the process will repeat until the maximum retry value is reached.

------------------------------------------->8-------------------------------------
 
05.07.2008 at 02:40PM PDT, ID: 21520570
This is great, but how do I determine that there is no connection?

WSAGetLastError() returns 0, so it still thinks that there's a connection when there isn't.  How do I confirm that it either worked, or failed?
 
05.07.2008 at 02:48PM PDT, ID: 21520629

Rank: Genius

You'll get an error when you write to the socket.
 
05.07.2008 at 05:18PM PDT, ID: 21521421
Nope.  Here's the sequence and when I get an error:

connect (no error)
recv (nothing returned, no error)
send "helo company.com" (no error)
recv (nothing returned, no error)
send "quit" -error generated after this send:
ERROR RECEIVING: The virtual circuit was terminated due to a time-out or other f
ailure. The application should close the socket as it is no longer usable.

Should I re-code to use WSAConnect, or is there a better more reliable mechanism for connecting?
 
05.07.2008 at 07:30PM PDT, ID: 21521918

Rank: Genius

>>Should I re-code to use WSAConnect,

Um, how are you doing that now?
 
05.07.2008 at 11:25PM PDT, ID: 21522670
I'm using standard sockets calls:
WSAStartup()
socket()
connect()
recv()
send()
shutdown()
WSACleanup()
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:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
/***********************************************/
/* SMTPTest.cpp                                */
/* A program to test SMTP mail communications  */
/*                                             */
/* Compile with:                               */
/* cl SPAMSend.c /link ws2_32.lib advapi32.lib */
/*                                             */
/***********************************************/
#include <stdio.h>
#include <winsock.h>
 
/*****************/
/* MAIN FUNCTION */
/*****************/
void main(int nArgc, char *pArgv[] )
{
	WSADATA				wsaData;
	SOCKET				s;				/* Socket identifier */
 
	char				buf[255];		/* Buffer for incoming */
	int					n;				/* Bytes to read */
	struct servent		*ServEntry;		/* Pointer to service info entry */
	struct hostent		*HostEntry;
	struct sockaddr_in	sin;			/* Internet socket structure */
	int					type;			/* socket type */
 
    // Evaluate arguments
    printf("SMTPTest v1.0\n\n");
    if ( nArgc != 2 )
    {
        printf("  A program to test SMTP mail communications.\n");
        printf("\nUsage:\t%s mailsystem\n\n", strupr(pArgv[0]));
        printf("\tmailsystem = IP address or DNS name of mail forwarder\n");
        return;
    }
 
    // Initialize WinSock
    if (WSAStartup( MAKEWORD( 2, 0), &wsaData) != 0)
    {
        printf("ERROR: Error starting up WinSock\n");
        return;
    }
 
    // Allocate a socket
    s = socket(PF_INET, SOCK_STREAM, 0);
    if ( s == INVALID_SOCKET )
    {
        printf("ERROR: Invalid socket\n");
        WSACleanup();
        return;
    }
 
    memset(&sin, 0, sizeof(sin));		    // Clear out all variables in socket structure
    sin.sin_family = AF_INET;			    // Set protocol to IP
 
    // Set port to SMTP
    if (ServEntry = getservbyname("smtp", "tcp"))
    {
        sin.sin_port = ServEntry->s_port;
    }
    else
    {
        printf("ERROR: Port discovery error\n");
        WSACleanup();
        return;
    }
 
    // Perform DNS resolution of IP address of mail server
	if ( (sin.sin_addr.s_addr = inet_addr(pArgv[1])) == INADDR_NONE)
	{
		if ( HostEntry = gethostbyname(pArgv[1]) )
		{
			memcpy(&sin.sin_addr, HostEntry->h_addr, HostEntry->h_length);
		}
		else
		{
			printf("ERROR: Can't get \"%s\" host entry\n", pArgv[1]);
			WSACleanup();
			return;
		}
	}
	printf("%s resolves to %s\n", pArgv[1], inet_ntoa(sin.sin_addr));
 
    // Connect a TCP socket to the server
    if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
        printf("ERROR: Cannot connect to server\n");
        WSACleanup();
        return;
    }
 
    // Receive mail server header
	buf[0] = '\0';
    n = recv(s, buf, 255, 0);
    if ( n == SOCKET_ERROR )
    {
		printf("ERROR: Cannot receive data\n");
		goto Cleanup;
	}
    buf[n-1] = '\0';
    printf("RECV> %s\n", buf);
 
    // Send "helo" to the server
    sprintf(buf, "helo company.com\r\n");
    n = send(s, buf, strlen(buf), 0);
 
    if ( n == SOCKET_ERROR )
    {
		printf("ERROR: Cannot send data\n");
		goto Cleanup;
	}
	buf[strlen(buf)-1] = '\0';
    printf("SEND> %s\n", buf);
 
    // Receive "250 OK" response from server
	buf[0] = '\0';
    n = recv(s, buf, 255, 0);
    if ( n == SOCKET_ERROR )
    {
		printf("ERROR: Cannot receive data\n");
		goto Cleanup;
	}
    buf[n-1] = '\0';
    printf("RECV> %s\n", buf);
 
    // Send "Quit" to the server
	sprintf(buf, "quit\r\n");
    n = send(s, buf, strlen(buf), 0);
    if ( n == SOCKET_ERROR )
    {
		printf("ERROR: Cannot send data\n");
		goto Cleanup;
	}
	buf[strlen(buf)-1] = '\0';
    printf("SEND> %s\n", buf);
 
    // Receive "221 closing connection" from the server
    n = recv(s, buf, 255, 0);
    if ( n == SOCKET_ERROR )
    {
		printf("ERROR: Cannot receive data\n");
		goto Cleanup;
	}
    buf[n-1] = '\0';
    printf("RECV> %s\n", buf);
 
    // Close TCP socket
Cleanup:
    if (shutdown(s, 0) != 0)
    {
        printf("Error with shutdown\n");
        WSACleanup();
        return;
    }
 
    // Cleanup WinSock
    if ( WSACleanup() != 0 )
    {
        printf("Error shutting down WinSock\n");
        return;
    }
 
    return;
}
Open in New Window
 
05.08.2008 at 01:52AM PDT, ID: 21523242

Rank: Genius

If the port you are trying to connect to is not open, then you should get a WSAECONNREFUSED from the connect call. ie. "ERROR: Cannot connect to server\n" should be shown.

Did you verify that the server is in fact correctly sending back the ACK,RST ? (with a sniffer for example). Do you have logs on the server side about the connection attempt ?

A simple test is to call your program with "localhost" as parameter (assuming that your pc does not have an SMTP server running on it). See if you get the same problem with that.

I did this exact test over here with your code, and I got the expected result - ie. the "ERROR: Cannot connect to server\n" error message.




Note : main should return int, not void !!
 
05.08.2008 at 02:02AM PDT, ID: 21523267

Rank: Genius

>> A simple test is to call your program with "localhost" as parameter

or if your machine doesn't send back an ACK,RST, try another pc on your network.
 
05.08.2008 at 08:29AM PDT, ID: 21525563

Rank: Genius

The problem is in fact ACK,RST (as quoted above) - the server should perform a CLOSE instead.
 
05.08.2008 at 09:27AM PDT, ID: 21526169
Here's what I get when I try to connect to a Windows 2000 workstation's IP address (no SMTP relay, no firewall, bare install):
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
C:\Source\Work\SMTPTest>smtptest 192.168.1.104
SMTPTest v1.0
 
192.168.1.104 resolves to 192.168.1.104
Connected: 0
RECV>
SEND> helo company.com
RECV>
SEND> quit
ERROR: Cannot receive data
 
C:\Source\Work\SMTPTest>
Open in New Window
 
05.08.2008 at 09:29AM PDT, ID: 21526185
Sorry, the above output included the line "Connected: 0".  I added this for more debugging:
1:
	printf("Connected: %u\n", WSAGetLastError());
Open in New Window
 
05.08.2008 at 09:34AM PDT, ID: 21526235
I just finished doing two sniffer traces.  The first one was to the Windows 2000 workstation.  The second was to my router (no SMTP process running there either).

Both of them responded identically:
SENT> SYN
RECV> RST,ACK
SENT> SYN
RECV> RST,ACK
SENT> SYN
RECV> RST,ACK

So it looks like the Microsoft IP stack is trying three times to get the port to open, yet I still don't get any way to trap this as an error until way later in the code, and it still seems unreliable as to where it's caught.
 
05.08.2008 at 09:35AM PDT, ID: 21526264

Rank: Genius

Strange - that's what I get with both a running MTA and without one running:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
C:\tmp\cc>smtptest srv2
SMTPTest v1.0
 
srv2 resolves to x.y.1.101
RECV> 220 srv2 Microsoft ESMTP MAIL Service, Version: 5.0.2195.6713 ready at Thu, 8 May 2008 ----------
SEND> helo company.com
RECV> 250 srv2 Hello [x.y.1.20]
SEND> quit
RECV> 221 2.0.0 srv2 Service closing transmission channel
 
C:\tmp\cc>smtptest mxp1
SMTPTest v1.0
 
xp1 resolves to x.y.1.20
ERROR: Cannot connect to server
Open in New Window
Accepted Solution
 
05.08.2008 at 10:43AM PDT, ID: 21526839

Rank: Genius

I get the same as jkr. And looking at the code, it should work.


>> Here's what I get when I try to connect to a Windows 2000 workstation's IP address (no SMTP relay, no firewall, bare install):

If you tested this with different machines, and get the same result, I'd suspect that there's a problem with your TCP/IP stack.


>> yet I still don't get any way to trap this as an error until way later in the code, and it still seems unreliable as to where it's caught.

It should cause an error immediately after the connect.
 
05.08.2008 at 04:32PM PDT, ID: 21529332
I now need to find out what's wrong/different with my computer where I'm testing.
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628