tftp客户端没有从服务器接收(发送到错误的端口)

时间:2012-12-21 13:31:29

标签: c++ unix client port tftp

我正在编写自己的TFTP客户端。 客户端用c ++编写(部分c)并在unix系统上运行(Linux Debian stable)。

这是我的问题:

我可以将读取请求(RRQ)发送到TFTP服务器(在Windows XP上打开TFTP服务器)。 TFTP服务器接收RRQ,但无法发送答案。

在服务器端,我看到此消息:客户端192.168.1.110:55239 C:\ OpenTFTPServer \ test.txt,通信错误

但是端口55239不是我在TFTP-Client中定义的端口。

以下是我的代码部分:

  • 设置客户端和服务器地址:

    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&结合:

    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!");
    }
    
  • 创建TFTP请求:

    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'};
    
  • 发送TFTP请求:

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

这是我的问题:

我做错了什么,TFTP-Server收到错误的端口?我认为它应该用cliaddr.sin_port = htons(cliport);

设置

感谢您的帮助&amp;问候 波多黎各

答案:

我的问题是,我将错误的套接字与错误的地址结构组合在一起

以下是我的工作代码部分:

  • 创建套接字:

    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!");
    }
    
  • 设置客户端和服务器地址:

    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&amp;结合:

    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!");
    }
    
  • 创建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};
    
  • 发送TFTP-Request:

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

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

1 个答案:

答案 0 :(得分:0)

我分别添加了答案,所以没有人必须在原帖中搜索。

<强>答案:

我的问题是,我将错误的套接字与错误的地址结构组合在一起

以下是我的工作代码部分:

  • 创建套接字:

    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!");
    }
    
  • 设置客户端和服务器地址:

    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&amp;结合:

    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!");
    }
    
  • 创建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};
    
  • 发送TFTP-Request:

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

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