資源簡介
UDP協(xié)議實(shí)現(xiàn)linux下的TFTP客戶端代碼
代碼片段和文件信息
/**********************************************************
Date:? NOV?28th?2006
Project?: TFTP?Client
Programers:
Jonathan?Felske
Andrew?Fullard?
Craig?Holmes
Reza?Rahmanian?
Adam?Tomalty?
File: TFTP?Client?(main)
Purpose: A?TFTP?client?that?will?request?a?connections?from
the?server?and?transefet?files.
Notes: Here?we?are?using?the?sendto?and?recvfrom
functions?so?the?server?and?client?can?exchange?data.
***********************************************************/
#include?“tftp.h“
/*a?function?to?print?the?Help?menu*/
void?help?(char?*);
/*a?function?to?create?the?request?packet?read?or?write*/
int?req_packet?(int?opcode?char?*filename?char?*mode?char?buf[]);
/*a?function?to?creat?an?ACK?packet*/
int?ack_packet?(int?block?char?buf[]);
/*a?function?to?create?the?Error?packets*/
int?err_packet?(int?err_code?char?*err_msg?char?buf[]);
/*a?function?that?will?print?the?ip:port?pair?of?the?server?or?client?plus?data?sent?or?recieved*/
void?ip_port?(struct?sockaddr_in?host);
/*a?function?to?send?a?file?to?the?server*/
void?tsend?(char?*pFilename?struct?sockaddr_in?server?char?*pMode
????int?sock);
/*a?function?to?get?a?file?from?the?server*/
void?tget?(char?*pFilename?struct?sockaddr_in?server?char?*pMode?int?sock);
/*?default?values?which?can?be?controlled?by?command?line?*/
char?path[64]?=?“/tmp/“;
int?port?=?69;
unsigned?short?int?ackfreq?=?1;
int?datasize?=?512;
int?debug?=?0?w_size?=?1?p_length?=?512;
int
main?(int?argc?char?**argv)
{
??/*local?variables?*/
??extern?char?*optarg;
??int?sock?server_len?len?opt; //n;
??char?opcode?filename[196]?mode[12]?=?“octet“;
??struct?hostent?*host; /*for?host?information?*/
??struct?sockaddr_in?server; //?client;?/*the?address?structure?for?both?the?server?and?client?*/
??FILE?*fp; /*a?pointer?to?a?file?that?the?client?will?send?or?get?from?the?server?*/
??if?(argc?2)
????{
??????help?(argv[0]);
??????return?0;
????}
??if?(!(host?=?gethostbyname?(argv[1])))
????{
??????perror?(“Client?could?not?get?host?address?information“);
??????exit?(2);
????}
/*?All?of?the?following?deals?with?command?line?switches?*/
??while?((opt?=?getopt?(argc?argv?“dnoh:P:p:g:l:w:“))?!=?-1) /*?this?function?is?handy?*/
????{
??????switch?(opt)
{
case?‘d‘: /*?debug?mode?(no?opts)?*/
??debug?=?1;
??break;
case?‘P‘: /*?Port?(opt?required)?*/
??port?=?atoi?(optarg);
??if?(debug)
????{
??????printf?(“Client:?The?port?number?is:?%d\n“?port);
????}
??break;
case?‘p‘: /*?put?a?file?on?the?server?*/
??strncpy?(filename?optarg?sizeof?(filename)?-?1);
??opcode?=?WRQ;
??fp?=?fopen?(filename?“r“); /*opened?the?file?for?reading?*/
??if?(fp?==?NULL)
????{
??????printf?(“Client:?file?could?not?be?opened\n“);
??????return?0;
????}
??if?(debug)
????{
??????printf?(“Client:?The?file?name?is:?%s?and?can?be?read“
??????filename);
????}
??fclose?(fp);
??break;
case?‘g‘: /*get?a?file?from?the?server?*/
??strncpy?(filename?optarg?sizeof?(filename)?-?1);
??opcode?=?RR
評論
共有 條評論