TFTP Server with LWIP 2.0.3, simple file transfer

tags: TFTP  LWIP

TFTP is a simple file transfer protocol. I am not too embarrassed about the protocol. For those interested, please refer to RFC1350:https://tools.ietf.org/html/rfc1350    

This example shows that the lower computer runs LWIP as the TFTP server and the Windows side as the client.

  1. Windows send files to the lower computer
  2. Get files from the lower machine

 

The LWIP package is perfect, TFTP is very convenient to use. Open the tftp_server.h file and you only need to write four callback functions, namely:

  • open a file
  • Close file
  • data input
  • Read data

Tftp_server.h as shown:

Of course, there is still one pit, and things are not as smooth as imagined. The author easily implemented the file transmission during debugging, but the file acquisition failed.

According to the description of tftp_server.h above, the read function written by the author returns 0 when the read succeeds, and the result of obtaining the file fails. Later, comparing the official API, I found that the tftp_server.h I downloaded is garbled. As shown in the following figure, the return value is >=0, which is successful, not =0.The final simulation confirms that you need to return the number of bytes read.. Think about it, or you can't know the file size.

 

The following attached code:

 

//tftp
struct tftp_context mytftp;

typedef struct
{
	uint8_t isOpenOK;
	Uint8_t type;//0:GCode 1: Cache GCode 2: Firmware
	uint8_t write;
	char name[64];
}TFTP_Handler;
TFTP_Handler tftp_Handler;

void* TFTP_Open(const char* fname, const char* mode, u8_t write)
{
	uint8_t res;
	char name[64];
	tftp_Handler.write=write;
	
	tftp_Handler.type=0;
	strcpy(name,"0:");
	strcat(name,fname);
	
	if(write)
	{
		res=f_open(file,name,FA_CREATE_ALWAYS|FA_WRITE);
	}
	else
	{
		res=f_open(file,name,FA_OPEN_EXISTING|FA_READ);
	}
	tftp_Handler.isOpenOK=res;
	return &tftp_Handler;
}
void TFTP_Close(void* handle)
{
	uint8_t res=0;
	 
	if(((TFTP_Handler*)handle)->isOpenOK)
		return;
	f_close(file);
	printf("Transfer file completed!\r\n");
}
int TFTP_Read(void* handle, void* buf, int bytes)
{
	uint8_t res;
	res=((TFTP_Handler*)handle)->isOpenOK;
	if(res)return -1;
	res = f_read(file,(uint8_t*)buf,bytes,&br);

	printf("Read File:%d  Len:%d  br:%d\r\n",res,bytes,br);
	return br;
}
int TFTP_Write(void* handle, struct pbuf* p)
{
	uint8_t res;
	res=((TFTP_Handler*)handle)->isOpenOK;
	if(res)return -1;
	res = f_write(file,p->payload,p->len,&bw);

	printf("Write File:%d  Len:%d  bw:%d\r\n",res,p->len,bw);
	return 0;
}
void TFTP_ContextInit()
{
	mytftp.open=TFTP_Open;
	mytftp.close=TFTP_Close;
	mytftp.read=TFTP_Read;
	mytftp.write=TFTP_Write;
}

 

Finally, there are two ways to debug Windows:

  • Using a command prompt

Send and get:

Compare it:

description:

  • Using debugging software

Intelligent Recommendation

TFTP file transfer under Windows

Download the file using tftp to transfer files first in Windowstftp64.exe/tftp32.exe The link to tftp64 is as follows: Link: https://pan.baidu.com/s/19yogpoYAsd7Brhk2GJ-aSA Extraction code: 1234 first...

Linux cloud computing architecture-FTP file transfer protocol and TFTP simple file transfer protocol

Article Directory Linux cloud computing architecture-FTP file transfer protocol and TFTP simple file transfer protocol 1. File Transfer Protocol (FTP) 2. Deploy FTP server 3. Trivial File Transfer Pro...

LinuxProbe 0x14 Virtual Website Host Function (Port), VSFTPD Service Transfer File, TFTP Simple File Transfer Protocol

Virtual website host function Port number The port number-based virtual host feature allows users to access website resources on the server via the specified port number. When using Apache to configur...

LWIP-TFTP-Notes

TFTP protocol Follow the RFC1350 standard here purpose Tftp is a simple file transfer protocol, designed based on udp, but there is also a tcp version. The function of only includes: reading and writi...

Use TFTP to file file transfer with ARM board

TFTP use 1 ping To ensure that the development board and PC can ping each other, if not PING pass, please check the following reason: Whether the network cable is connected; Whether the IP address of ...

More Recommendation

Solution for TFTP Server Transfer Timed Out

Method 1: Restart the server Method 2: Uninstall reinstallation Step 1: Uninstall Step 2: Installation Pay attention to modify configuration: Step 3: Restart the server Step 4: Test At last:...

Fifteen of the TCP/IP protocol learning record: TFTP: Simple File Transfer Protocol

Unlike the File Transfer Protocol (F T P) using T C P, in order to keep it simple and short, the T F T P will use U D P. The advantage of T F T P is the simplicity of implementation. Single rather tha...

Detailed TCP/IP Volume 1: Protocol (9) [DNS: Domain Name System, TFTP: Simple File Transfer Protocol]

DNS is a distributed database for TCP/IP applications. It provides translation between host names and IP addresses and routing information about e-mail. DNS basics There is no organization that manage...

TFTP: Simple text transfer protocol packet format analysis and file reading code implementation

In this section, we look at the assembly method of TFTP data packets to lay the foundation for our code to implement the protocol. There are 5 different data packets in the TFTP protocol, which corres...

[TCP-IP Details Volume 1: Agreement] CH15 TFTP: Simple File Transfer Protocol

table of Contents 1 Introduction 2. Agreement 2.1 Read request (RRQ) 2.2 Write Request (WRQ) 2.3 Others 3. Security 4. Small knot 1 Introduction TFTP uses UDP (Different Document Transfer Protocol FTP...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top