-
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.
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
|