C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 1st, 2007, 03:04 AM
drezard drezard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 209 drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 18 h 21 m 18 sec
Reputation Power: 12
Winsock Connection Problems

Ive finished my winsock applications. Well they compile. Now, I have the problem of getting them to connect. Heres my code for both:

Server:
Code:
#include <windows.h>
#include <iostream>

WSADATA wsaData;
WORD version;
int iResult;
struct sockaddr_in sin;
SOCKET server;
SOCKET client;
int length; 

int main(){

	version = MAKEWORD(2, 2);

	iResult = WSAStartup(version, &wsaData);

	if (iResult != 0) {

		printf("WSAStartup failed: %d\n", iResult);
		return 1;

	}

	server = socket(AF_INET, SOCK_STREAM, 0);

	memset(&sin, 0, sizeof(sin));

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = INADDR_ANY;

	sin.sin_port = htons(8890);
	
	if (bind(server, (struct sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR) {

		printf("bind failed\n");
		return 1;

	}

	printf("Listening");

	while(listen(server, SOMAXCONN) == SOCKET_ERROR);

	length = sizeof(sin);
	client = accept(server,(struct sockaddr*) &sin, &length);

	closesocket(server);
	closesocket(client);

	WSACleanup();


}


Client:
Code:
#include <windows.h>
#include <iostream>

WSADATA wsaData;
WORD version;
int iResult;
struct sockaddr_in sin;
SOCKET client;

int main(){

	version = MAKEWORD(2, 2);

	iResult = WSAStartup(version, &wsaData);

	if (iResult != 0) {

		printf("WSAStartup failed: %d\n", iResult);
		return 1;

	}

	client = socket(AF_INET, SOCK_STREAM, 0); 

	memset(&sin, 0, sizeof(sin));

	sin.sin_family = AF_INET;

	sin.sin_addr.S_un.S_un_b.s_b1 = 127;
	sin.sin_addr.S_un.S_un_b.s_b2 = 0;
	sin.sin_addr.S_un.S_un_b.s_b3 = 0;
	sin.sin_addr.S_un.S_un_b.s_b1 = 1;

	sin.sin_port = htons(8890);

	printf("connecting");

	if ( connect(client, (struct sockaddr*) &sin, sizeof (sin)) == SOCKET_ERROR ) {
    
		return 1;

	}

	closesocket(client);

	WSACleanup();

}


I have unblocked the port on my pc. As you can see im trying to test it on just my computer (127.0.0.1). What other problems can u see?

Daniel
__________________
PHP and C++ Programmer.

Reply With Quote
  #2  
Old July 1st, 2007, 03:09 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 4,297 dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 17 h 31 m 24 sec
Reputation Power: 1013
OK, what is the specific error code you get when the client fails to connect?

WSAGetLastError().

Then look up what that error code means. On MSDN at http://msdn2.microsoft.com/en-us/library/ms740668.aspx.

As I stated already, printing out the error code gives you half a chance of figuring out what went wrong. Without it, you have no chance.

Reply With Quote
  #3  
Old July 1st, 2007, 03:44 AM
drezard drezard is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 209 drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level)drezard User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 18 h 21 m 18 sec
Reputation Power: 12
Thats the thing. It doesn't give me an error code. It just keeps trying to connect. It doesn't fail as in programming wise. It fails as in it can't find the server.

Daniel

Reply With Quote
  #4  
Old July 1st, 2007, 02:21 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 4,297 dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 17 h 31 m 24 sec
Reputation Power: 1013
Looking at your code, I do not see a single call to WSAGetLastError()!

It's not going to give you the error code unless you request it. That's the way that C works. I do not see where you request the error code. You need to request the error code.

In UNIX and Linux, sockets errors are part of the general C error reporting system. However, they're not part of the general reporting system in the Windows/DOS world, so Winsock has its own function to report the error code, WSAGetLastError(), and its own set of error codes, which can be a pain to track down at times.

Here's a function in my sample code that reports the error code:
Code:
/**************************************************************************/
/* ReportError                                                            */
/*    Displays a message that reports the error                           */
/*    Encapsulates the difference between UNIX and Winsock error handling */
/* Winsock Note:                                                          */
/*    WSAGetLastError() only returns the error code number without        */
/*    explaining what it means.  A list of the Winsock error codes        */
/*    is available from various sources, including Microsoft's            */
/*    on-line developer's network library at                              */
/*  http://msdn2.microsoft.com/en-us/library/ms740668.aspx                */
/**************************************************************************/
void ReportError(char *errorMessage)
{
    fprintf(stderr,"%s: %d\n", errorMessage, WSAGetLastError());
}

The error message that you would pass in would be something like:
ReportError("connect failed");

Please note that WSAGetLastError() is in the Winsock DLL, so if WSAStartup() fails then you cannot use it to report that error.

You have your tools. Now you need to use them.

Last edited by dwise1_aol : July 1st, 2007 at 02:25 PM.

Reply With Quote
  #5  
Old July 1st, 2007, 03:57 PM
KillerRabbit KillerRabbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: Finland
Posts: 255 KillerRabbit User rank is Sergeant (500 - 2000 Reputation Level)KillerRabbit User rank is Sergeant (500 - 2000 Reputation Level)KillerRabbit User rank is Sergeant (500 - 2000 Reputation Level)KillerRabbit User rank is Sergeant (500 - 2000 Reputation Level)KillerRabbit User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 Days 8 h 19 m 36 sec
Reputation Power: 16
Well, guess why you're only seeing "connecting..."?

Because it's the last thing you're printing, put the accept() function in if() clause, and check if connection is established, if is, you'll print something. I'm pretty sure the connection succeeds, you just don't see it, because you're not printing it.

And what's the point of this:

Code:
while(listen(server, SOMAXCONN) == SOCKET_ERROR);


Because using listen() you only set the maximum allowed connections.

Reply With Quote
  #6  
Old July 1st, 2007, 05:00 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 4,297 dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 17 h 31 m 24 sec
Reputation Power: 1013
Plus, there's this little bit of idiocy (not your bit, since you had copied this from another source):
Code:
	sin.sin_addr.S_un.S_un_b.s_b1 = 127;
	sin.sin_addr.S_un.S_un_b.s_b2 = 0;
	sin.sin_addr.S_un.S_un_b.s_b3 = 0;
	sin.sin_addr.S_un.S_un_b.s_b1 = 1;

You assign 127 to the first byte of sin_addr, then the last thing you is overwrite that with a 1. And you never initialize the fourth byte.

Change that second "1" to a "4" and it will stand a far better chance of working.

I've seen that exact same error committed by someone else who posted here, also using an example that he had found on-line. I suspect that you may have tapped into that same faulty source.

And do rewrite it so that you will output two different messages: one when the connect fails and the other when it succeeds. That way you will know for sure, rather than just having to assume.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Winsock Connection Problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT