简体   繁体   English

端口好像没开,虽然在听

[英]Port does not seem to be open, even though listening

My server is not going passed the listening stage, even though my client is trying to run the socket.connect() function in a loop until connection is made.我的服务器没有通过监听阶段,即使我的客户端试图在一个循环中运行socket.connect() function 直到建立连接。 Is something flawed in my server code?我的服务器代码有问题吗?

#define PORT 8080
void server()
{
    int serverSocket, newSocket;
    struct sockaddr_in serverAddr;
    struct sockaddr_storage serverStorage;
    socklen_t addr_size;
    int opt = TRUE;

    //Create socket
    serverSocket = socket(PF_INET, SOCK_STREAM, 0);
    if (setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, 
        sizeof(opt))<0)
        error("SETSOCKOPT ERROR");

    //Configure setting of the server address struct
    serverAddr.sin_family = AF_INET; 
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); 
    serverAddr.sin_port = htons(PORT); 

    //Set all bits of the padding field to 0 
    memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

    //Bind the address struct to the socket 
    bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

    //Listen on the socket, with max 40 connection requests queued

    if(listen(serverSocket,40)==0)
      printf("\nListening\n");
    else
      printf("\nError\n");
    pthread_t tid[60];
    int i = 0;
    while(1)
    {
      //Accept call creates a new socket for the incoming connection
      addr_size = sizeof serverStorage;
      printf("HERE");
      newSocket = accept(serverSocket, (struct sockaddr *) &serverStorage, &addr_size);
      //pthread_mutex_lock(&lock);
      //for each client request creates a thread and assign the client request to it to process
      //so the main thread can entertain next request
      int error_thread = pthread_create(&tid[i], NULL, socketThread, (void *)(uintptr_t) newSocket);
      if(  error_thread != 0 )
      {
        printf("\nFailed to create thread, %i\n", error_thread);
        rebootOWASYS();
      }
      else
      {
          printf("\nSOCKET %i CREATED\n", newSocket);
      }

      if( i >= 50)
      {
          i = 0;
          while(i < 50)
          {
              pthread_join(tid[i++],NULL);
          }
          i = 0;
      }
    }
}

The server manages to print "Listening", however, my client is unable to connect (does not reach the "here" print).服务器设法打印“Listening”,但是,我的客户端无法连接(未到达“here”打印)。 Trying the nc -zv 10.130.28.130 8080 is not successful, however pinging the server is.尝试nc -zv 10.130.28.130 8080不成功,但是对服务器执行 ping 操作。 My client is looping the socket.connect(10.130.28.130, 8080) as long as no connections is accepted.只要不接受连接,我的客户端就会循环socket.connect(10.130.28.130, 8080)

def try_connect(address):
    global connected
    global client
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.settimeout(2)
    try:
        client.connect(address)
        connected = True
    except:
        client.close()
        connected = False

    finally:
        return [client, connected]

Am I missing something obvious?我错过了一些明显的东西吗?

The first problem I see is trying to call the connect function without two sets of parentheses:我看到的第一个问题是尝试在没有两组括号的情况下调用连接 function:

client.connect((addr, port)) # Like that

Maybe you were overthinking it or I'm underthinking it.也许你想多了,或者我想多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使安装了模块,导入似乎也不起作用 python 并返回“ModuleNotFoundError” - Importing does not seem to work python and returns "ModuleNotFoundError" even though module is installed TypeError: 'NoneType' object 不支持项目分配,即使变量似乎是正确的 - TypeError: 'NoneType' object does not support item assignment, even though variables seem to be correct Python:在路由器后面打开一个侦听端口(upnp?) - Python: Open a Listening Port Behind a Router (upnp?) AttributeError-即使似乎没有属性错误 - AttributeError - Even though there seem to be no attribute error Request.form 找不到我的字段,即使它们似乎存在 - Request.form not finding my fields even though they seem to exist 两个变量似乎指向同一个列表,即使它们应该是唯一的 - Two variables seem to point to the same list, even though they should be unique RegEX似乎不匹配,即使它应该匹配 - RegEX doesn't seem to match, even though it should 如何保护在开放端口上侦听,发送和接收的python套接字? - How to secure a python socket listening, sending and receiving on an open port? pyftpdlib运行,但似乎没有在侦听端口,但其他ftp服务器应用程序仍在工作 - pyftpdlib runs but doesn't seem to be listening on port yet other ftp server apps work 为什么它说响应没有定义,即使它是 - Why does it say response is not defined even though it is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM