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

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
,