#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#define N 128
typedef struct sockaddr SA;
void PrintHelp()
{
printf("help : display help info\n");
printf("list : get file list of server\n");
printf("get : get <file>\n");
printf("put : put <file>\n");
printf("quit : quit the client\n");
return;
}
void ProcessList(struct sockaddr_in server_addr)
{
int sockfd, nbyte;
char buf[N];
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
printf("fail to list\n");
return;
}
if (connect(sockfd, (SA *)&server_addr, sizeof(server_addr)) < 0)
{
printf("fail to connect server\n");
goto ERROR_1;
}
strcpy(buf, "L");
send(sockfd, buf, N, 0);
while ((nbyte = recv(sockfd, buf, N, 0)) != 0)
{
printf("%s\n", buf);
}
ERROR_1:
close(sockfd);
return;
}
void ProcessGet(struct sockaddr_in server_addr, char command[])
{
int sockfd, nbyte, fd;
char buf[N];
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
printf("fail to get\n");
return;
}
if (connect(sockfd, (SA *)&server_addr, sizeof(server_addr)) < 0)
{
printf("fail to connect server\n");
goto ERROR_2;
}
sprintf(buf, "G%s", command+4);
send(sockfd, buf, N, 0);
recv(sockfd, buf, N, 0);
if (buf[0] == 'N') // no such file
{
printf("No such file on server\n");
goto ERROR_2;
}
if ((fd = open(command+4, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
{
printf("fail to create local file %s\n", command+4);
goto ERROR_2;
}
while ((nbyte = recv(sockfd, buf, N, 0)) > 0)
{
write(fd, buf, nbyte);
}
close(fd);
ERROR_2:
close(sockfd);
return;
}
void ProcessPut(struct sockaddr_in server_addr, char command[])
{
int sockfd, fd, nbyte;
char buf[N];
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
printf("fail to get\n");
return;
}
if (connect(sockfd, (SA *)&server_addr, sizeof(server_addr)) < 0)
{
printf("fail to connect server\n");
goto ERROR_3;
}
if ((fd = open(command+4, O_RDONLY)) < 0)
{
printf("fail to open %s\n", command+4);
goto ERROR_3;
}
sprintf(buf, "P%s", command+4);
send(sockfd, buf, N, 0);
while ((nbyte = read(fd, buf, N)) > 0)
{
send(sockfd, buf, nbyte, 0);
}
close(fd);
ERROR_3:
close(sockfd);
return;
}
int main(int argc, char *argv[])
{
int sockfd, fd, nbyte;
char command[32];
struct sockaddr_in server_addr;
if (argc < 3)
{
printf("Usage : %s <server_ip> : <port>\n", argv[0]);
exit(-1);
}
// XXX:step 1 int socket(int domain, int type, int protocol);
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "fail to socket : %s\n", strerror(errno));
exit(-1);
}
#ifdef _DEBUG_
printf("socket is %d\n", sockfd);
#endif
// XXX:step 2 int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
//server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
while ( 1 )
{
printf("<client> ");
fgets(command, 32, stdin);
command[strlen(command)-1] = '\0'; // overwrite the '\n'
if (strcmp(command, "help") == 0)
{
PrintHelp();
}
else if (strcmp(command, "list") == 0)
{
ProcessList(server_addr);
}
else if (strncmp(command, "get ", 4) == 0)
{
ProcessGet(server_addr, command);
}
else if (strncmp(command, "put ", 4) == 0)
{
ProcessPut(server_addr, command);
}
else if (strcmp(command, "quit") == 0)
{
printf("Bye\n");
break;
}
else
{
printf("wrong command, 'help' for command list\n");
}
}
return 0;
}
tftp-test.zip_linux tftp_tftp c_tftp linux
版权申诉
5星 · 超过95%的资源 103 浏览量
2022-09-21
09:03:36
上传
评论
收藏 483KB ZIP 举报
weixin_42651887
- 粉丝: 79
- 资源: 1万+
最新资源
- SVID_20240523_141155_1.mp4
- Code for the complete guide to tkinter tutorial
- 关于百货中心供应链管理系统.zip
- SimpleFolderIcon-master 修改Unity的Project下的文件夹图标
- A python Tkinter widget to display tile based maps
- A pure Python library for adding tables to a Tkinter application
- Vector资源文件.zip
- MobaXterm-Installer
- MicroMsg.xlsx
- 88-520告白(520气球).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈