블로그 이미지
Kanais
Researcher & Developer 퍼즐을 완성하려면 퍼즐 조각들을 하나 둘씩 맞춰나가야 한다. 인생의 퍼즐 조각들을 하나 둘씩 맞춰나가다 보면 인생이란 퍼즐도 완성되는 날이 오려나...?

calendar

1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Notice

2015. 5. 11. 16:01 Programming/Network

작성날짜    : 2011-03-16


출처 : ehclub - http://cafe.daum.net/sbehclub?t__nil_cafemy=item


TCP 서버

 

ls = socket();             // listen소켓   

           //매개변수...

           //네트워크. - 사용하는 네트워크가 무엇인가.(AF_INET)

           //전송 방식. (SOCK_STREAM - DGRAM)       // UDT : TTL 소멸되서 전송 안될수도 있고순서도 섞일  있고신뢰성 보장 X

bind(sock, &serv_addr,( 적었음)...);

           //bind  - 랜카드에 Linking할때 사용되는

           //bind 항상 자기 자신의 ip bind.

           //&serv_addr   :         SOCKADDR                          // IPv4 : SOCKADDR_IN

lister(sock, 5);           // 동접자수 제한.

int len;

while(1)

{

           ds = accept(sock,(CSOCKADR *)&cliaddr,&len);                 // 접속 되게 해주는 메소드 - reading 하기위한 소켓

           CreateThread(0,0,Doit,ds,0,0);
}

 

send(SOCKET, char *, Size size, flag);

 

TCP 클라이언트

 

socket

bind(옵션)                 // http://no1rogue.blog.me/30096947717

connet(sock,(SOCKADDR *)&servaddr, sizeof(servaddr));

 

 

일반화 부분 *

class MyLib

{

public:

    static LONG GetHostIP(); //자신의 IP주소(network byte order) 반환하는 메소드 - 반환

};

--------------------------------------------------------------------------------------

LONG MyLib::GetHostIP()

{

     char name[256];

     gethostname(name,255); //호스트  얻어오기

     hostent *hten;

     hten =gethostbyname(name); //호스트의 하드웨어 엔트리 얻어오기

     return (LONG)hten[1].h_addr_list; //하드웨어 엔트리에서 IP주소 반환하기

 

* TCP Server *

class MyTcpServ

{

    SOCKET sock;

    SOCKADDR_IN servaddr;

    int pnum;

public:     

     MyTcpServ(int _pnum,int lcnt=5,bool all=false);

     virtual ~MyTcpServ();

    operator SOCKET()

     {

          return sock;

     }      

     const SOCKADDR_IN *GetMyAddrInfo()const

     {

          return &servaddr;

     }

};

 

MyTcpServ::MyTcpServ(int _pnum,int lcnt,bool all)

{

     pnum = _pnum;

     if((sock = socket(AF_INET,SOCK_STREAM,0))==-1)

     {

           rerurn ;

     }

 

     memset(&servaddr,0,sizeof(servaddr));

     servaddr.sin_family = AF_INET;

     servaddr.sin_addr.s_addr =GetHostIP();                   // inet_addr("192.168.34.100");

     servaddr.sin_port = htons(pnum);                 // host byte other n(포트번호 - 0~1024포트번호는 쓰지말자)

                                                                // noht   htons반대

 

 

     while(bind(sock,(SOCKADDR *)&servaddr,sizeof(servaddr) == -1)

     {

           pnum +=2;

           servaddr.sin_port = htons(pnum);

     }

 

     if(listen(sock,lcnt)==-1)

     {

          closesocket(sock);

          return ;

     };

     return sock;

}

MyTcpServ::~MyTcpServ()

{

    if(sock!=-1)

    {

        closesocket(sock);

    }

}

 

* TCPServerThread - 추가 작업 필요 *

class MyTcpServThread:publi MyTcpServ

{

     

     

public:     

     MySocketThread(int _pnum,int lcnt=5,bool all=false);

     virtual ~MySocketThread();

}

MyTcpServThread::MyTcpServThread(int _pnum,int lcnt,bool all):MyTcpServ(_pnum,lcnt,all)

{

};

MyTcpServThread::~MyTcpServThread()

{

}


'Programming > Network' 카테고리의 다른 글

[Network] UDT Server  (0) 2015.05.11
[Network] IPv4 Stack  (0) 2015.05.11
[Network] IPC  (0) 2015.05.11
[Network] IOCP  (0) 2015.05.11
[Network] connect  (0) 2015.05.11
posted by Kanais