简体   繁体   中英

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. 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). Trying the nc -zv 10.130.28.130 8080 is not successful, however pinging the server is. My client is looping the socket.connect(10.130.28.130, 8080) as long as no connections is accepted.

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:

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

Maybe you were overthinking it or I'm underthinking it.

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