首页 > 解决方案 > Telnet 拒绝连接

问题描述

我正在设置一个拼写检查程序,但无法连接到包含该程序的服务器。我不知道这是否是创建套接字连接或我如何使用 telnet 客户端的问题。

我已经在我自己的计算机上使用地址为 127.0.0.1 和端口 3335 的 telnet 对其进行了测试,它工作正常。然后,当我尝试将其测试到 linux 服务器时,telnet 无法连接,要么超时,要么直接拒绝连接。

有关我何时尝试连接到 linux 服务器的一些背景知识。我 ssh 进入服务器运行程序。然后我运行程序,然后在另一个 shell 窗口中使用 telnet 来尝试连接。我使用命令获取服务器的地址nslookup mach_name

我将在下面粘贴用于创建套接字的代码,以防它是程序中的代码问题:

    int main(int argc, char** argv) {
       dequeue(); // to take junk values out of the queue
       // initialize all values
       pthread_mutex_init(&workLock, NULL);
       pthread_cond_init(&canTake, NULL);
       pthread_cond_init(&canPut,NULL);
       char *dict = NULL, *port = NULL;
       int newsockfd; // to create the socket conditions 
       struct sockaddr_in clienaddr; // to get the client info
       socklen_t clilen;
       pthread_t threads[NUM_WORKERS]; // for the thread id
       if(argc < 2){
           dict = DEFAULT_DICT;
           port = DEFAULT_PORT;
       } else {
           dict = argv[1];
           port = argv[2];         
       }
       for (int i = 0; i < NUM_WORKERS; i++) {
           pthread_create(&(threads[i]), NULL, (void *) &worker,NULL);
       }
       load_table(dict); // load the table 
       int listenfd, portno;
       struct sockaddr_in server;
       socklen_t srv_len = sizeof(server);
       listenfd = socket(AF_INET, SOCK_STREAM, 0);
       if(listenfd < 0){
           perror("socket failed");
           exit(EXIT_FAILURE);
        }
        // changes all the buffers to 0 
        bzero((char *) &server, sizeof(server));
        // changes the port into a number 
        portno = atoi(port);
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_family = AF_INET;
        server.sin_port = htons(portno); 
        //binds the port to the listenfd
        if(bind(listenfd, (struct sockaddr *) &server, srv_len)< 0){
           perror("bind failed");
           exit(EXIT_FAILURE);
        }
        // waits for incoming connections 
        if(listen(listenfd,BACKLOG) < 0){
           close(listenfd);
           perror("listen failed");
           exit(EXIT_FAILURE);
        }
        clilen = sizeof(clienaddr); 
        bzero((char *) &clienaddr, sizeof(clienaddr));
        // listenfd = getlistenfd(port); // get the listen fd

        while(1){
           newsockfd = accept(listenfd, (struct sockaddr *) &clienaddr, &clilen); // accept a connection 
           pthread_mutex_lock(&workLock); // make sure only one thread can access the queue 
           while(size == MAXSIZE) pthread_cond_wait(&canPut, &workLock); // make sure main thread is put to sleep if there is no space for the queue
           enqueue(newsockfd); // add the socket to the queue
           pthread_cond_signal(&canTake); // signal that worker threads can take from the queue
           pthread_mutex_unlock(&workLock); // unlock so another socket can be added 
       }
       close(listenfd);
       return (EXIT_SUCCESS);
   }

标签: clinuxmultithreadingsocketstelnet

解决方案


推荐阅读