⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
字号:
//-----------------------------------------------------------------------------
// Name: Sockets_Receive
//
// Description: Example code showing how to send 
//				and receive information from the web.
//				*NOTE* Uses TCP/IP for guaranteed messaging.
//
// File Function: Main program file (main.cpp)
//
// Code: 
//		Copyright (c) 2003 LostLogic Corporation. All rights reserved.
//
// Libraries Required:
//		ws2_32.lib
//
// Local Files Required:
//		main.cpp
//
// DX Files: 
//		Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------

// Standard Includes
#include <iostream.h>
#include <winsock.h>
#include <stdio.h>

void main(void)
{
	SOCKET		skSocket;
	sockaddr_in saServerAddress;
	int			iPort = 80;
	int			iStatus;
	WSADATA		wsaData;
	WORD		wVersionRequested;
	LPHOSTENT	lpHost;
	char		szHost[128];
	char		szSendBuffer[256];
	char		szRecvBuffer[32768];
	int			iBytesSent;
	int			iBytesReceived;

	// Init the host value, change this URL to whatever valid URL you wish
	sprintf(szHost,"www.lostlogic.com");

	// Tell WinSock we want version 2
	wVersionRequested = MAKEWORD( 2, 0 );

	// Initialize the socket handle
	skSocket = INVALID_SOCKET;

	// Startup WinSock
	iStatus = WSAStartup( wVersionRequested, &wsaData );

	// Create the socket
	skSocket = socket( AF_INET, SOCK_STREAM, 0 );
	
	// Check if there was an error
	if( skSocket == INVALID_SOCKET ) {
		cout << "**ERROR** Could Not Create Socket" << endl;
		// Clean up WinSock
		WSACleanup();
		exit(1);
	}

	cout << "<-- SOCKET CREATED -->" << endl;

	// Initialize the server address data structure
	memset(&saServerAddress,0,sizeof(sockaddr_in));
	// Set this by default
	saServerAddress.sin_family = AF_INET;
	// Load the IP Address
	saServerAddress.sin_addr.s_addr = inet_addr(szHost);

	// If the host specified is not an IP Address we must look up the value
	if( saServerAddress.sin_addr.s_addr == INADDR_NONE )
	{
		cout << "<-- LOOKING UP HOST IP -->" << endl;
		// Get the host name
		lpHost = gethostbyname(szHost);
		// Check if we got something back
		if (lpHost != NULL) {
			// Load the server address with the host information
			saServerAddress.sin_addr.s_addr = ((LPIN_ADDR)lpHost->h_addr)->s_addr;
		}
		else {
			cout << "**ERROR** Could Not Locate Host" << endl;
			// Clean up WinSock
			WSACleanup();
			exit(1);
		}
	}

	// Set the Server Port
	saServerAddress.sin_port = htons( iPort );

	// Attempt to connect to the server
	iStatus = connect(skSocket, (struct sockaddr*)&saServerAddress,sizeof(sockaddr));
	
	// Check if there was an error
	if( iStatus == SOCKET_ERROR ) {
		cout << "**ERROR** Could Not Connect To Server" << endl;
		// Clean up WinSock
		WSACleanup();
		exit(1);
	}

	cout << "<-- CONNECTED TO SERVER -->" << endl;

	// Load the data to send
	sprintf(szSendBuffer,"GET / HTTP/1.0\n\n");

	// Send the HTTP Request
	iBytesSent = send( skSocket, szSendBuffer, strlen( szSendBuffer ), 0 );

	cout << "<-- SENT " << iBytesSent << " BYTES -->" << endl;
	
	// Clear buffer
	memset( szRecvBuffer, 0x00, 32768 );
	
	// Wait for incoming data
	iBytesReceived = recv( skSocket, szRecvBuffer, 32768, 0 );

	// Output the data received
	cout << szRecvBuffer << endl;

	cout << "<-- RECEIVED " << iBytesReceived << " BYTES -->" << endl;

	// Close the socket
	closesocket(skSocket);

	// Clean up WinSock
	WSACleanup();
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -