简体   繁体   中英

tftp client not receiving from server (send to wrong port)

I'm writing my own TFTP-client. The client is written in c++ (part wise c) and runs on a unix system (Linux Debian stable).

Here is my problem:

I can send my read-request(RRQ) to the TFTP-server(Open TFTP Server on Windows XP). The TFTP-server receives the RRQ, but can't send the answer.

On the server side I see this message: Client 192.168.1.110:55239 C:\\OpenTFTPServer\\test.txt, Communication Error

But the port 55239 is not the port I defined in my TFTP-Client.

Here are my code-parts:

  • set client and server adresses:

     memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(servport); // servport=69 servaddr.sin_addr.s_addr = inet_addr("192.168.1.152"); memset(&cliaddr,0,sizeof(cliaddr)); cliaddr.sin_family = AF_INET; cliaddr.sin_port = htons(cliport); // cliport=13337 cliaddr.sin_addr.s_addr = inet_addr("192.168.1.110"); 
  • connect & bind:

     if (connect(serv, (struct sockaddr *) &servaddr, serv_length) < 0) { bail("Unable to connect with the server!"); } if(bind(cli, (struct sockaddr *)&cliaddr, cli_length)<0) { bail("Unable to bind to adr & port!"); } 
  • create TFTP request:

     memset( &cBuf1, 0, sizeof cBuf1); rdwrFrame = (struct tftp_wr_rq *)&cBuf1; rdwrFrame->opcode=htons(1); rdwrFrame->data={'t','e','s','t','.','t','x','t','\\0','o','c','t','e','t','\\0'}; 
  • send TFTP request:

     if (sendto (serv,cBuf1,19,0,(struct sockaddr *)&servaddr,serv_length) < 0) { bail ("Send the first requirment Frame error -- UDP"); } 

Here is my question:

What am I doing wrong, that the TFTP-Server receives the wrong port? I thought it should be set with cliaddr.sin_port = htons(cliport);

Thanks for your help & regards Rico

Answer:

My problem was, that I combined the wrong sockets with the wrong address structs

Here are my working code-parts:

  • create sockets:

     serv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (serv < 0) { bail("Unable to create socket tx!"); } cli = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (cli < 0) { bail("Unable to create socket rx!"); } 
  • set client and server adresses:

     memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(servport); servaddr.sin_addr.s_addr = inet_addr("192.168.1.152"); memset(&cliaddr,0,sizeof(cliaddr)); cliaddr.sin_family = AF_INET; cliaddr.sin_port = htons(cliport); cliaddr.sin_addr.s_addr = inet_addr("192.168.1.110"); 
  • connect & bind:

     if(bind(cli, (struct sockaddr *)&cliaddr, cli_length)<0) { bail("Unable to bind to adr & port!"); } if (connect(serv, (struct sockaddr *) &servaddr, serv_length) < 0) { bail("Unable to connect with the server!"); } 
  • create TFTP-Request:

     memset( &cBuf1, 0, sizeof cBuf1); rdwrFrame = (struct tftp_wr_rq *)&cBuf1; rdwrFrame->opcode=htons(1); rdwrFrame->data={'t','e','s','t','.','t','x','t',(char)0,'o','c','t','e','t',(char)0}; 
  • send TFTP-Request:

     if (sendto (cli,cBuf1,19,0,(struct sockaddr *)&servaddr,serv_length) < 0) { bail ("Send the first requirment Frame error -- UDP"); } 
  • receive Data:

     recvfrom(cli,cBuf2,19,0,(struct sockaddr *)&cliaddr,(socklen_t*)&cli_length); 

I added the answer separatly, so nobody has to search it in the original post.

Answer:

My problem was, that I combined the wrong sockets with the wrong address structs

Here are my working code-parts:

  • create sockets:

     serv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (serv < 0) { bail("Unable to create socket tx!"); } cli = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (cli < 0) { bail("Unable to create socket rx!"); } 
  • set client and server adresses:

     memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(servport); servaddr.sin_addr.s_addr = inet_addr("192.168.1.152"); memset(&cliaddr,0,sizeof(cliaddr)); cliaddr.sin_family = AF_INET; cliaddr.sin_port = htons(cliport); cliaddr.sin_addr.s_addr = inet_addr("192.168.1.110"); 
  • connect & bind:

     if(bind(cli, (struct sockaddr *)&cliaddr, cli_length)<0) { bail("Unable to bind to adr & port!"); } if (connect(serv, (struct sockaddr *) &servaddr, serv_length) < 0) { bail("Unable to connect with the server!"); } 
  • create TFTP-Request:

     memset( &cBuf1, 0, sizeof cBuf1); rdwrFrame = (struct tftp_wr_rq *)&cBuf1; rdwrFrame->opcode=htons(1); rdwrFrame->data={'t','e','s','t','.','t','x','t',(char)0,'o','c','t','e','t',(char)0}; 
  • send TFTP-Request:

     if (sendto (cli,cBuf1,19,0,(struct sockaddr *)&servaddr,serv_length) < 0) { bail ("Send the first requirment Frame error -- UDP"); } 
  • receive Data:

     recvfrom(cli,cBuf2,19,0,(struct sockaddr *)&cliaddr,(socklen_t*)&cli_length); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM