작성자: mwyun([멍])
본 강좌에서는 네트웍을 통해 접속한 클라이언트가 전송한 수식데이터를 받아서 서버에서 연산후 그 결과를 다시 클라이언트로 전송하는 C/S 프로그램 예제이다.
지금까지 익한 자료구조와 네트워크 프로그래밍 기법, 계산기 알고리즘을 이용하여 직접해보도록 한다.
클라이언트
calc_clent.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <string.h>
#define MAXLINE 512
#define MAX_SOCK 128
char *escapechar = "exit";
char name[10];
void print_packet(char *buf, int n)
{
int i;
int offset;
for (offset = 0; offset < n; offset += 16)
{
printf("%07d ", offset);
for (i = offset; i <= offset + 15 && i < n; i++)
{
if (buf[i] == '\n') printf(" \\n");
else if (buf[i] == '\r') printf(" \\r");
else if (buf[i] == '\t') printf(" \\t");
else if (buf[i] == '\0') printf(" \\0");
else if (buf[i] == ' ') printf(" ");
else
{
if (buf[i] < '!' || buf[i] >= 127)
printf("%03x", buf[i]);
else
printf("%3c", buf[i]);
}
printf(" ");
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
char line[MAXLINE], message[MAXLINE+1];
int n,pid;
struct sockaddr_in server_addr;
int maxfdp1;
int s;
fd_set read_fds;
if (argc != 4)
{
printf("사용법: %s <calc_server_ip> <port> <user name>\n", argv[0]);
exit(0);
}
sprintf(name, "[%s]", argv[3]);
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
printf("Client: Can't open stream socket.\n");
exit(0);
}
bzero((char *)&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(atoi(argv[2]));
if (connect(s, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
printf("Client: Can't connect to server.\n");
exit(0);
}
else
printf("서버에 접속하였습니다.");
maxfdp1 = s + 1;
FD_ZERO(&read_fds);
while (1)
{
FD_SET(0, &read_fds);
FD_SET(s, &read_fds);
if (select(maxfdp1, &read_fds, (fd_set*)0, (fd_set*)0, (struct timeval *)0) < 0)
{
printf("select error\n");
exit(0);
}
if (FD_ISSET(s, &read_fds))
{
int size;
if ((size = recv(s, message, MAXLINE, 0)) > 0)
{
print_packet(message, size);
message[size] = '\0';
printf("%s", message);
printf("%s ", name);
fflush(stdout);
}
else
{
printf("Good bye.\n");
close(s);
exit(0);
}
}
if (FD_ISSET(0, &read_fds))
{
printf("%s ", name);
if (fgets(message, MAXLINE, stdin))
{
if (message[0] == '#')
sprintf(line, "%s", message);
else
sprintf(line, "%s %s", name, message);
if (send(s, line, strlen(line), 0) < 0)
printf("Error: Write error on socket.\n");
if (strstr(message, escapechar) != NULL)
{
printf("Good bye.\n");
close(s);
exit(0);
}
// fflush(stdin);
}
}
}
return 0;
}
[mwyun@iokorea calc_server]$ ./calc_server 10000
수신 대기중...
1 번째 계산기 사용자 추가.
수신 대기중...
0000000 [ m w y u n ] h e l l o \n
[mwyun] hello
수신 대기중...
0000000 # 1 0 - ( 4 / 2 ) + 3 \n
infix expr: 10.000000 - ( 4.000000 / 2.000000 ) + 3.000000
postfix expr: 10.000000 4.000000 2.000000 / - 3.000000 +
= 11.000000
= 11.000000
수신 대기중...
0000000 [ m w y u n ] e x i t \n
계산기 사용자 1명 탈퇴. 현재 접속자 수 = 0
수신 대기중...
클라이언트 실행화면
[mwyun@iokorea calc_client]$ ./calc_client 127.0.0.1 10000 mwyun
서버에 접속하였습니다.0000000 C o n n e c t e d t o c a l
0000016 c _ s e r v e r \n
Connected to calc_server
[mwyun] hello
[mwyun] #10-(4/2)+3
[mwyun] 0000000 = 1 1 . 0 0 0 0 0 0 \n
= 11.000000
[mwyun] exit
[mwyun] Good bye.
[mwyun@iokorea calc_client]$
Contents
채팅&계산기 C/S 프로그램
클라이언트
calc_clent.c
컴파일
실행
서버
calc_server.c
calc_module.c
컴파일
실행
데모
서버 실행화면
클라이언트 실행화면
참조
Recent Posts
Archive Posts
Tags