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

NETWORK

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


<NETWORK>



=접속자수 제한 ( 임의로 3명으로 정함 )






=4명째 접속시 문제 발생 => 서버에서 client 강제로 종료되도록 수정 필요







=>수정 후















=서버 종료키 입력시 모든 클라이언트 종료되도록 수정







=PORT번호 입력 실행


=서버가 ctrl + c 클라이언트 문제 발생(메세지 무한 출력)

ctrl + c 값 => 0








=>수정 후







=클라이언트에서 ctrl + c입력시 서버 종료







=> 수정 후







<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 NIC_NAME_MSG    (9+2)
#define BUF_SIZE    (255)
#define MSG_SIZE    (BUF_SIZE + 1 + NIC_NAME_MSG)
#define MSG_END     "\x01\x02\x03"//\x뒤는 아스키코드 헥사번호로 인식 //"quit"
#define MAX_USER    3
#define NIC_NAME_SIZE   9
#endif //__SMARTSOCK2_H__


<server2.c>

#include "smartsock2.h"

int main(int iRet, char *cpCmd[])
{
  
int iSock; //socket 생성
  struct sockaddr_in stAddr; //socket의 주소
  //int iRet; //error 값은 저장한다.
  int iCSock[MAX_USER+1]; //새로운 소켓을 저장할 변수이다.
  unsigned int uiUser;
  socklen_t uiSockLen 
= sizeof(struct sockaddr); //unsigned int이다.
  char cBuffer[BUF_SIZE]; //echo서버를 위한 변수
  char cMsg[MSG_SIZE];
  fd_set fdRead;
  
unsigned int uiCnt;
  
unsigned int uiCnt2;
  
int iMSock;
  
char cNick[MAX_USER][NIC_NAME_SIZE];
  
unsigned short usPort = PORT;


  if(2 == iRet)
  {
    iSock 
= atoi(cpCmd[1]);
    
if(1024 < iSock)
    {
      
if(65535 > iSock)
      {
        usPort 
=
 iSock;
      }
    }
  }

  printf(
"PORT: %d\n", usPort);
  iSock 
= socket(AF_INET, SOCK_STREAM, 0); //socket 생성
  if(iSock < 0//socket 에러 확인
  {
    perror(
"socket() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 10;
  }

  bzero(
&stAddr, sizeof(stAddr)); //구조체를 지운다.
  stAddr.sin_family = AF_INET;    //
  stAddr.sin_port = htons(usPort);          //고정 PORT X
  stAddr.sin_addr.s_addr = inet_addr(IP); //고정 IP

  iRet = bind(iSock, (struct sockaddr *)&stAddr, sizeof(stAddr));
  
if(iRet < 0//bind 에러 확인
  {
    perror(
"bind() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 15;
  }

  iRet 
= listen(iSock, 5);
  
if(iRet < 0)
  {
    perror(
"listen() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 20;
  }
  uiUser 
= 0;
  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET(iSock, 
&fdRead);
    iMSock 
= iSock;
    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      FD_SET(iCSock[uiCnt], 
&fdRead);
      
if(iMSock < iCSock[uiCnt])
      {
        iMSock 
= iCSock[uiCnt];
      }
    }
    select(iMSock+
1&fdRead, 000);
    
if(0 != FD_ISSET(iSock, &fdRead))
    {
      iCSock[uiUser] 
= accept(iSock, (struct sockaddr*)&stAddr, &uiSockLen);
      
if(iCSock[uiUser] < 0)
      {
        perror(
"accept() error : ");
        
continue;
      }
      if(MAX_USER <= uiUser)
      {
        read(iCSock[uiUser], cBuffer, NIC_NAME_SIZE);
        sprintf(cMsg, 
"Server is FULL.");
        write(iCSock[uiUser], cMsg, 
sizeof(cMsg));
        write(iCSock[uiUser], MSG_END, 
sizeof(MSG_END));
        close(iCSock[uiUser]);
        printf(
"Server is FULL : [%s]\n", cBuffer);
        printf(
"client IP : [%s]\n", inet_ntoa(stAddr.sin_addr)); //client IP
        continue;
      }

      read(iCSock[uiUser], cNick[uiUser], NIC_NAME_SIZE);
      printf(
"incoming client: [%s]\n", cNick[uiUser]); //누군가 접근중이다.
      printf("client IP : [%s]\n", inet_ntoa(stAddr.sin_addr)); //client IP
      write(iCSock[uiUser], "Welcome!"sizeof("Welcome!"));
      sprintf(cMsg, 
"[%s]님이 입장하셨습니다.", cNick[uiUser]);
      
for(uiCnt2 = 0; uiCnt2 < uiUser ;++uiCnt2)
      {
        write(iCSock[uiCnt2], cMsg, MSG_SIZE);
      }
      ++uiUser;
      printf(
"%s\n", cMsg);
    }

    
if(0 != FD_ISSET(0&fdRead))
    {
      iRet = read(0, cBuffer, BUF_SIZE);
      
if(0 == iRet)
      {
        
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
        {
          sprintf(cMsg, 
"Server is ENDING.");
          write(iCSock[uiCnt], cMsg, 
sizeof(cMsg));
          write(iCSock[uiCnt], MSG_END, 
sizeof(MSG_END));
        }
        
break;
      }

      cBuffer[iRet-
1= 0;
      sprintf(cMsg, 
"공지사항: [%s]", cBuffer);
      
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
      {
        write(iCSock[uiCnt], cMsg, MSG_SIZE);
      }
      printf(
"%s\n", cMsg);
    }

    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      
if(0 != FD_ISSET(iCSock[uiCnt], &fdRead))
      {
        
iRet = read(iCSock[uiCnt], cMsg, MSG_SIZE);

        
//if(0 == strncmp(MSG_END, cMsg+strlen(cNick[uiCnt])+2, strlen(MSG_END)))
        if(
          (
0 == strncmp(MSG_END, cMsg, sizeof(MSG_END)))
         ||
         (0 == iRet)
          )
        {
          sprintf(cMsg, 
"[%s]님이 퇴장하셨습니다.", cNick[uiCnt]);
          close(iCSock[uiCnt]);
          --uiUser;
          iCSock[uiCnt] 
=
 iCSock[uiUser];
          memcpy(cNick[uiCnt],cNick[uiUser], NIC_NAME_SIZE);
        }  

        
for(uiCnt2 = 0; uiCnt2 < uiUser ;++uiCnt2)
        {
          write(iCSock[uiCnt2], cMsg, MSG_SIZE);
        }

        printf(
"%s\n", cMsg);
      }
    }
  }

  
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
  {
    close(iCSock[uiCnt]);
  }
  close(iSock);  
//
  return 0;
}


<client2.c>

#include "smartsock2.h"

int main(int iRet, char *cpCmd[])
{
  
int iFd;
  
struct sockaddr_in stAddr;
  
int iLen;
 
 //int iRet;
  fd_set fdRead;
  
char cBuffer[BUF_SIZE];
  
char cMsg[MSG_SIZE];
  
char cNick[NIC_NAME_SIZE];
  
unsigned short usPort = PORT;


  
if(2 == iRet)
  {
    iFd 
= atoi(cpCmd[1]);
    
if(1024 < iFd)
    {
      
if(65535 > iFd)
      {
        usPort 
= iFd;
      }
    }
  }
  printf(
"PORT: %d\n", usPort);

  
while(1)
  {
    printf(
"Input Nickname: ");
    fflush(stdout);
    iRet 
= read(0, cNick, NIC_NAME_SIZE);
    
if(2 > iRet) // ctrl+d : 0 enter : 1
    {
      
continue;
    }
    cNick[iRet - 
1= 0;
    
break;
  }

  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(usPort);

  iLen 
= sizeof(struct sockaddr_in);

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

  write(iFd, cNick, NIC_NAME_SIZE);

  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET(iFd, 
&fdRead);
    select(iFd+
1&fdRead, 000);
    
if(0 != FD_ISSET(0&fdRead))
    {
      iRet = read(0, cBuffer, BUF_SIZE);
      
if(0 == iRet)
      {
        write(iFd, MSG_END, 
sizeof(MSG_END));
        
break;
      }

      cBuffer[iRet - 
1= 0;
      sprintf(cMsg, 
"[%s]%s", cNick, cBuffer);
      write(iFd, cMsg, MSG_SIZE);
      printf(
"[Send MSG] : [%s]\n", cBuffer);

    }
    
if(0 != FD_ISSET(iFd, &fdRead))
    {
      iRet = read(iFd, cMsg, MSG_SIZE);//iFd:네트워크
      if(0 == iRet)
      {
        printf(
"Server Error.\n");
        
break;
      }

      
if(0 == strncmp(MSG_END, cMsg, sizeof(MSG_END)))
      {
        
break;
      }
      printf(
"%s\n", cMsg);
    }

  }
  close(iFd);

  
return 0;
}




=채팅 업그레이드


-채팅 방 여러개로 : 멀티룸 채팅



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

20150831  (0) 2015.08.31
20150828  (0) 2015.08.30
20150826  (0) 2015.08.26
20150825  (0) 2015.08.25
20150824  (0) 2015.08.24
Posted by ahj333
,

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

NETWORK

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

<NETWORK>


=“quit” 입력한 클라이언트만 종료되도록 수정


<client2.c>

#include "smartsock2.h"

int main(int argc, char **argv)
{
    
int iFd;
    
struct sockaddr_in stAddr;
    
int iLen;
    
int iRet;
    fd_set fdRead;
    
char cBuffer[BUF_SIZE];

    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);
        select(iFd+
1&fdRead, 000);
       
 if(0 != FD_ISSET(0&fdRead))
        
{
            iRet 
= read(0, cBuffer, MSG_SIZE);
            cBuffer[iRet - 
1= 0;
            write(iFd, cBuffer, MSG_SIZE);
            printf(
"[Send MSG] : [%s]\n", cBuffer);
            if(0 == strncmp(cBuffer, MSG_END, strlen(MSG_END)))
            {
                
break;
            }

       
 }

        
if(0 != FD_ISSET(iFd, &fdRead))
        {
            read(iFd, cBuffer, MSG_SIZE);
//iFd:네트워크
            printf("[Server MSG] : [%s] \n", cBuffer);
        }

    }

    close(iFd);

    
return 0;
}







=닉네임 반영

=fflush(stdin); 동작 X => 버퍼 안비워짐






=

<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 NIC_NAME_MSG  (9+2)
#define BUF_SIZE    (255)
#define MSG_SIZE    (BUF_SIZE + 1 + NIC_NAME_MSG)
#define MSG_END     "\x01\x02\x03"//\x뒤는 아스키코드 헥사번호로 인식 //"quit"

#define MAX_USER  30
#define NIC_NAME_SIZE  9
#endif //__SMARTSOCK2_H__


<server2.c>

#include "smartsock2.h"

int main(void)
{
  
int iSock; //socket 생성
  struct sockaddr_in stAddr; //socket의 주소
  int iRet; //error 값은 저장한다.
  int iCSock[MAX_USER]; //새로운 소켓을 저장할 변수이다.
  unsigned int uiUser;
  socklen_t uiSockLen 
= sizeof(struct sockaddr); //unsigned int이다.
  char cBuffer[BUF_SIZE]; //echo서버를 위한 변수
  char cMsg[MSG_SIZE];
  iSock 
= socket(AF_INET, SOCK_STREAM, 0); //socket 생성
  fd_set fdRead;
  
unsigned int uiCnt;
  
unsigned int uiCnt2;
  
int iMSock;
  
char cNick[MAX_USER][NIC_NAME_SIZE];

  
if(iSock < 0//socket 에러 확인
  {
    perror(
"socket() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 10;
  }

  bzero(
&stAddr, sizeof(stAddr)); //구조체를 지운다.
  stAddr.sin_family = AF_INET;    //
  stAddr.sin_port = htons(PORT);          //고정 PORT
  stAddr.sin_addr.s_addr = inet_addr(IP); //고정 IP

  iRet = bind(iSock, (struct sockaddr *)&stAddr, sizeof(stAddr));
  
if(iRet < 0//bind 에러 확인
  {
    perror(
"bind() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 15;
  }

  iRet 
= listen(iSock, 5);
  
if(iRet < 0)
  {
    perror(
"listen() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 20;
  }
  uiUser 
= 0;
  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET(iSock, 
&fdRead);
    iMSock 
= iSock;
    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      FD_SET(iCSock[uiCnt], 
&fdRead);
      
if(iMSock < iCSock[uiCnt])
      {
        iMSock 
= iCSock[uiCnt];
      }
    }
    select(iMSock+
1&fdRead, 000);
    
if(0 != FD_ISSET(iSock, &fdRead))
    {
      iCSock[uiUser] 
= accept(iSock, (struct sockaddr*)&stAddr, &uiSockLen);
      
if(iCSock[uiUser] < 0)
      {
        perror(
"accept() error : ");
        
continue;
      }
      
read(iCSock[uiUser], cNick[uiUser], NIC_NAME_SIZE);
      printf(
"incoming client: [%s]\n", cNick[uiUser]); //누군가 접근중이다.
      printf("client IP : [%s]\n", inet_ntoa(stAddr.sin_addr)); //client IP
      write(iCSock[uiUser], "Welcome!"sizeof("Welcome!"));
      sprintf(cMsg, 
"[%s]님이 입장하셨습니다.", cNick[uiUser]);
    
  for(uiCnt2 = 0; uiCnt2 < uiUser ;++uiCnt2)
      {
        write(iCSock[uiCnt2], cMsg, MSG_SIZE);
      }
      ++uiUser;

      printf(
"%s\n", cMsg);
    }

    
if(0 != FD_ISSET(0&fdRead))
    {
      iRet 
= read(0, cBuffer, BUF_SIZE);
      cBuffer[iRet-
1= 0;
      
sprintf(cMsg, "공지사항: [%s]", cBuffer);
      
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
      {
        write(iCSock[uiCnt], cMsg, MSG_SIZE);
      }
      printf(
"%s\n", cMsg);
    }

    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      
if(0 != FD_ISSET(iCSock[uiCnt], &fdRead))
      {
        read(iCSock[uiCnt], cMsg, MSG_SIZE);
        
//if(0 == strncmp(MSG_END, cMsg+strlen(cNick[uiCnt])+2, strlen(MSG_END)))
        if(0 == strncmp(MSG_END, cMsg, sizeof(MSG_END)))
        {
          sprintf(cMsg, 
"[%s]님이 퇴장하셨습니다.", cNick[uiCnt]);
          close(iCSock[uiCnt]);
          --uiUser;
          iCSock[uiCnt] 
= iCSock[uiUser];
          memcpy(cNick[uiCnt],cNick[uiUser], NIC_NAME_SIZE);
        }  

        
for(uiCnt2 = 0; uiCnt2 < uiUser ;++uiCnt2)
        {
          write(iCSock[uiCnt2], cMsg, MSG_SIZE);
        }
      
        printf(
"%s\n", cMsg);
      }
    }
  }

  
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
  {
    close(iCSock[uiCnt]);
  }
  close(iSock);  
//
  return 0;
}


<client2.c>

#include "smartsock2.h"

int main(int argc, char **argv)
{
  
int iFd;
  
struct sockaddr_in stAddr;
  
int iLen;
  
int iRet;
  fd_set fdRead;
  
char cBuffer[BUF_SIZE];
  
char cMsg[MSG_SIZE];
  
char cNick[NIC_NAME_SIZE];

  
while(1)
  {
    printf(
"Input Nickname: ");
    fflush(stdout);
    iRet 
= read(0, cNick, NIC_NAME_SIZE);
    
if(2 > iRet) // ctrl+d : 0 enter : 1
    {
      
continue;
    }
    cNick[iRet - 
1= 0;
    
break;
  }

  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;
  }

  write(iFd, cNick, NIC_NAME_SIZE);

  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET(iFd, 
&fdRead);
    select(iFd+
1&fdRead, 000);
    
if(0 != FD_ISSET(0&fdRead))
    {
      iRet 
= read(0, cBuffer, BUF_SIZE);
      
if(0 == iRet) //종료문자 입력 시 종료
      {

        
break;
      }

      cBuffer[iRet - 
1= 0;
      sprintf(cMsg, 
"[%s]%s", cNick, cBuffer);
      write(iFd, cMsg, MSG_SIZE);
      printf(
"[Send MSG] : [%s]\n", cBuffer);
    
    }
    
if(0 != FD_ISSET(iFd, &fdRead))
    {
      read(iFd, cMsg, MSG_SIZE);
//iFd:네트워크
      printf("%s\n", cMsg);
    }

  }
  write(iFd, MSG_END, 
sizeof(MSG_END));
  close(iFd);

  
return 0;
}





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

20150828  (0) 2015.08.30
20150828  (0) 2015.08.28
20150825  (0) 2015.08.25
20150824  (0) 2015.08.24
20150821  (0) 2015.08.23
Posted by ahj333
,

<NETWORK>


<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"
#define MAX_USER  30
#endif //__SMARTSOCK2_H__


<client2.c>

#include "smartsock2.h"

int main(int argc, char **argv)
{
  
int iFd;
  
struct sockaddr_in stAddr;
  
int iLen;
  
int iRet;
  fd_set fdRead;
  
char cBuffer[BUF_SIZE];

  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);
    select(iFd+
1&fdRead, 000);
    
if(0 != FD_ISSET(0&fdRead))
    {
      iRet 
= read(0, cBuffer, MSG_SIZE);
      cBuffer[iRet - 
1= 0;
      write(iFd, cBuffer, MSG_SIZE);
      printf(
"[Send MSG] : [%s]\n", cBuffer);
    }
    
if(0 != FD_ISSET(iFd, &fdRead))
    {
      read(iFd, cBuffer, MSG_SIZE);
//iFd:네트워크
      printf("[Server MSG] : [%s] \n", cBuffer);
    }
    
if(0 == strncmp(cBuffer, MSG_END, strlen(MSG_END)))
    {
      
break;
    }
  }

  close(iFd);

  
return 0;
}


<server2.c>


#include "smartsock2.h"

int main(void)
{
  
int iSock; //socket 생성
  struct sockaddr_in stAddr; //socket의 주소
  int iRet; //error 값은 저장한다.
  int iCSock[MAX_USER]; //새로운 소켓을 저장할 변수이다.
  unsigned int uiUser;
  socklen_t uiSockLen 
= sizeof(struct sockaddr); //unsigned int이다.
  char cBuffer[BUF_SIZE]; //echo서버를 위한 변수
  iSock = socket(AF_INET, SOCK_STREAM, 0); //socket 생성
  fd_set fdRead;
  unsigned int uiCnt;
  
unsigned int uiCnt2;
  
int iMSock;


  
if(iSock < 0//socket 에러 확인
  {
    perror(
"socket() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 10;
  }

  bzero(
&stAddr, sizeof(stAddr)); //구조체를 지운다.
  stAddr.sin_family = AF_INET;    //
  stAddr.sin_port = htons(PORT);          //고정 PORT
  stAddr.sin_addr.s_addr = inet_addr(IP); //고정 IP

  iRet = bind(iSock, (struct sockaddr *)&stAddr, sizeof(stAddr));
  
if(iRet < 0//bind 에러 확인
  {
    perror(
"bind() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 15;
  }

  iRet 
= listen(iSock, 5);
  
if(iRet < 0)
  {
    perror(
"listen() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 20;
  }
  uiUser 
= 0;
  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET(iSock, 
&fdRead);
    
iMSock = iSock;
    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      FD_SET(iCSock[uiCnt], 
&fdRead);
      
if(iMSock < iCSock[uiCnt])
      {
        iMSock 
= iCSock[uiCnt];
      }
    }
    select(iMSock+
1&fdRead, 000);
    
if(0 != FD_ISSET(iSock, &fdRead))
    {
      iCSock[uiUser] 
= accept(iSock, (struct sockaddr*)&stAddr, &uiSockLen);
      
if(iCSock[uiUser] < 0)
      {
        perror(
"accept() error : ");
        
continue;
      }
      printf(
"incoming client\n"); //누군가 접근중이다.
      printf("client IP : [%s]\n", inet_ntoa(stAddr.sin_addr)); //client IP
      write(iCSock[uiUser], "Welcome!"sizeof("Welcome!"));
      ++uiUser;
    }

    
if(0 != FD_ISSET(0&fdRead))
    {
      iRet 
= read(0, cBuffer, MSG_SIZE);
      cBuffer[iRet-
1= 0;
      
      
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
      {
        write(iCSock[uiCnt], cBuffer, MSG_SIZE);
      }
    }
    
    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      
if(0 != FD_ISSET(iCSock[uiCnt], &fdRead))
      {
        read(iCSock[uiCnt], cBuffer, MSG_SIZE);
        
for(uiCnt2 = 0; uiCnt2 < uiUser ;++uiCnt2)
        {
          
if(uiCnt != uiCnt2)  // 보낸곳 제외
            write(iCSock[uiCnt2], cBuffer, MSG_SIZE);
        }
        
if(0 == strncmp(MSG_END, cBuffer, strlen(MSG_END)))
        {
          close(iCSock[uiCnt]);
          --uiUser;
          iCSock[uiCnt] 
= iCSock[uiUser];
          
        }
      }
    }
  }
    
  
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
  {
    close(iCSock[uiCnt]);
  }
  close(iSock);  
//

  return 0;
}






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

20150828  (0) 2015.08.28
20150826  (0) 2015.08.26
20150824  (0) 2015.08.24
20150821  (0) 2015.08.23
20150820  (0) 2015.08.21
Posted by ahj333
,

<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() + 채팅 프로그램 : 멀티 프로세스 대신 사용


<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"
#define MAX_USER  30
#endif //__SMARTSOCK2_H__


<client2.c>

#include "smartsock2.h"

int main(int argc, char **argv)
{
  
int iFd;
  
struct sockaddr_in stAddr;
  
int iLen;
  
int iRet;
  fd_set fdRead;
  
char cBuffer[BUF_SIZE];

  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);
    select(iFd+
1&fdRead, 000);
    
if(0 != FD_ISSET(0&fdRead))
    {
      iRet 
= read(0, cBuffer, MSG_SIZE);
      cBuffer[iRet - 
1= 0;
      write(iFd, cBuffer, MSG_SIZE);
      printf(
"[Send MSG] : [%s]\n", cBuffer);
    }
    
if(0 != FD_ISSET(iFd, &fdRead))
    {
      read(iFd, cBuffer, MSG_SIZE);
//iFd:네트워크
      printf("[Server MSG] : [%s] \n", cBuffer);
    }
    
if(0 == strncmp(cBuffer, MSG_END, strlen(MSG_END)))
    {
      
break
;
    }

  }

  close(iFd);

  
return 0;
}


<server2.c>

#include "smartsock2.h"

int main(void)
{
  
int iSock; //socket 생성
  struct sockaddr_in stAddr; //socket의 주소
  int iRet; //error 값은 저장한다.
  int iCSock[MAX_USER]; //새로운 소켓을 저장할 변수이다.
  unsigned int uiUser;
  socklen_t uiSockLen 
= sizeof(struct sockaddr); //unsigned int이다.
  char cBuffer[BUF_SIZE]; //echo서버를 위한 변수
  iSock = socket(AF_INET, SOCK_STREAM, 0); //socket 생성
  fd_set fdRead;
  
unsigned int uiCnt;

  
if(iSock < 0//socket 에러 확인
  {
    perror(
"socket() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 10;
  }

  bzero(
&stAddr, sizeof(stAddr)); //구조체를 지운다.
  stAddr.sin_family = AF_INET;    //
  stAddr.sin_port = htons(PORT);          //고정 PORT
  stAddr.sin_addr.s_addr = inet_addr(IP); //고정 IP

  iRet = bind(iSock, (struct sockaddr *)&stAddr, sizeof(stAddr));
  
if(iRet < 0//bind 에러 확인
  {
    perror(
"bind() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 15;
  }

  iRet 
= listen(iSock, 5);
  
if(iRet < 0)
  {
    perror(
"listen() error : ");
    close(iSock); 
//리턴하기 전에 close도 해주어야한다.
    return 20;
  }
  uiUser 
= 0;
  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET(iSock, 
&fdRead);
    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      FD_SET(iCSock[uiUser], 
&fdRead,0,0,0);
    }
    select(iSock+
1&fdRead, 000);
    
if(0 != FD_ISSET(iSock, &
fdRead))
    {
      iCSock[uiUser] 
= accept(iSock, (struct sockaddr*)&stAddr, &uiSockLen);
      
if(iCSock[uiUser] < 0)
      {
        perror(
"accept() error : ");
        
continue;
      }
      ++uiUser;
    }
  }

  close(iSock);  
//
  printf("incoming client\n"); //누군가 접근중이다.

  printf("client IP : [%s]\n", inet_ntoa(stAddr.sin_addr)); //client IP
  printf("client PORT : [%d]\n", ntohs(stAddr.sin_port)); //client PORT

  write(iCSock, "Welcome!"sizeof("Welcome!"));

  
while(1)
  {
    read(iCSock, cBuffer, MSG_SIZE); 
//
    printf("[Client]: [%s]\n", cBuffer);
    write(iCSock, cBuffer, MSG_SIZE);
    
if(0 == strncmp(MSG_END, cBuffer, strlen(MSG_END))) //
    {
      
break;
    }
  }

  close(iCSock);

  
return 0;
}











=정리필요

uiUser

iCSock




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

20150826  (0) 2015.08.26
20150825  (0) 2015.08.25
20150821  (0) 2015.08.23
20150820  (0) 2015.08.21
20150819  (0) 2015.08.19
Posted by ahj333
,

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

ASSEMBLY

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


<ASSEMBLY>



=DEBUG


ml /c /coff /Zi 파일명

link /debug /subsystem:console /entry:start /out:실행파일명 obj파일명 io.obj kernel32.lib







=Windbg.exe 실행






=Step Into로 코드 진행




=View - Register




=View - Memory : 시작 주소 지정






























=Listing File


ml /c /coff /Fi 소스파일명










<main.lst>




=상수 Operands



(대소문자 구분 x)


mask   BYTE  01111101b

mask   BYTE  7dh

mask   BYTE  125

mask   BYTE  175q



-

char1 BYTE 'm' ; value is 6D

char2 BYTE 6dh ; value is 6D

string1 BYTE "Joe" ; value is 4A 6F 65

string2 BYTE "Joe's" ; value is 4A 6F 65 27 73


words WORD 10, 20, 30, 40

DblArray DWORD 100 DUP(999)


=Instruction Operands


mov    al, '/'  ; al = '/';






=Coping Data


mov    Count  Number ; illegal => cpu를 거치지 않고서는 불가능 함



int Number = 100;

int Count;


Count = Number;


=>

mov    eax    , Number

mov    Count  , eax



=MOV명령어 뒤 오는 OP1, OP2 종류 설명 표

-

mov    REG   MEM   

                REG

                IMM






-

mov    MEM   REG

                IMM






-




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

20150825  (0) 2015.08.25
20150824  (0) 2015.08.24
20150820  (0) 2015.08.21
20150819  (0) 2015.08.19
20150818  (0) 2015.08.19
Posted by ahj333
,