1 头文件部分
#ifndef __HEAD_H__
2 #define __HEAD_H__
3
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <math.h>
12 #include <time.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #include <pthread.h>
16 #include <signal.h>
17 #include <sys/wait.h>
18 #include <sys/ipc.h>
19 #include <sys/msg.h>
20 #include <sys/shm.h>
21 #include <sys/sem.h>
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #define ERR_MSG(msg) do{\
26 fprintf(stderr,"__%d__",__LINE__);\
27 perror(msg);\
28 }while(0)
29 #define LINE do{\
30 printf("__%d__\n",__LINE__);\
31 }while(0)
32 int do_download(int cfd,struct sockaddr_in sin);
33 int do_upload(int cfd,struct sockaddr_in sin);
34 #endif
35
36 函数部分
37 #include "head.h"
38 int do_download(int cfd,struct sockaddr_in sin)
39 {
40 char buf[516] = "";
41 short *ptr1 = (short*)buf;
42 *ptr1 = htons(1);
43 char arr[20] = "";
44 printf("请输入文件名");
45 scanf("%s",arr);
46 getchar();
47 char *ptr2 = buf+2;
48 strcpy(ptr2,arr);
49 char* ptr3 = ptr2+strlen(ptr2)+1;
50 strcpy(ptr3,"octet");
51 int size = 2+strlen(ptr2)+1+strlen(ptr3)+1;
52 if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
53 {
54 ERR_MSG("sendto");
55 return -1;
56 }
57 printf("发送下载请求成功");
58 LINE;
59 struct sockaddr_in cin;
60 socklen_t addrlen = sizeof(cin);
61 ssize_t res = 0;
62 int fdr = open(arr,O_WRONLY|O_CREAT|O_APPEND,0664);
63 if(fdr < 0)
64 {
65 ERR_MSG("fdr");
66 return -1;
67 }
68 bzero(buf,sizeof(buf));
69 int i = 1;
70 char adf[4] = "";
71 short *pt1 = (short*)adf;
72 short *pt2 = pt1+1;
73 while(1)
74 {
75 bzero(buf,sizeof(buf));
76 res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
77 if(res < 0);
78 {
79 ERR_MSG("recvfrom");
80 }
81
82 *pt1 = htons(4);
83 *pt2 = htons(i);
84 i++;
85 if(sendto(cfd,adf,4,0,(struct sockaddr*)&cin,sizeof(cin)) < 0)
86 {
87 ERR_MSG("sendto");
88 return -1;
89 }
90 if(res-4 == 512)
91 {
92 write(fdr,&buf[4],512);
93 }
94 else
95 {
96 write(fdr,&buf[4],res-4);
97 break;
98 }
99 }
100 close(fdr);
101
102
103 return 0;
104 }
105 int do_upload(int cfd,struct sockaddr_in sin)
106 {
107 char buf[516] = "";
108 short* p1 = (short*)buf;
109 *p1 = htons(2);
110 char arr[20] = "";
111 printf("请输入文件名");
112 scanf("%s",arr);
113 getchar();
114 char *p2 = buf+2;
115 strcpy(p2,arr);
116 char* p3 = p2+strlen(p2)+1;
117 strcpy(p3,"octet");
118 int size = 2+strlen(p2)+1+strlen(p3)+1;
119 if(sendto(cfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
120 {
121 ERR_MSG("sendto");
122 return -1;
123 }
124 printf("发送上传请求成功");
125 int fdr = open(arr,O_RDONLY);
126 if(fdr < 0)
127 {
128 ERR_MSG("open");
129 return -1;
130 }
131 ssize_t res1 = 0;
132 int sum = 0;
133 off_t offset = lseek(fdr,0,SEEK_END);
134 lseek(fdr,0,SEEK_SET);
135 sum = (int)offset/512;
136 int flag = 0;
137 bzero(buf,sizeof(buf));
138 short *ptr1 = (short*)buf;
139 short* ptr2 = ptr1+1;
140 struct sockaddr_in cin;
141 socklen_t addrlen = sizeof(cin);
142 int num = 1;
143 ssize_t res = 0;
144 char adf[4] = "";
145 short* pd1 = (short*)adf;
146 short* pd2 = pd1+1;
147 while(1)
148 {
149 bzero(buf,sizeof(buf));
150 bzero(adf,sizeof(adf));
151 *ptr1 = htons(3);
152 *ptr2 = htons(num);
153 *pd1 = htons(4);
154 *pd2 = htons(num);
155 printf("%d %d",buf[1],buf[2]);
156 LINE;
157 res = recvfrom(cfd,adf,4,0,(struct sockaddr*)&cin,&addrlen);
158 printf("%d %d %d\n",adf[0],adf[1],(int)res);
159 if(res < 0)
160 {
161 ERR_MSG("recvfrom");
162 return -1;
163 }
164 if(flag < sum)
165 {
166 res1 = read(fdr,&buf[4],512);
167 printf("%d\n",(int)res1);
168 LINE;
169 if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen) < 0)
170 {
171 ERR_MSG("sendto");
172 return -1;
173 }
174 }
175 else
176 {
177 res1 = read(fdr,&buf[4],offset-sum*512);
178 LINE;
179 if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen) < 0)
180 {
181 ERR_MSG("sendto");
182 return -1;
183 }
184 break;
185 }
186 flag++;
187 num++;
188 LINE;
189 }
190
191
192
193
194 }
主函数部分
195 #include "head.h"
196 #define PORT 69
197 #define IP "192.168.0.100"
198 int main(int argc, const char *argv[])
199 {
200 int cfd = socket(AF_INET,SOCK_DGRAM,0);
201 if(cfd < 0)
202 {
203 ERR_MSG("socket");
204 return -1;
205 }
206 printf("socket create success cfd=%d",cfd);
207 LINE;
208 struct sockaddr_in sin;
209 sin.sin_family = AF_INET;
210 sin.sin_port = htons(PORT);
211 sin.sin_addr.s_addr = inet_addr(IP);
212 char choose = 0;
213 while(1)
214 {
215 printf("------------------------\n");
216 printf("---------1. 下载--------\n");
217 printf("---------2. 上传--------\n");
218 printf("---------3. 退出--------\n");
219 printf("------------------------\n");
220 printf("请输入>>>>");
221 choose = getchar();
222 while(getchar()!='\n');
223 switch(choose)
224 {
225 case '1':
226 do_download(cfd,sin);
227 break;
228 case '2':
229 do_upload(cfd,sin);
230 break;
231 case '3':
232 goto FOUNCE;
233 default:
234 printf("输入错误,请重新输入\n");
235 break;
236
237 }
238 }
239 FOUNCE:
240 close(cfd);
241 return 0;
242 }
网络编程第3天作业
最新推荐文章于 2024-09-09 15:42:52 发布