Hi I'm new to socket programming and I'm trying out the following code from the tutorial of http://www.binarytides.com/winsock-socket-programming-tutorial/
I'm trying to connect to server and I'm using the IP address of google. Here is the code:
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
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.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.224.72");
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");
return 0;
}
So far socket can be created but I can't connect to the server. To be more specific I always exit and have following:
The program '[2060] SocketCTest.exe: Native' has exited with code 1 (0x1).
even if I set a breakpoint before returns.
There's no error. I've tried on my computer and connect returned with error ( WSAETIMEDOUT
). I'm not sure whether there are the network settings on my computer, proxies, firewalls, and so on; or that google host is configured not to accept direct socket connections.
Anyway here's your code with some small adjustments:
#include <stdio.h>
#include <conio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib") //Winsock Library
int main(int argc, char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char c = 0;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0)
{
printf("Failed. Error Code : %d.\nPress a key to exit...", WSAGetLastError());
c = getch();
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
printf("Could not create socket : %d.\n", WSAGetLastError());
WSACleanup();
c = getch();
return 1;
}
printf("Socket created. Connecting...\n");
memset(&server, 0, sizeof server);
server.sin_addr.s_addr = inet_addr("74.125.224.72");
server.sin_family = AF_INET;
server.sin_port = htons(80);
//Connect to remote server
if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("Connect error:%d.\nPress a key to exit...", WSAGetLastError());
closesocket(s);
WSACleanup();
c = getch();
return 1;
}
puts("Connected.\nPress a key to exit...");
closesocket(s);
WSACleanup();
c = getch();
return 0;
}
Now if you want to see the code actually connecting i'd suggest using localhost
( 127.0.0.1
) instead of 74.125.224.72
, and the port 3389
for example (it's the Remote Desktop Server (RDP) port, if you have RDP configured and running on your computer) instead of 80
; or you could let 80
if you have a web server ( IIS ?) running on your computer.
To get a list of server programs that run on your computer run the command:
`netstat -an | findstr LISTEN`
which would output a bunch of lines (and the ones that we care about are) in this form (here's the one corresponding to the RDP example from above):
`TCP 127.0.0.1:3389 0.0.0.0:0 LISTENING`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.