Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
file-test
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
김 민서
file-test
Commits
567dad38
Commit
567dad38
authored
7 years ago
by
minseo
Browse files
Options
Downloads
Patches
Plain Diff
add sender.c
parent
09bb23e3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
FileSocket/sender.c
+143
-0
143 additions, 0 deletions
FileSocket/sender.c
FileSocket/speech
+207
-0
207 additions, 0 deletions
FileSocket/speech
with
350 additions
and
0 deletions
FileSocket/sender.c
0 → 100644
+
143
−
0
View file @
567dad38
/*
* sender.c
*
* send a file to client from server.
*
* Created on: 2018. 5. 12.
* Author: minseo
*/
#include
<stdlib.h>
#include
<stdio.h>
#include
<string.h>
#include
<fcntl.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<arpa/inet.h>
#include
<unistd.h>
#define BUFSIZE 1024
int
socket_open
(
int
server_fd
);
struct
sockaddr_in
socket_bind
(
int
server_fd
,
struct
sockaddr_in
server_addr
);
void
socket_listen
(
int
server_fd
);
int
socket_accept
(
int
server_fd
,
int
client_fd
,
struct
sockaddr_in
client_addr
,
int
client_len
);
void
file_send
(
int
client_fd
);
void
main
()
{
int
server_fd
;
int
client_fd
;
int
client_len
;
struct
sockaddr_in
server_addr
;
struct
sockaddr_in
client_addr
;
server_fd
=
socket_open
(
server_fd
);
server_addr
=
socket_bind
(
server_fd
,
server_addr
);
socket_listen
(
server_fd
);
while
(
1
)
{
client_fd
=
socket_accept
(
server_fd
,
client_fd
,
client_addr
,
client_len
);
file_send
(
client_fd
);
close
(
client_fd
);
}
}
// server socket 생성
int
socket_open
(
int
server_fd
)
{
if
((
server_fd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
-
1
)
{
perror
(
"socket open error."
);
exit
(
1
);
}
return
server_fd
;
}
// server socket에 주소, 포트 정보 연결
struct
sockaddr_in
socket_bind
(
int
server_fd
,
struct
sockaddr_in
server_addr
)
{
int
bValid
=
1
;
memset
(
&
server_addr
,
0
,
sizeof
(
server_addr
));
server_addr
.
sin_family
=
AF_INET
;
server_addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
server_addr
.
sin_port
=
htons
(
50000
);
setsockopt
(
server_fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
(
const
char
*
)
&
bValid
,
sizeof
(
bValid
));
if
(
bind
(
server_fd
,
(
struct
sockaddr
*
)
&
server_addr
,
sizeof
(
server_addr
))
==
-
1
)
{
perror
(
"socket bind error."
);
exit
(
1
);
}
return
server_addr
;
}
// server로 들어오는 client를 기다림
void
socket_listen
(
int
server_fd
)
{
if
(
listen
(
server_fd
,
5
)
==
-
1
)
{
perror
(
"socket listen error."
);
exit
(
1
);
}
}
// server로 들어온 client의 요청을 받아 들임
int
socket_accept
(
int
server_fd
,
int
client_fd
,
struct
sockaddr_in
client_addr
,
int
client_len
)
{
client_len
=
sizeof
(
client_addr
);
if
((
client_fd
=
accept
(
server_fd
,
(
struct
sockaddr
*
)
&
client_addr
,
&
client_len
))
==
-
1
)
{
perror
(
"socket accept error."
);
exit
(
1
);
}
return
client_fd
;
}
// server에서 client로 파일 전송
void
file_send
(
int
client_fd
)
{
FILE
*
send_fp
;
char
send_buf
[
BUFSIZE
]
=
{
'\0'
,
};
char
receive_buf
[
BUFSIZE
]
=
{
'\0'
,
};
int
send_size
=
0
;
int
receive_size
=
0
;
int
file_size
=
0
;
int
total_send_size
=
0
;
// 1. 파일을 읽기 모드로 염
if
((
send_fp
=
fopen
(
"speech"
,
"r"
))
==
NULL
)
{
perror
(
"send file open error."
);
exit
(
1
);
}
// 2. 파일의 전체 사이즈를 구함
fseek
(
send_fp
,
0
,
SEEK_END
);
file_size
=
ftell
(
send_fp
);
rewind
(
send_fp
);
// printf("%d \n", file_size);
// 3. 파일의 전체 사이즈를 client로 보냄
if
(
send
(
client_fd
,
&
file_size
,
sizeof
(
file_size
),
0
)
!=
sizeof
(
file_size
))
{
perror
(
"file send error."
);
exit
(
1
);
}
// 파일에서 더이상 읽을 데이터가 없을 때 까지 반복
while
(
1
)
{
memset
(
send_buf
,
0
,
BUFSIZE
);
// 4. 파일의 char 크기만큼 데이터를 읽어서 버퍼에 저장
send_size
=
fread
(
send_buf
,
sizeof
(
char
),
BUFSIZE
,
send_fp
);
// 파일에서 더이상 읽을 데이터가 없으면 while문 종료
if
(
send_size
==
0
)
{
break
;
}
// printf(" read size : %d, string size : %d\n", send_size, strlen(send_buf));
// 5. 버퍼에 저장된 데이터를 client로 전송
if
(
send
(
client_fd
,
send_buf
,
send_size
,
0
)
!=
send_size
)
{
perror
(
"file send error."
);
exit
(
1
);
}
total_send_size
+=
send_size
;
printf
(
"send_size:%d
\n
"
,
send_size
);
}
fclose
(
send_fp
);
}
This diff is collapsed.
Click to expand it.
FileSocket/speech
0 → 100644
+
207
−
0
View file @
567dad38
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