다음은 socket 기본 함수를 이용하여 웹페이지에 접속하는 프로그램이다. 접속만 하고 실제로 긁어오는건 구현이 안되있다.
int main() { int connectSD;//소켓 디스크립터 struct sockaddr_in origin_addr; char *addr = "http://www.kurapa.com";
if((connectSD = socket(PF_INET, SOCK_STREAM, 0)) < 0) error_handling("connectSD socket() error");
memset(&origin_addr, 0, sizeof(origin_addr)); origin_addr.sin_family = AF_INET; origin_addr.sin_addr.s_addr = inet_addr(addr); origin_addr.sin_port = htons(80);
if(connect(connectSD, (struct sockaddr*)&origin_addr, sizeof(origin_addr)) == -1) error_handling("connect() error");
return 0;
}
그리고 다음은 WIN32기반으로 구현해본 간단 접속 프로그램 예제이다.
#include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h>
#define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "27015"
int __cdecl main(int argc, char **argv) { WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; struct addrinfo *result = NULL, *ptr = NULL, hints; char *sendbuf = "this is a test"; char recvbuf[DEFAULT_BUFLEN]; int iResult; int recvbuflen = DEFAULT_BUFLEN; // Validate the parameters if (argc != 2) { printf("usage: %s server-name
", argv[0]); return 1; }
// Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { printf("WSAStartup failed: %d
", iResult); return 1; }
ZeroMemory( &hints, sizeof(hints) ); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result); if ( iResult != 0 ) { printf("getaddrinfo failed: %d
", iResult); WSACleanup(); return 1; }
// Attempt to connect to an address until one succeeds for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); if (ConnectSocket == INVALID_SOCKET) { printf("Error at socket(): %ld
", WSAGetLastError()); freeaddrinfo(result); WSACleanup(); return 1; }
// Connect to server. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); ConnectSocket = INVALID_SOCKET; continue; } break; }
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) { printf("Unable to connect to server!
"); WSACleanup(); return 1; }
// Send an initial buffer iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iResult == SOCKET_ERROR) { printf("send failed: %d
", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; }
printf("Bytes Sent: %ld
", iResult);
// shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { printf("shutdown failed: %d
", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; }
// Receive until the peer closes the connection do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if ( iResult > 0 ) printf("Bytes received: %d
", iResult); else if ( iResult == 0 ) printf("Connection closed
"); else printf("recv failed: %d
", WSAGetLastError());
} while( iResult > 0 );
// cleanup closesocket(ConnectSocket); WSACleanup();
return 0; }
Tags: client socket win32소켓 소켓 클라이언트
|