ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 20140616 (TCP/IP client, server 소스)
    부산IT학원/스마트컨트롤러 2014. 6. 16. 17:06

    81일차












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

    TCP/IP 소켓 프로그래밍 client, server

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




    --- client 소스 흐름


    socket()

    connect()

    close()





    - server 소스 흐름


    socket()

    bind()

    listen()

    accpet()

    close()






    --- bind

    소켓에 저장된 소켓 주소를 넣는다.


    #include <sys/types.h>

    #include <sys/socket.h>


    int  bind(int  sockfd, struct sockaddr *my_addr, socklen_t addrlen);


    sockfd : 소켓 번호

    my_addr : 소켓 주소 정보

    addrlen : 소켓 주소 정보 길이


    리턴값 : 성공 시 0, 실패 시 -1 반환되며 errno에 적당한 값이 설정됨.





    --- errno

    Error Number라는 뜻으로. 오류시 오류의 원인과 관련된 번호가 설정된다.


    #include <errno.h>


    extern int errno;  // 전역변수가 선언되어 있음





    -- perror

    errno 관련 함수


    #include <stdio.h>

    #include <errno.h>


    void perror(const char *s);





    --- listen
    소켓에서 연결들을 기다린다.

    #include <sys/socket.h>

    int listen(int s, int backlog);


    s : 소켓번호

    backlog : 보통 5를 줌.


    대충 요런 역할이다.






    --- accept

    소켓에 연결을 허용한다.


    #include <sys/types.h>

    #include <sys/socket.h>


    int accept(int   s,  struct  sockaddr  *addr,  socklen_t *addrlen);


    s : 소켓번호

    addr : client 주소 정보 저장할 곳

    addrlen : 정보 크기



    리턴 값 : 연결된 client 소켓 번호가 리턴됨. 에러 시 -1.











    --- 예제 소스


    - client.c


    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/stat.h>
    #include <arpa/inet.h>
    #include <errno.h>


    int main()
    {
      int iSock;
      int iRet;
      struct sockaddr_in stAddr;

    // 소켓 생성
      iSock = socket(AF_INET, SOCK_STREAM, 0);
      if(-1 == iSock)
      {
        perror("socket() ");
        return -10;
      }

    // 소켓 정보 부여
      stAddr.sin_family = AF_INET;
      stAddr.sin_port = htons(PORT);
      stAddr.sin_addr.s_addr = inet_addr("192.168.10.250");

    // 연결
      iRet = connect(iSock, (struct sockaddr *)&stAddr, sizeof(stAddr));
      if(-1 == iRet)
      {
        perror("connect() ");
        close(iSock);
        return -11;
      }

    // 내용 전송
      printf("iSock : %d\n", iSock);


      close(iSock);
      
      return 0;
    }




    - server.c


    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/stat.h>
    #include <arpa/inet.h>
    #include <errno.h>


    int main()
    {
      int iSock;
      int iClient1;
      int iRet;
      unsigned int uiSize;
      struct sockaddr_in stAddr;
      struct sockaddr_in stClient1;

    // 소켓 생성
      iSock = socket(AF_INET, SOCK_STREAM, 0);
      if(-1 == iSock)
      {
        perror("socket() ");
        return -10;
      }

    // 주소 부여
      stAddr.sin_family = AF_INET;
      stAddr.sin_port = htons(PORT);
      stAddr.sin_addr.s_addr = inet_addr("192.168.10.250");

    // 소켓에 주소 설정
      iRet = bind(iSock, (struct sockaddr *)&stAddr, sizeof(stAddr));
      if(-1 == iRet)
      {
        perror("bind() ");
        close(iSock);
        return -11;
      }

    // 연결 대기
      iRet = listen(iSock, 5);
      if(-1 == iRet)
      {
        perror("listen() ");
        close(iSock);
        return -12;
      }

    // 연결 허용
      uiSize = sizeof(stClient1);
      iClient1 = accept(iSock, (struct sockaddr *)&stClient1, &uiSize);
      if(-1 == iClient1)
      {
        perror("accept() ");
        close(iSock);
        return -13;
      }

    // 내용
      printf("iSock : %d\n", iSock);
      printf("iClient1 : %d\n", iClient1);


      close(iSock);
      close(iClient1);
      
      return 0;
    }


Designed by Tistory.