Network/_Network Programming

C로짠 Client Windows Socket 소스

Dakuo 2010. 11. 15. 05:37

C로짠 Client Windows Socket 소스입니다.

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define IP "127.0.0.1"
#define PORT 80

int main()
{
        WSADATA wsaData;
        SOCKET client;
        SOCKADDR_IN serveraddr; 
        char buf[1024];

        WSAStartup(0x0202, &wsaData);

        printf("WSAStartup : %d\n", WSAGetLastError());
        client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        printf("socket : %d\n", WSAGetLastError());            

        serveraddr.sin_family = AF_INET;
        serveraddr.sin_port = htons(PORT);                                                      
        serveraddr.sin_addr.S_un.S_addr = inet_addr(IP);

        connect(client, (SOCKADDR*)&serveraddr, sizeof(serveraddr));

        memset(buf, 0, 1024);
        recv(client, buf, 1024, 0);
        printf("echo data : \n%s\n", buf);
     
        WSACleanup();
        system("pause");
}