==========================================================================================

NETWORK

==========================================================================================

<NETWORK>



=입출력 다중화

-인터럽트 + 폴링



=SELECT()


-fd_set : 128 byte = 1024 bit


장치들의 상태 모니터링 가능 (0,1,2-stdin,stdout,stderr ...)



int select( ,R,W,E, );





select() blocking => R, W, E 중 이벤트 감지가 되면 BLOCKING 풀림


-timeout 0 => 무한 대기


-select()인자 : (감시대상 번호(ex : 2) + 1, READ, WRITE, ERROR, TIMEOUT)



=>멀티 스레드/ 멀티 프로세스 사용 없이 채팅 가능


<select.c>

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
   
 fd_set A;
    printf(
"fd_set size : %d bytes\n",sizeof(fd_set));
    
FD_ZERO(&A);    // 초기화
    if(1==FD_ISSET(2,&A)
)
    {
        printf(
"2번의 값: 1\n");
    }
    
else
    {
        printf(
"2번의 값: 0\n");
    }
    
FD_SET(2&A);  // 2번 BIT 1
    if(1==FD_ISSET(2,&A))
    {
        printf(
"2번의 값: 1\n");
    }
    
else
    {
        printf(
"2번의 값: 0\n");
    }
    FD_CLR(
2&A);  // 2번 BIT 0
    if(1==FD_ISSET(2,&A))
    {
        printf(
"2번의 값: 1\n");
    }
    
else
    {
        printf(
"2번의 값: 0\n");
    }

    FD_SET(
0&A);  // 키보드 입력 감시 셋
    select(1&A, 000);
    
printf("select 풀림\n");
    
return 0;
}





<select_1.c>

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define STDIN 0
#define MAX_LINE 80

int main(int argc, char **argv)
{
    fd_set readfds;
    
int fdn;
    
char rbuf[MAX_LINE];
    
struct timeval timeout;

  
  FD_ZERO(&readfds);
    FD_SET(STDIN, 
&readfds);


    
while(1)
    {
      
  timeout.tv_sec = (long)3;
        timeout.tv_usec 
= 0L;

        printf(
"> ");
        fflush(stdout);
       
 fdn = select(STDIN+1&readfds, NULL, NULL, &timeout);
        
if(fdn ==0)
        {
            printf(
"\nError : Time Out\n");
        }
        
else
        {
            memset(rbuf, 
0x00, MAX_LINE);
            read(STDIN, rbuf, MAX_LINE -
1);
            
if(strncmp(rbuf, "quit\n",5== 0)
                
break;
            printf(
"Your Message : %s", rbuf);
        }
        
FD_SET(STDIN, &readfds);
    }
    
return 0;
}







=select() + 채팅 프로그램


<smartsock2.h>

#ifndef __SMARTSOCK2_H__
#define __SMARTSOCK2_H__

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/select.h>


#define PORT    7777
#define IP      "192.168.0.219"

#define MSG_SIZE    255
#define BUF_SIZE    (MSG_SIZE + 1)
#define MSG_END     "quit"

#endif //__SMARTSOCK2_H__


<client2.c>

#include "smartsock2.h"

int main()
{
    
int iFd;
    
struct sockaddr_in stAddr;
    
int iLen;
    
int iRet;
 
   fd_set fdRead;

    iFd = socket(AF_INET, SOCK_STREAM, 0);
    
if(-1 == ifd)
    {
        perror(
"socket() call error : ");
        
return 100;
    }

    stAddr.sin_family 
= AF_INET;
    stAddr.sin_addr.s_addr 
= inet_addr(IP);
    stAddr.sin_port 
= htons(PORT);

    iLen 
= sizeof(struct sockaddr_in);

    iRet = connect(iFd, (struct sockaddr *)&stAddr, iLen);
    
if(-1 == iRet)
    {
        perror(
"connect() call error : ");
        close(iFd);
        
return 200;
    }

    
while(1)
    {
       
 FD_ZERO(&fdRead);
        FD_SET(
0&fdRead);
        FD_SET(iFd, 
&fdRead);

    }

    close(iFd);
    
return 0;
}



'2015 스마트 콘트롤러 > 업무일지' 카테고리의 다른 글

20150820  (0) 2015.08.21
20150819  (0) 2015.08.19
20150817  (0) 2015.08.18
20150814  (0) 2015.08.17
20150813  (0) 2015.08.13
Posted by ahj333
,