连接错误:“无主机路由”

时间:2013-09-26 23:13:25

标签: c linux sockets

我正在编写基于AX.25协议的服务器/客户端c程序。 服务器创建套接字,绑定成功并监听即将到来的连接。 客户端在不同的线程中运行但在与“无路由到主机”连接时失败

服务器代码

#include <sys/socket.h>
#include <netax25/ax25.h>
#include <netax25/axlib.h>
#include <netax25/axconfig.h>

#include <stdio.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <signal.h>
#include <syslog.h>

#include <sys/types.h>
#include <linux/socket.h>
#include <stdlib.h>

#include <sys/un.h>

#include <string.h>
#include <errno.h>



int main(int argc,char **argv,char **envp) {


    int ax25_socket = -1;
    unsigned char buffer[512];

    struct full_sockaddr_ax25 addr, axconnect ;

      char *port ="3";// sm0 port number:3
      char *call = "OH2BNS-8";// sm0 callsign


      bzero((char *) &addr, sizeof(struct full_sockaddr_ax25));

        addr.fsa_ax25.sax25_family = AF_AX25;
        addr.fsa_ax25.sax25_ndigis = 1;


        if (ax25_config_load_ports() == 0) {
                    printf( "Problem with axports file");
                    //return -1;
            }

        char* ax25port = (char*) ax25_config_get_addr(port);

        ax25_aton_entry( call, addr.fsa_ax25.sax25_call.ax25_call);
        ax25_aton_entry( ax25port, addr.fsa_digipeater[0].ax25_call);


        ax25_socket = socket(AF_AX25, SOCK_SEQPACKET, 0);
            if (ax25_socket < -1)
              printf( "error in create socket");

            if (bind(ax25_socket, (struct sockaddr *)&addr, sizeof(struct full_sockaddr_ax25)) < 0) {

             perror("bind--");
             return -1;
            }


            if(listen(ax25_socket,2) != 0)
            {
                printf("cannot listen on socket!\n");
                        close(ax25_socket);
                        return 0;
            }
                puts("listening");
            //bzero((char *) &axconnect, sizeof(struct full_sockaddr_ax25));
                int len =sizeof(struct full_sockaddr_ax25);
            int temp_sock_desc = accept(ax25_socket, (struct sockaddr*)&axconnect, &len);
                if (temp_sock_desc == -1)
                {
                    printf("cannot accept client!\n");
                    close(ax25_socket);
                    return 0;
                }




            return 0;
}

客户端代码

#include <sys/socket.h>
#include <netax25/ax25.h>
#include <netax25/axlib.h>
#include <netax25/axconfig.h>

#include <stdio.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <signal.h>
#include <syslog.h>

#include <sys/types.h>
#include <linux/socket.h>
#include <stdlib.h>

#include <sys/un.h>

#include <string.h>
#include <errno.h>


int main(int argc, char *argv[])
{
    int ax25_socket = -1;
    unsigned char buffer[512];

    struct full_sockaddr_ax25 axconnect ;

    char *port ="3";// sm0 port number:3
    char *call ="OH2BNS-8";// sm0 callsign

    bzero((char *) &axconnect, sizeof(struct full_sockaddr_ax25));

    axconnect.fsa_ax25.sax25_family = AF_AX25;
    axconnect.fsa_ax25.sax25_ndigis = 1;

         if (ax25_config_load_ports() == 0) {
                printf( "Problem with axports file");
                   //return -1;
                        }



                 char* ax25port = (char*) ax25_config_get_addr(port);

                 ax25_aton_entry( call, axconnect.fsa_ax25.sax25_call.ax25_call);
                 ax25_aton_entry( ax25port, axconnect.fsa_digipeater[0].ax25_call);

                    ax25_socket = socket(AF_AX25, SOCK_SEQPACKET, 0);
                                  if (ax25_socket < -1)
                                       printf( "error in create socket");



                            if (connect(ax25_socket, (struct sockaddr *)&axconnect, sizeof(struct full_sockaddr_ax25)) != 0) {
                                        perror("--");
                                            switch (errno) {
                                                case ECONNREFUSED:
                                                    printf("*** Connection refused\r");
                                                    break;
                                                case ENETUNREACH:
                                                    printf("*** No known route\r");
                                                    break;
                                                case EINTR:
                                                    printf("*** Connection timed out\r");
                                                    break;
                                                default:
                                                    printf("ERROR: cannot connect to AX.25 callsign\r");
                                                    break;
                                            }

                                            close(ax25_socket);
                                    }
                        printf("Connected!!\r");
                        int n = write(ax25_socket,"Message!!!!",18);
                                if(n = -1)
                                    {
                                            perror("write--");
                                    }


                return 0;
}

2 个答案:

答案 0 :(得分:2)

简单地说,“没有主机路由”“意味着客户端的路由表中没有服务器IP地址的路由。你能ping服务器的IP地址吗?很可能你不应该并且ping应该说服务器不可访问。如果是,那么这个错误与你的程序无关,你可能遇到连接问题。

您可以在“route -n”的输出中找到服务器的条目。如果没有,那么您应检查服务器子网的更大前缀。如果这也不存在,那么您应该确认您有默认路由设置。

为了进一步确认,我会做以下两项测试。首先,如果您尝试在同一个盒子上运行客户端/服务器会发生什么?其次,如果您尝试在两个盒子(存在于同一子网中)和同一LAN上运行客户端/服务器会发生什么?如果您没有看到此问题并且您的应用程序运行正常,那么这应该确认您遇到了连接问题。

答案 1 :(得分:0)

我知道这是一个老问题,但我怀疑ax25port有问题 - 应该是YOURCALL-0,其中YOURCALL与现有ax25端口的HWaddr匹配(试试/sbin/ifconfig | fgrep AX.25