#ifndef UNICODE #define UNICODE #endif #define WIN32_LEAN_AND_MEAN #include#include #include // Link with ws2_32.lib #pragma comment(lib, "Ws2_32.lib") #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT 80 int main() { //---------------------- // Declare and initialize variables. int iResult; WSADATA wsaData; SOCKET ConnectSocket = INVALID_SOCKET; struct sockaddr_in clientService; int recvbuflen = DEFAULT_BUFLEN; char *sendbuf = "GET / HTTP/1.1\r\n\r\n"; char recvbuf[DEFAULT_BUFLEN] = ""; //---------------------- // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) { wprintf(L"WSAStartup failed with error: %d\n", iResult); return 1; } //---------------------- // Create a SOCKET for connecting to server ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { wprintf(L"socket failed with error: %ld\n", WSAGetLastError()); WSACleanup(); return 1; } //---------------------- // The sockaddr_in structure specifies the address family, // IP address, and port of the server to be connected to. clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( "180.70.134.19" ); clientService.sin_port = htons( DEFAULT_PORT ); //---------------------- // Connect to server. iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) ); if (iResult == SOCKET_ERROR) { wprintf(L"connect failed with error: %d\n", WSAGetLastError() ); closesocket(ConnectSocket); WSACleanup(); return 1; } //---------------------- // Send an initial buffer iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iResult == SOCKET_ERROR) { wprintf(L"send failed with error: %d\n", WSAGetLastError()); closesocket(ConnectSocket); WSACleanup(); return 1; } printf("Bytes Sent: %d\n", iResult); // Receive until the peer closes the connection do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); if ( iResult > 0 ) wprintf(L"Bytes received: %d\n", iResult); else if ( iResult == 0 ) wprintf(L"Connection closed\n"); else wprintf(L"recv failed with error: %d\n", WSAGetLastError()); } while( iResult > 0 ); // close the socket iResult = closesocket(ConnectSocket); if (iResult == SOCKET_ERROR) { wprintf(L"close failed with error: %d\n", WSAGetLastError()); WSACleanup(); return 1; } WSACleanup(); return 0; }
2013년 1월 27일 일요일
hi socket
피드 구독하기:
댓글 (Atom)
댓글 없음:
댓글 쓰기