|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Winsock Connection Problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|