-
May 7th, 2020, 04:53 PM
#1
Can't Connect
Trying to get up and running with Winsocks.
I have the following code and can't get a connection going.
I have seen this example on several sites.
The program appears to hang on the following command:
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
Here is the program:
I'm using VS2017.
The compiler was giving a warning that the function was depreciated, I disabled depreciated errors and got it to compile but it doesn't connect.
Could that be the problem?
Code:
#include<stdio.h>
#include <conio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main()
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
return 1;
}
printf("Initialised.");
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
}
printf("\nSocket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons(80);
//Connect to remote server
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
puts(" connect error");
return 1;
}
puts("Connected");
_getch();
return 0;
}
Rate this post if it helped you.
-
May 8th, 2020, 01:47 AM
#2
Re: Can't Connect
From MSDN:
Return value
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.
I do not see where you call the WSAGetLastError.
Last edited by VictorN; May 8th, 2020 at 04:41 AM.
Victor Nijegorodov
-
May 8th, 2020, 03:03 AM
#3
Re: Can't Connect
It just sits there and does nothing (hangs). I went to MSDN and some more ideas. I'll give that a shot.
btw, when I tried to compile that in VS 2017, I got a message that the "connect" function is depreciated???
Rate this post if it helped you.
-
May 8th, 2020, 04:09 AM
#4
Re: Can't Connect
I suspect it's waiting for a reply. I'd suggest you use Wireshark to examine the packets on the network to see what's happening.
What site is 74.125.235.20 ? When I try pinging it, it times-out and trying to connect to it via firefox, it just hangs - same as you are seeing. Is this site valid and active? The issue you are having could be with the site.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 9th, 2020, 12:41 AM
#5
Re: Can't Connect
Thanks,
That's exactly what was happening the site doesn't exist. I got it from an some website as the an address for GOOGLE.COM,
As I was doing some reading I learned about PING (told you guys I was a beginner , didn't even know what Ping is for). Anyhow plugged the ping address in there and it started working.
Thanks again guys.
I might be coming around here with more Winsock questions, my goal is to be able to read\write to a binary file on a server from my MFC application. I wrote my own database using fwrite, fread, fopen.
Now I have to make my desktop app Client\Server.
Thanks again guys.
Rate this post if it helped you.
-
May 9th, 2020, 12:47 AM
#6
Re: Can't Connect
Originally Posted by VictorN
From MSDN:I do not see where you call the WSAGetLastError.
Vic,
I'm not sure if you're asking me to go look up the error code so I can learn what is happening or you just read MSDN and realized how to get more information on the problem.
However, I did add error printing and got the error code, havn't had time to look it up tho.
I will get the code to you tho, It should be some code that says the server doesn't exist or doesn't accept connections.
Rate this post if it helped you.
-
May 9th, 2020, 03:42 AM
#7
Re: Can't Connect
Originally Posted by ADSOFT
Thanks,
That's exactly what was happening the site doesn't exist. I got it from an some website as the an address for GOOGLE.COM,
As I was doing some reading I learned about PING (told you guys I was a beginner , didn't even know what Ping is for). Anyhow plugged the ping address in there and it started working.
Thanks again guys.
I might be coming around here with more Winsock questions, my goal is to be able to read\write to a binary file on a server from my MFC application. I wrote my own database using fwrite, fread, fopen.
Now I have to make my desktop app Client\Server.
Thanks again guys.
There are ways of doing this other than using sockets. See https://docs.microsoft.com/en-us/win...communications for a discussion on these.
If you are going to be using sockets, then I'd suggest you get a basic understanding of TCP/IP and Wireshark.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 9th, 2020, 03:46 AM
#8
Re: Can't Connect
Thank You very much.
I will look into that.
Rate this post if it helped you.
-
May 9th, 2020, 03:51 AM
#9
Re: Can't Connect
Originally Posted by ADSOFT
... my goal is to be able to read\write to a binary file on a server from my MFC application.
Then I'd recommend you using not the raw Winsocks functions but the MFC CAsyncSocket class.
Originally Posted by ADSOFT
... I wrote my own database using fwrite, fread, fopen.
Why reinvent the wheel? Why not use already existing and free DBMS system like SQL Server (Express edition)?
Victor Nijegorodov
-
May 9th, 2020, 04:06 AM
#10
Re: Can't Connect
I read your link.
MFC CInternetFile looks promising. I want to be communicating through the Ineternet.
Rate this post if it helped you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|