echo_selectserv.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/select.h>
#define BUF_SIZE 100
#define SERVER_IP INADDR_ANY
#define SERVER_PORT "9987"
void error_handling(char *buf);
int main(/*int argc, char *argv[]*/)
{
int serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
struct timeval timeout;
fd_set reads, cpy_reads;
socklen_t adr_sz;
int fd_max, str_len, fd_num, i;
char buf[BUF_SIZE];
/*if(argc!=2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
printf("argc count: %d\n", argc);*/
//1. server socket 생성
serv_sock=socket(PF_INET, SOCK_STREAM, 0);
//2. server socket 주소 정보 초기화
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family=AF_INET;
serv_adr.sin_addr.s_addr=htonl(SERVER_IP);
serv_adr.sin_port=htons(atoi(SERVER_PORT));
//3. server socket 주소 정보 binding
if(bind(serv_sock, (struct sockaddr*) &serv_adr, sizeof(serv_adr))==-1)
error_handling("bind() error");
//4. client 접속 대기
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
printf("server socket listening...\n");
FD_ZERO(&reads);
FD_SET(serv_sock, &reads);
fd_max=serv_sock;
while(1)
{
cpy_reads=reads;
timeout.tv_sec=5;
timeout.tv_usec=5000; //위의 timeout.tv_sec 또는 timeout.tv_usec 하나만 써도 됨
if((fd_num=select(fd_max+1, &cpy_reads, 0, 0, &timeout))==-1) //오류 발생시(-1일때)
break;
if(fd_num==0) // timeout 발생
continue;
for(i=0; i<fd_max+1; i++) // 폴링방식으로 계속 체크. fd_max'+1' 이유: fd_max가 서버라서 서버갯수
{
if(FD_ISSET(i, &cpy_reads))
{
if(i==serv_sock) // connection request!
{
adr_sz=sizeof(clnt_adr);
clnt_sock=
accept(serv_sock, (struct sockaddr*)&clnt_adr, &adr_sz);
FD_SET(clnt_sock, &reads);
if(fd_max<clnt_sock)
fd_max=clnt_sock;
printf("connected client: %d \n", clnt_sock);
read(clnt_sock, buf, BUF_SIZE);
buf[strlen(buf) - 1] = '\0';
printf("------------------------------\n");
printf("ID: %s entered\n", buf);
printf("------------------------------\n");
memset(buf, 0, BUF_SIZE);
}
else // read message!
{
str_len=read(i, buf, BUF_SIZE);
printf("str_len : %d\n", str_len);
if(str_len==0) // close request!
{
FD_CLR(i, &reads);
close(i);
printf("closed client: %d \n", i);
}
else
{
write(i, buf, str_len); // echo!
printf("ID: %s\n", buf);
memset(buf, 0, BUF_SIZE);
}
}
}
}
}
close(serv_sock);
return 0;
}
void error_handling(char *buf)
{
fputs(buf, stderr);
fputc('\n', stderr);
exit(1);
}
echo_clint.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 100
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT "9987"
void error_handling(char *message);
int main(/*int argc, char *argv[]*/)
{
int sock;
char message[BUF_SIZE];
int str_len;
struct sockaddr_in serv_adr;
/*
if(argc!=3) {
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}*/
sock=socket(PF_INET, SOCK_STREAM, 0);
if(sock==-1)
error_handling("socket() error");
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family=AF_INET;
serv_adr.sin_addr.s_addr=inet_addr(SERVER_IP);
serv_adr.sin_port=htons(atoi(SERVER_PORT));
if(connect(sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr))==-1)
error_handling("connect() error!");
else
puts("Connected...........");
printf("enter ID: ");
char id[20] = { 0, };
fgets(id, 20, stdin);
write(sock, id, strlen(id));
id[strlen(id) - 1] = '\0';
printf("------------------------------\n");
printf("ID: %s entered\n", id);
printf("------------------------------\n");
while(1)
{
fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if(!strcmp(message,"q\n") || !strcmp(message,"Q\n"))
break;
write(sock, message, strlen(message));
str_len=read(sock, message, BUF_SIZE-1);
message[str_len]=0;
printf("Message from server: %s", message);
}
close(sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
개행문자 없애기
1. id[strlen(id) - 1] = '\0';을 server와 clinet 둘 다 추가
2. client에서 write(sock, id, strlen(id)-1); : 문자열에서 -1한 문자열을 서버로 전송해주기
3. client는 그대로 두고 server에 아래처럼 추가.
if(fd_max<clnt_sock)
fd_max=clnt_sock;
printf("connected client: %d \n", clnt_sock);
int readByte = read(clnt_sock, buf, BUF_SIZE);
char tmpBuf[BUF_SIZE] = {0,};
strncpy(tmpBuf, buf, readByte - 1);
printf("------------------------------\n");
printf("ID: %s entered\n", tmpBuf);
printf("------------------------------\n");
memset(buf, 0, BUF_SIZE);
memset(tmpBuf, 0, BUF_SIZE);
server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/select.h>
#define BUF_SIZE 100
#define SERVER_IP INADDR_ANY
#define SERVER_PORT "9987"
void error_handling(char *buf);
int main(/*int argc, char *argv[]*/)
{
int serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
struct timeval timeout;
fd_set reads, cpy_reads;
socklen_t adr_sz;
int fd_max, str_len, fd_num, i;
char buf[BUF_SIZE];
/*if(argc!=2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
printf("argc count: %d\n", argc);*/
//1. server socket 생성
serv_sock=socket(PF_INET, SOCK_STREAM, 0);
//2. server socket 주소 정보 초기화
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family=AF_INET;
serv_adr.sin_addr.s_addr=htonl(SERVER_IP);
serv_adr.sin_port=htons(atoi(SERVER_PORT));
//3. server socket 주소 정보 binding
if(bind(serv_sock, (struct sockaddr*) &serv_adr, sizeof(serv_adr))==-1)
error_handling("bind() error");
//4. client 접속 대기
if(listen(serv_sock, 5)==-1)
error_handling("listen() error");
printf("server socket listening...\n");
FD_ZERO(&reads);
FD_SET(serv_sock, &reads);
fd_max=serv_sock;
while(1)
{
cpy_reads=reads;
timeout.tv_sec=5;
timeout.tv_usec=5000; //위의 timeout.tv_sec 또는 timeout.tv_usec 하나만 써도 됨
if((fd_num=select(fd_max+1, &cpy_reads, 0, 0, &timeout))==-1) //오류 발생시(-1일때)
break;
if(fd_num==0) // timeout 발생
continue;
for(i=0; i<fd_max+1; i++) // 폴링방식으로 계속 체크. fd_max'+1' 이유: fd_max가 서버라서 서버갯수
{
if(FD_ISSET(i, &cpy_reads))
{
if(i==serv_sock) // connection request!
{
adr_sz=sizeof(clnt_adr);
clnt_sock=
accept(serv_sock, (struct sockaddr*)&clnt_adr, &adr_sz);
FD_SET(clnt_sock, &reads);
if(fd_max<clnt_sock)
fd_max=clnt_sock;
printf("connected client: %d \n", clnt_sock);
int readByte = read(clnt_sock, buf, BUF_SIZE);
char tmpBuf[BUF_SIZE] = {0,};
strncpy(tmpBuf, buf, readByte - 1);
printf("------------------------------\n");
printf("ID: %s entered\n", tmpBuf);
printf("------------------------------\n");
memset(buf, 0, BUF_SIZE);
memset(tmpBuf, 0, BUF_SIZE);
}
else // read message!
{
str_len=read(i, buf, BUF_SIZE);
printf("str_len : %d\n", str_len);
if(str_len==0) // close request!
{
FD_CLR(i, &reads);
close(i);
printf("closed client: %d \n", i);
}
else
{
write(i, buf, str_len); // echo!
printf("ID: %s\n", buf);
memset(buf, 0, BUF_SIZE);
}
}
}
}
}
close(serv_sock);
return 0;
}
void error_handling(char *buf)
{
fputs(buf, stderr);
fputc('\n', stderr);
exit(1);
}
'임베디드 > C언어 소켓 통신' 카테고리의 다른 글
makefile (0) | 2020.10.08 |
---|---|
구조체 사용 (0) | 2020.10.08 |
echo_client.c echo_selectserv.c (0) | 2020.09.29 |
echo_selectserv echo_client (0) | 2020.09.29 |
chpt12. echo_select (0) | 2020.09.25 |