반응형
    
    
    
  - 본 내용은 Linux (Ubuntu 14.04 lts)를 기반으로 제작되었습니다. -
소캣 프로그래밍은 TCP/IP 기반으로 하였습니다.
뮤텍스, 세마포어 멀티 스레드 통신의 기반이 되는 서버 클라이언트 프로그램이다.
소켓 프로그래밍 - (24) 뮤텍스(Mutex)를 이용한 멀티 스레드 통신 :: http://www.crocus.co.kr/528
소켓 프로그래밍 - (25) 세마포어(Semaphore)를 이용한 멀티 스레드 통신 :: http://www.crocus.co.kr/529
이 두가지를 만들기 전, 어떤 틀을 이용하여 제작하였는지 보여주고자 한다.
간단한 코드이고, 이전 게시물에서 이 코드에 대한 자세한 내용들은 확인이 가능하다.
< tcp.h >
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  | //tcp.h #include <iostream> #include <pthread.h> #include <queue> #include <semaphore.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #define PORT 20162 #define BUFFER_SIZE 100 #define LISTEN_QUEUE_SIZE 5 using namespace std; //                                                       This source code Copyright belongs to Crocus //                                                        If you want to see more? click here >>  | Crocus | 
< server.c >
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100  | //server.cpp # include "tcp.h" int main() {     struct sockaddr_in listenSocket;     memset(&listenSocket, 0, sizeof(listenSocket));     listenSocket.sin_family = AF_INET;     listenSocket.sin_addr.s_addr = htonl(INADDR_ANY);     listenSocket.sin_port = htons(PORT);     int listenFD = socket(AF_INET, SOCK_STREAM, 0);     int connectFD;     ssize_t receivedBytes;     char readBuff[BUFFER_SIZE];     char sendBuff[BUFFER_SIZE];     if (bind(listenFD, (struct sockaddr *) &listenSocket, sizeof(listenSocket)) == -1) {         printf("Can not bind.\n");         return -1;     }     if (listen(listenFD, LISTEN_QUEUE_SIZE) == -1) {         printf("Listen fail.\n");         return -1;     }     printf("Waiting for clients...\n");     while (1)      {         struct sockaddr_in connectSocket, peerSocket;         socklen_t connectSocketLength = sizeof(connectSocket);         while((connectFD = accept(listenFD, (struct sockaddr*)&connectSocket, (socklen_t *)&connectSocketLength)) >= 0)         {             getpeername(connectFD, (struct sockaddr*)&peerSocket, &connectSocketLength);             char peerName[sizeof(peerSocket.sin_addr) + 1] = { 0 };             sprintf(peerName, "%s", inet_ntoa(peerSocket.sin_addr));             // 접속이 안되었을 때는 출력 x             if(strcmp(peerName,"0.0.0.0") != 0)                 printf("Client : %s\n", peerName);             if (connectFD < 0)             {                 printf("Server: accept failed\n");                 exit(0);             }             ssize_t receivedBytes;         int n[2];         int sum = 0;         int cnt = 0;             while(cnt < 2)             {                    receivedBytes = read(connectFD, readBuff, BUFFER_SIZE);                 printf("%lu bytes read\n", receivedBytes);                 readBuff[receivedBytes] = '\0';                // fputs(readBuff, stdout);                 fflush(stdout);         n[cnt] = atoi(readBuff);         printf("n :: %d\n",n[cnt]);         sum += n[cnt];              cnt++;         if(cnt == 2)                {                        printf("sum :: %d\n",sum);                     sprintf(sendBuff,"%d",sum);                       write(connectFD, sendBuff, strlen(sendBuff));                 }             }             close(connectFD);         }     }     close(listenFD);     return 0; } //                                                       This source code Copyright belongs to Crocus //                                                        If you want to see more? click here >>  | Crocus | 
< client.c >
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73  | //client.cpp # include "tcp.h" int main(int argc, char** argv) {     if (argc != 2)      {         printf("Usage: %s IPv4-address\n", argv[0]);         return -1;     }     struct sockaddr_in connectSocket;     memset(&connectSocket, 0, sizeof(connectSocket));     connectSocket.sin_family = AF_INET;     inet_aton(argv[1], (struct in_addr*) &connectSocket.sin_addr.s_addr);     connectSocket.sin_port = htons(PORT);     int connectFD = socket(AF_INET, SOCK_STREAM, 0);     if (connect(connectFD, (struct sockaddr*) &connectSocket, sizeof(connectSocket)) == -1)      {         printf("Can not connect.\n");         return -1;     }     else      {         int readBytes, writtenBytes;         char sendBuffer[BUFFER_SIZE];         char receiveBuffer[BUFFER_SIZE];             //서버에 문자열을 보낸 뒤 서버가 보낸 echo를 받아 출력.             printf("수 1 :: ");             fgets(sendBuffer,BUFFER_SIZE,stdin);             write(connectFD, sendBuffer, strlen(sendBuffer));             printf("수 2 :: ");             fgets(sendBuffer,BUFFER_SIZE,stdin);             write(connectFD, sendBuffer, strlen(sendBuffer));             readBytes = read(connectFD, receiveBuffer, BUFFER_SIZE);             printf("%d bytes read\n", readBytes);             receiveBuffer[readBytes] = '\0';         if(readBytes != 100)             printf("서버에서 계산한 두 수의 합 :: %s\n",receiveBuffer);           fputs(receiveBuffer, stdout);             fflush(stdout);     }     close(connectFD);     return 0; }     //                                                       This source code Copyright belongs to Crocus //                                                        If you want to see more? click here >>  | Crocus | 
반응형
    
    
    
  'Applied > Network' 카테고리의 다른 글
| 소켓 프로그래밍 - (27) I/O Multiplexing Select를 이용한 통신 (0) | 2016.11.20 | 
|---|---|
| 소켓 프로그래밍 - (26) I/O Multiplexing Select 개념 및 소스코드 (0) | 2016.11.20 | 
| 소켓 프로그래밍 - (25) 세마포어(Semaphore)를 이용한 멀티 스레드 통신 (0) | 2016.11.17 | 
| 소켓 프로그래밍 - (24) 뮤텍스(Mutex)를 이용한 멀티 스레드 통신 (0) | 2016.11.17 | 
| 소켓 프로그래밍 - (23) Mutex Condition 개념 및 소스 코드 (6) | 2016.11.11 |