Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
System Programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
서현 박
System Programming
Commits
6a3f957f
Commit
6a3f957f
authored
1 year ago
by
서현 박
Browse files
Options
Downloads
Patches
Plain Diff
sh
parent
30c04423
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
serverTest.c
+206
-0
206 additions, 0 deletions
serverTest.c
with
206 additions
and
0 deletions
serverTest.c
0 → 100644
+
206
−
0
View file @
6a3f957f
#include
<arpa/inet.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<sys/socket.h>
#include
<pthread.h>
#include
<wiringPi.h>
#include
<unistd.h>
#include
<softTone.h>
#include
<stdbool.h>
#define BUTTON 28
#define BUZZER 29
#define START 10
#define END 30
int
count_clnt
=
0
;
int
clnt_socks
[
3
];
void
error_handling
(
char
*
message
){
fputs
(
message
,
stderr
);
fputc
(
'\n'
,
stderr
);
exit
(
1
);
}
void
*
sendMessage
(
void
*
arg
){
int
clnt_sock
=
*
((
int
*
)
arg
);
char
message
[
1024
];
int
repeat
=
0
;
bool
checkButton
=
true
;
int
scale
[
23
]
=
{
659
,
659
,
0
,
659
,
0
,
523
,
659
,
0
,
784
,
0
,
0
,
0
,
392
,
0
,
0
,
0
,
523
,
0
,
0
,
392
,
0
,
0
,
330
}
;
//client 수 카운드
count_clnt
++
;
//모든 client 접속 완료시 실행
while
(
1
){
if
(
count_clnt
==
3
){
break
;
}
}
delay
(
5000
);
//wiringPi 셋업
wiringPiSetup
();
//모드 설정
pinMode
(
BUTTON
,
INPUT
);
softToneCreate
(
BUZZER
);
//buffer 초기화
memset
(
message
,
0
,
sizeof
(
message
));
while
(
1
){
//버튼 눌렸을 시
if
(
digitalRead
(
BUTTON
)
==
HIGH
){
//수면모드 강제시작
if
(
repeat
<
START
){
strcpy
(
message
,
"s"
);
write
(
clnt_sock
,
message
,
sizeof
(
message
));
repeat
=
START
+
1
;
}
//수면모드 강제 종료
else
if
(
START
<
repeat
&&
repeat
<
END
){
strcpy
(
message
,
"e"
);
write
(
clnt_sock
,
message
,
sizeof
(
message
));
if
(
read
(
clnt_socks
[
1
],
message
,
sizeof
(
message
))
==
-
1
)
perror
(
"read error in getMessage
\n
"
);
else
write
(
clnt_socks
[
0
],
message
,
sizeof
(
message
));
repeat
=
END
+
1
;
}
}
//수면 준비
if
(
repeat
==
0
){
strcpy
(
message
,
"rs1"
);
write
(
clnt_sock
,
message
,
sizeof
(
message
));
}
else
if
(
repeat
==
5
){
strcpy
(
message
,
"rs2"
);
write
(
clnt_sock
,
message
,
sizeof
(
message
));
}
//수면모드 실행
else
if
(
repeat
==
START
){
strcpy
(
message
,
"s"
);
write
(
clnt_sock
,
message
,
sizeof
(
message
));
}
//기상 준비
else
if
(
repeat
==
25
){
strcpy
(
message
,
"re"
);
write
(
clnt_sock
,
message
,
sizeof
(
message
));
}
//기상
else
if
(
repeat
==
END
){
strcpy
(
message
,
"e"
);
int
a
=
write
(
clnt_sock
,
message
,
sizeof
(
message
));
int
i
=
0
;
//기상알람 + Button이 눌리면 꺼짐
do
{
// printf ("%3d\n", i) ;
softToneWrite
(
BUZZER
,
scale
[
i
])
;
i
++
;
delay
(
200
)
;
//노래 반복위해 초기화
if
(
i
==
23
)
i
=
0
;
if
(
digitalRead
(
BUTTON
)
==
HIGH
)
checkButton
=
false
;
}
while
(
checkButton
);
softToneWrite
(
BUZZER
,
0
)
;
//모션센서 값 받아옴
if
(
read
(
clnt_socks
[
1
],
message
,
sizeof
(
message
))
==
-
1
)
perror
(
"read error in getMessage
\n
"
);
else
{
//LCD한테 값 넘겨줌
delay
(
3000
);
write
(
clnt_socks
[
0
],
message
,
sizeof
(
message
));
}
pthread_exit
((
void
*
)
0
);
}
repeat
++
;
delay
(
1000
);
}
}
int
main
(
int
argc
,
char
*
argv
[]){
int
serv_sock
,
clnt_sock
=
-
1
;
struct
sockaddr_in
serv_addr
,
clnt_addr
;
socklen_t
clnt_addr_size
;
pthread_t
p_thread
[
3
];
if
(
argc
!=
2
){
printf
(
"Usage : %s <port>
\n
"
,
argv
[
0
]);
}
//socket 설정
serv_sock
=
socket
(
PF_INET
,
SOCK_STREAM
,
0
);
if
(
serv_sock
==
-
1
)
error_handling
(
"socket() error"
);
//소켓 주소에 바인딩
memset
(
&
serv_addr
,
0
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
serv_addr
.
sin_port
=
htons
(
atoi
(
argv
[
1
]));
if
(
bind
(
serv_sock
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
==
-
1
)
error_handling
(
"bind() error"
);
//수신대기
if
(
listen
(
serv_sock
,
5
)
==
-
1
)
error_handling
(
"listen() error"
);
if
(
clnt_sock
<
0
)
clnt_addr_size
=
sizeof
(
clnt_addr
);
int
i
=
0
;
//다른 RP에 기상시간, 취침시간 전송
while
(
1
){
clnt_sock
=
accept
(
serv_sock
,
(
struct
sockaddr
*
)
&
clnt_addr
,
&
clnt_addr_size
);
//client 연결 수락
if
(
clnt_sock
==
-
1
){
error_handling
(
"accept() error"
);
exit
(
0
);
}
clnt_socks
[
i
]
=
clnt_sock
;
printf
(
"Connection established
\n
"
);
pthread_create
(
&
p_thread
[
i
],
NULL
,
sendMessage
,
&
clnt_socks
[
i
]);
i
++
;
}
pthread_join
(
p_thread
[
0
],
NULL
);
pthread_join
(
p_thread
[
1
],
NULL
);
pthread_join
(
p_thread
[
2
],
NULL
);
close
(
clnt_sock
);
close
(
serv_sock
);
return
(
0
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment