版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、要求:用socket 编程接口编写两个程序,分别为客户程序(client.c)和服务器程序(server.c),该程序应能实现下述命令功能:get:取远方的一个文件put:传给远方一个文件pwd:显示远主当前目录dir:列出远方当前目录cd :改变远方当前目录? :显示你提供的命令quit :退出返回注意事项:这此命令的具体工作方式(指给出结果的形式)可以参照FTP 的相应命令,有余力的同学可以多实现几个命令。使用Socket进行Client/Server程序设计的一般连接过程是这样的:Server端Listen(监听)某个端口是否有连接请求,Client端向Server端发出Connect(
2、连接)请求,Server端向Client端发回Accept(接受)消息。一个连接就建立起来了。Server端和Client端都可以通过Send,Write等方法与对方通信。 对于一个功能齐全的Socket,都要包含以下基本结构,其工作过程包含以下四个基本的步骤: (1) 创建Socket; (2) 打开连接到Socket的输入/出流; (3) 按照一定的协议对Socket进行读/写操作; (4) 关闭Socket. 下面是源代码:/*client.c*/#include <winsock2.h>#include <stdio.h>#include <stdlib.
3、h>#pragma comment(lib,"ws2_32.lib")#define DEFAULT_PORT 2302#define DEFAULT_BUFFER 2048#define DEFAULT_MESSAGE "This is a test of the emergency broadcasting system"char szServe
4、rip128, / Server to connect to szMessage1024; / Message to send to severint iPort = DEFAULT_PORT; / Port on server to connect to/DWOR
5、D dwCount = DEFAULT_COUNT; / Number of times to send messageBOOL bSendOnly = FALSE; / Send data only; don't receiveint dirfun();int getfun();int putfun();int pwdfun();int cdfun();int mdfun();int delfun();int usafun(); void usa
6、ge() printf("usage: client -p:x -s:IP -n:x -onn"); printf(" -p:x Remote port to send ton"); printf(" -s:IP &
7、#160; Server's IP address or hostnamen"); printf(" -n:x Number of times to send messagen"); printf(" -o
8、; Send messages only; don't receiven"); ExitProcess(1);void ValidateArgs(int argc, char *argv) int i; for(i = 1; i < argc; i+)
9、 if (argvi0 = '-') | (argvi0 = '/') switch (tolower(argvi1) &
10、#160; case 'p': / Remote port if (strle
11、n(argvi) > 3) iPort = atoi(&argvi3); brea
12、k; case 's': / Server if (strlen(argvi) > 3)
13、; strcpy(szServerip, &argvi3); break; &
14、#160; case 'n': / Number of times to send message if (strlen(argvi) &g
15、t; 3) /dwCount = atol(&argvi3); break;
16、 case 'o': / Only send message; don't receive bSendOnly
17、 = TRUE; break; default:
18、 usage(); break;
19、 int main(int argc, char *argv) WSADATA wsd; SOCKET sClient; char szBufferDEFAULT_BUFFER; int
20、60; ret; /unsigned int i; /int j; struct sockaddr_in server; struct hostent *host = NULL;&
21、#160; char choice5,choice240; / Parse the command line and load Winsock / argv1="-s:" strcpy(szServerip, &argv13); /ValidateArgs(argc, argv);
22、0; if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) printf("Failed to load Winsock library!n"); return 1; /strcpy(szMessage, DEFAULT_MESSAGE);
23、; / / Create the socket, and attempt to connect to the server / sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sClient = INVALID_SOCKET) printf("s
24、ocket() failed: %dn", WSAGetLastError(); return 1; server.sin_family = AF_INET; server.sin_port = htons(iPort); printf("server.sin_port=%un",server.sin_port);
25、 server.sin_addr.s_addr = inet_addr(szServerip); / / If the supplied server address wasn't in the form / "aaa.bbb.ccc.ddd" it's a hostname, so try to resolve it / if (server.sin_add
26、r.s_addr = INADDR_NONE) host = gethostbyname(szServerip); if (host = NULL) printf("
27、Unable to resolve server: %sn", szServerip); return 1; CopyMemory(&server.sin_addr, host->h_addr_list0,
28、0; host->h_length); if (connect(sClient, (struct sockaddr *)&server, sizeof(server) = SOCKET_ERROR) printf("connect()
29、 failed: %dn", WSAGetLastError(); return 1; /显示接通信息 / / /for(j=0;j<2;j+) /
30、 ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0); if (ret = 0) / Graceful close
31、60; return 0; else if (ret = SOCKET_ERROR) printf(&qu
32、ot;recv() failed: %dn", WSAGetLastError(); return 0; szBufferret = '
33、0' printf("%sn",szBuffer); if(ret<15)
34、; ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0); if (ret = 0)
35、160; / Graceful close return 0; else if (ret = SOCKET_
36、ERROR) /printf("recv() failed: %dn", WSAGetLastEr
37、ror(); return 0; &
38、#160; szBufferret = '0' printf("%sn",szBuffer); &
39、#160; /printf("DEFAULT_BUFFER=%dn",DEFAULT_BUFFER); while(1)
40、0; puts("-"); printf("ftp> "); scanf("%s", choice);
41、0; if(strncmp(choice,"dir",3)=0|strncmp(choice,"DIR",2)=0) dirfun(sClient);
42、 continue; else if(strncmp(choice,"pwd",3)=0|strncmp(choice,"PWD",3)=0)
43、60; pwdfun(sClient); continue;
44、; else if(strncmp(choice,"?",1)=0)
45、160; usafun(sClient); continue;
46、0; else if(strncmp(choice,"quit",4)=0|strncmp(choice,"QUIT",2)=0)
47、0; break; scanf("%s", choice2);
48、 if(strncmp(choice,"get",3)=0|strncmp(choice,"GET",3)=0) getfun(sClient,choice2);
49、 continue; else if(strncmp(choice,&
50、quot;put",3)=0|strncmp(choice,"PUT",3)=0) putfun(sClient,choice2);
51、160; continue; else if(strncmp(choice,"cd",2)=0|strncmp
52、(choice,"CD",2)=0) cdfun(sClient,choice2); &
53、#160; continue; else if(strncmp(choice,"md",2)=0|strncmp(choice,"MD",2)=0) &
54、#160; mdfun(sClient,choice2);
55、 continue; else if(strncmp(choice,"del",3)=0|strncmp(choice,"DEL",3)=0)
56、0; delfun(sClient,choice2); continue;
57、0; /else puts("输入错误,请重新输入"); fflush(stdin);
58、 fflush(stdin); printf("n"); printf("n"); closesocket(sClient); WSACleanup();
59、60; return 0;int dirfun(SOCKET sClient) int ret; char *MSG="dir$"char szBuffer80; strcpy(szMessage, MSG);
60、; ret = send(sClient, szMessage, strlen(szMessage), 0); if (ret = 0) return 1; else if (ret = SOCKET_ERROR) &
61、#160; printf("send() failed: %dn", WSAGetLastError(); return 1;
62、60; /printf("Send %d bytesn", ret); /printf("bSendOnly=%dn",bSendOnly); while(!bSendOnly)
63、0; /读取流并显示 /ret = recv(sClient, szBuffer, 80, 0); /printf("%s"
64、;,szBuffer); ret = recv(sClient, szBuffer, 80, 0); if (ret = 0) / Graceful close
65、0; return 1; else if (ret = SOCKET_ERROR)
66、 printf("recv() failed: %dn", WSAGetLastError(); return 1; sz
67、Bufferret = '0'
68、0; if(strncmp(szBuffer,"226 Close",strlen("226 Close")=0) break;
69、; printf("%s",szBuffer); if(strncmp(szBuffer,"500 Syntax error",strle
70、n("500 Syntax error")=0) break;
71、 return 0;int getfun(SOCKET sClient,char filename40) int ret;
72、160; FILE *fpre; char szBuffer80; szMessage0='0' strcat(szMessage, "get$"); /
73、strcat(szMessage, ""); strcat(szMessage,filename); /printf("MSG4=%cn",szMessage4); /szM
74、essage0='g'szMessage1='e'szMessage2='t' / /szMessage4='m'szMessage5='e'szMessage6='.'szMessage7='t'
75、/ /szMessage8='x'szMessage9='t'szMessage10='0' ret = send(sClient, szMessage, strlen(szMessage)+1, 0); if (ret = 0)
76、; return 1; else if (ret = SOCKET_ERROR) printf("send() failed: %dn", WSAGetLastError();
77、60; return 1; printf("Send %d bytesn", ret); ret = recv(sClient, szBuffer, 80, 0);
78、60; /*if (ret = 0) / Graceful close break; else if (ret = SOCKET_ERROR)
79、 printf("recv() failed: %dn", WSAGetLastError(); break; */
80、0; szBufferret = '0' printf("%sn",szBuffer); if(strncmp( szBuffer,"125 Transfering.",strlen("125 Transfering.") )=0)
81、160; if( (fpre=fopen(filename,"w") = NULL )
82、; printf("open errer"); return 1;
83、0; printf("bSendOnly=%dn",bSendOnly); while(!bSendOnly)
84、 /读取流并显示 ret = recv(sClient, szBuffer, 80, 0);
85、160; if (ret = 0) / Graceful close return 1;
86、0; else if (ret = SOCKET_ERROR)
87、0; printf("recv() failed: %dn", WSAGetLastError(); return 1;
88、160; szBufferret = '0'
89、0; /printf("%s",szBuffer); if(strncmp
90、(szBuffer,"226 Transfer",strlen("226 Transfer")=0) &
91、#160; break; if(strncmp(szBuffer,"500 Syntax error",strlen("50
92、0 Syntax error")=0) break;
93、0; fprintf(fpre,"%s",szBuffer);
94、 printf("%sn",szBuffer); fclose(fpre); return 0;int pu
95、tfun(SOCKET sClient,char filename40) int ret;/int i; FILE *fpse;/char *filename; /char *MSG="get0me.txt"
96、 char szBuffer80,temp_buffer80; /sprintf(szMessage, "get0","te.txt0"); szMessage0='0'
97、 strcat(szMessage, "put$"); strcat(szMessage,filename); ret = send(sClient, szMessage, strlen(szMessage)+1, 0); if (ret = 0)
98、0; return 0; else if (ret = SOCKET_ERROR) printf("send() failed: %dn", WSAGetLastError();
99、160; return 1; /printf("Send %d bytesn", ret); /filename="me.txt"
100、0; printf("filename=%sn",filename); if( (fpse=fopen(filename,"r") = NULL ) printf("
101、;open errer"); return 1; else
102、 printf("The file %s found,ready to transfer.n",filename); /i=0; while (fgets(temp_buffer,80,fpse)
103、!=NULL) sprintf(szBuffer,"%s",temp_buffer);
104、; send(sClient, szBuffer, 80, 0); sprintf(szBuffer, "226 Transfer compl
105、eted. rn"); ret = send(sClient, szBuffer, strlen(szBuffer), 0); fclose(fpse); return 0;int pwdfun(SOCKET sClient) &
106、#160; int ret; char *MSG="pwd$" char szBuffer160; strcpy(szMessage, MSG); ret = send(sClient, szMessage, strlen(szMessage), 0); if (ret = 0)
107、 return 1; else if (ret = SOCKET_ERROR) printf("send() failed: %dn", WSAGetLastError();
108、0; return 1; printf("Send %d bytesn", ret); printf("bSendOnly=%dn",bSendOnly); while(!bSendOnly)
109、160; /读取流并显示 ret = recv(sClient, szBuffer, 160, 0); if (ret = 0) / Graceful close
110、60; return 1; else if (ret = SOCKET_ERROR) printf("recv() failed: %dn", WSAGetLastErr
111、or(); return 1; szBufferret = '0' printf("%sn",szBuffer);
112、160; if(strncmp(szBuffer,"226 Close",strlen("226 Close")=0) break;
113、160; if(strncmp(szBuffer,"500 Syntax error",strlen("500 Syntax error")=0) &
114、#160; break; return 0;int cdfun(SOCKET sClient,char pathname40) int ret; szMessage0='0'
115、0; strcat(szMessage, "cd$"); strcat(szMessage,pathname); ret = send(sClient, szMessage, strlen(szMessage)+1, 0); if (ret =
116、 0) return 1; else if (ret = SOCKET_ERROR) printf("send() failed: %dn", W
117、SAGetLastError(); return 1; printf("Send %d bytesn", ret); return 0;int mdfun(SOCKET sClie
118、nt,char pathname20) int ret;char szBuffer160; /char *MSG="md$" szMessage0='0' strcat(szMessage, "md$"); /strcat(szMessage, "
119、;"); strcat(szMessage,pathname); /strcpy(szMessage, MSG); ret = send(sClient, szMessage, strlen(szMessage)+1, 0); if (ret = 0) &
120、#160; return 1; else if (ret = SOCKET_ERROR) printf("send() failed: %dn", WSAGetLastError();
121、; return 1; printf("Send %d bytesn", ret); while(!bSendOnly)
122、 /读取流并显示 ret = recv(sClient, szBuffer, 80, 0);
123、if (ret = 0) / Graceful close return 1; else if (ret = SOCKET_ERROR)
124、0; printf("recv() failed: %dn", WSAGetLastError(); return 1; &
125、#160; szBufferret = '0' printf("%s&quo
126、t;,szBuffer); if(strncmp(szBuffer,"226 Close",strlen("226 Close")=0) &
127、#160; break; if(strncm
128、p(szBuffer,"500 Syntax error",strlen("500 Syntax error")=0) break;
129、60; return 0;int delfun(SOCKET sClient,char name20) int ret;char szBuffer80
130、; szMessage0='0' strcat(szMessage, "del$"); strcat(szMessage,name); ret = send(sClient, szMessage, strlen(szMessage)+1, 0); if (ret = 0)
131、160; return 1; else if (ret = SOCKET_ERROR) printf("send() failed: %dn", WSAGetLastError(); return 1; &
132、#160; printf("Send %d bytesn", ret); while(!bSendOnly) ret = recv(sClient, szBuffer, 80, 0); if (ret = 0)
133、0; / Graceful close return 1; else if (ret = SOCKET_ERROR) &
134、#160; printf("recv() failed: %dn", WSAGetLastError(); return 1;
135、0; szBufferret = '0' if(strncmp(szBuffer,"del ok",strlen("del ok")=0)
136、60; printf("del %s okn",name); break;
137、 printf("%s",szBuffer); if(strncmp(szBuffer,"500 Syntax error",strlen("500 Syntax error")=0)
138、60; break; return 0;int usafun() puts("-"); puts("get
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 防疫工作先进事迹简介(6篇)
- 《供配电技术》6.9 教案
- 景区讲解员实习总结(11篇)
- 有关校园安全的演讲稿范文(33篇)
- 幼儿园生命教育的教案6篇
- 销售公司总经理年会发言稿
- 企业为居民供水合同
- 山西省长治市(2024年-2025年小学五年级语文)人教版竞赛题(上学期)试卷及答案
- 2024年民航运输项目资金筹措计划书代可行性研究报告
- 高考生物复习教案生命活动的调节
- 杭州注册公司公司章程范本
- 中国科学家光学之父王大珩的红色故事PPT大力弘扬科学家精神PPT课件(带内容)
- 配电网工程施工工艺规范课件
- 财务预算编制说明范文(通用十四篇)
- GCMS气相色谱质谱联用仪基础知识和培训教材课件
- 小学科学招聘考试试题含答案(五套)
- 幼儿园大班语言活动《好长好长的电话》教案
- 主题趣味游戏班会
- 机械工程师招聘
- 博物馆安全生产规章制度
- 张家口市基层诊所基本公共卫生服务医疗机构卫生院社区卫生服务中心村卫生室地址信息
评论
0/150
提交评论