Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

echo 서버 프로그램

  1. 윈속(winsock)기반 에코 서버 프로그램으로 BSD:::소켓(:12)과 POSIX(:12) 인터페이스로 만들었음.
  2. 에코 클라이언트 프로그램과 쌍으로 테스트할 수 있다.
#include <winsock.h>  
#include <stdio.h>  
  
#define MAX_PACKETLEN 512  
#define PORT 5552  
  
int main()  
{  
    WSADATA wsaData;  
    int status;  
    int SockLen;  
    int Readn,Writen;  
    SOCKET EndpointSocket, ClientSocket;  
    struct sockaddr_in SockInfo, ClientSockInfo;  
    char ReadBuffer[MAX_PACKETLEN];  
  
  
    if(WSAStartup(MAKEWORD(2,2),&wsaData)!= 0)  
    {  
        printf("error\r\n");  
        return 0;  
    }  
  
    EndpointSocket = socket( AF_INET, SOCK_STREAM, 0 );  
    if( EndpointSocket == INVALID_SOCKET )  
        return 1;  
  
    printf("Success socket create\r\n");  
    ZeroMemory(&SockInfo, sizeof( struct sockaddr_in ));  
  
    SockInfo.sin_family = AF_INET;  
    SockInfo.sin_port = htons( PORT );  
    SockInfo.sin_addr.S_un.S_addr = htonl(INADDR_ANY);  
  
    status = bind( EndpointSocket, (struct sockaddr*)&SockInfo, sizeof( struct sockaddr_in) );  
    if( status == SOCKET_ERROR)   
    {  
        printf("Bind Error\n");  
        return 0;  
    }  
    if( SOCKET_ERROR == listen( EndpointSocket, 5 ))  
    {  
        printf("listen Error\n");  
        return 0;  
    }  
  
    while(1)  
    {  
        ZeroMemory( &ClientSockInfo, sizeof( struct sockaddr_in ) );  
        SockLen = sizeof(struct sockaddr_in);  
        ClientSocket = accept( EndpointSocket, (struct sockaddr*)&ClientSockInfo, &SockLen );  
        if(ClientSocket == INVALID_SOCKET)  
        {  
            printf("Accept Error\n");  
            closesocket(EndpointSocket);  
            WSACleanup();  
            return 1;  
        }  
        printf("Accept Client\n");  
        Readn = recv( ClientSocket, ReadBuffer, MAX_PACKETLEN,0 );  
        if( Readn > 0 )  
        {  
            Writen = send( ClientSocket, ReadBuffer, Readn, 0 );  
        }  
        else  
        {  
            printf("read Error\n");  
        }  
        closesocket(ClientSocket);   
    }  
    closesocket( EndpointSocket );   
    WSACleanup();  
    return 0;  
  
}  

관련 글