您的位置:首页 > 理论基础 > 计算机网络

socket网络编程实现文件从服务器端到客户端的传输

2012-09-23 13:53 891 查看

代码片段(1)

[代码][C/C++]代码

viewsource

print?

001
server.c
002
003
#include<sys/socket.h>

004
#include<netinet/in.h>

005
#include<netinet/ip.h>

006
#include<unistd.h>
007
#include<arpa/inet.h>

008
#include<sys/types.h>

009
#include<sys/wait.h>
010
#include<sys/stat.h>
011
#include<fcntl.h>
012
#include<signal.h>
013
#include<stdio.h>
014
#include<stdlib.h>
015
#include<string.h>
016
#include<errno.h>
017
void
sigchid(
int
signum)
018
{
019
waitpid(-1,NULL,0);
020
}
021
022
void
exit_err(
const
char
*s)
023
{
024
perror
(s);
025
exit
(1);
026
}
027
028
int
main(
void
)

029
{
030
signal
(SIGCHLD,sigchid);
031
032
int

val,
true

=1;
033
char

buf1[128];
034
int

open_fd,confd,read_count,write_count;
035
struct

sockaddr_insockaddr;
036
socklen_taddrlen;
037
sockaddr.sin_family=AF_INET;
038
sockaddr.sin_port=htons(10000);
039
sockaddr.sin_addr.s_addr=htonl(INADDR_ANY);
040
041
int

fd=socket(AF_INET,SOCK_STREAM,0);
042
if

(fd==-1)
043
exit_err(
"socket"
);
044
045
int

bin=
046
bind(fd,(
struct

sockaddr*)&sockaddr,
sizeof
(sockaddr));
047
if

(bin==-1)
048
exit_err(
"bind"
);
049
050
if

(listen(fd,5)==-1)
051
exit_err(
"listen"
);
052
053
054
while

(1){
055
056
addrlen=
sizeof
(sockaddr);
057
058
confd=accept(fd,(
struct

sockaddr*)&sockaddr,&addrlen);
059
if

(-1==confd){
060
if

(
errno

==EINTR){
061
continue
;
062
}
063
exit_err(
"accept"
);
064
}

065
pid_tpid=fork();
066
if
(pid==-1)
067
exit_err(
"fork"
);
068
if
(pid==0){
/*child*/
069
070
while
(1){
071
open_fd=open(
"./txt"
,O_RDONLY);
072
while

((read_count=read(open_fd,buf1,
sizeof
(buf1)))>0)
073
074
while

(read_count){
075
write_count=write(confd,buf1,read_count);
076
if

(write_count<0){
077
exit_err(
"write"
);
078
}
079
read_count-=write_count;
080
}
081
}

082
close(open_fd);
083
close(confd);
084
exit
(0);
085
}

086
else

{
/*parent*/
087
close(confd);
088
}

089
}

090
close(fd);
091
092
093
client.c
094
095
#include<sys/socket.h>

096
#include<netinet/in.h>

097
#include<netinet/ip.h>

098
#include<unistd.h>
099
#include<arpa/inet.h>

100
#include<sys/time.h>
101
#include<sys/types.h>

102
#include<sys/stat.h>
103
#include<fcntl.h>
104
#include<stdio.h>
105
#include<stdlib.h>
106
#include<string.h>
107
#include<errno.h>
108
109
110
void
exit_err(
const
char
*s)
111
{
112
perror
(s);
113
exit
(1);
114
}
115
116
int
main(
void
)

117
{
118
int

i,retval,open_fd,w;
119
char

buf[128],nebuf[128];
120
short

port=10000;
121
char

*ipaddr=
"127.0.0.1"
;
122
struct

sockaddr_insockaddr;
123
sockaddr.sin_family=AF_INET;
124
sockaddr.sin_port=htons(port);
125
sockaddr.sin_addr.s_addr=inet_addr(ipaddr);
126
socklen_taddrlen;
127
128
int

fd=socket(AF_INET,SOCK_STREAM,0);
129
if

(fd==-1)
130
exit_err(
"socket"
);
131
132
addrlen=
sizeof
(sockaddr);
133
int

sockcon=connect(fd,(
struct

sockaddr*)&sockaddr,
134
sizeof
(sockaddr));
135
136
if

(sockcon==-1)
137
exit_err(
"connect"
);
138
139
while
(1){
140
open_fd=open(
"./data"
,O_RDWR);
141
while

((retval=read(fd,nebuf,
sizeof
(nebuf)))>0){
142
while

(retval>0){
143
w=write(open_fd,nebuf,retval);
144
if

(-1==retval)
145
exit_err(
"read"
);
146
retval-=w;
147
}
148
}

149
}

150
close(open_fd);
151
152
153
close(fd);
154
}
155
156
157
158
return

0;
159
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐