我是C ++编程的新手,我正在尝试制作一个简单的客户端套接字程序,通过它的ip连接网站但是当我运行程序时遇到错误并显示"连接失败&# 34 ;.
#include <stdio.h>
#include "winsock2.h"
void main() {
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "173.194.123.14" );
clientService.sin_port = htons( 80 );
if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR ) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
}
我想知道这是连接网站的正确方法吗?如果没有,我如何通过其IP连接到网站?有人可以帮帮我吗?