套接字号因为每个新的telnet连接都没有改变

时间:2015-11-30 15:32:42

标签: c++ sockets telnet tcp-ip

以下是前提: - 我想显示每个telnet连接和断开连接的用户名。 telnet终端显示远程连接的微处理器上发生的所有活动的日志。 我在这里设计了一种检查如何从现有日志结构中显示用户名的方法。

但是在调试期间,我发现对于每个创建或关闭的新telnet连接,我的套接字no继续保持不变为8.

telnet连接是否应该在概念上以这种方式工作?

以下是代码的片段: -

void connectTelnet()
{
   sockaddr_in       clientIn;
   INT               addressLength;
   INT32             socketNum;
   TEXT              stringMsg[200];

   socketNum = accept(telnet_data.server_socket, (sockaddr*)&client_in, &addressLength);

   if (socketNum != -1)             //right here, the socketNum = 8
   {
      this->initializeTelnetSocket((UINT8)socketNum);

      this->telnet_data.socket_data[socketNum].remote_ip_port =
            client_in.sin_port;
      this->telnet_data.socket_data[socketNum].remote_ip_address[0] =
            (UINT8)(client_in.sin_addr.s_addr >> 24);
      this->telnet_data.socket_data[socketNum].remote_ip_address[1] =
            (UINT8)(client_in.sin_addr.s_addr >> 16);
      this->telnet_data.socket_data[socketNum].remote_ip_address[2] =
            (UINT8)(client_in.sin_addr.s_addr >> 8);
      this->telnet_data.socket_data[socketNum].remote_ip_address[3] =
            (UINT8)(client_in.sin_addr.s_addr);
   //log username here
    ethernet.logEthernetConnection(
            TCP, TELNET, TELNET_PORT,
            telnet_data.socket_data[socketNum].remote_ip_address,
            telnet_data.socket_data[socketNum].remote_ip_port,
            (TEXT *)NULL, LOG_CONNECTED, 1);
      this->telnet_data.socket_data[socketNum].connected = TRUE;

      //Set tcp push flag in this socket. so that data will be sent quickly..
      ethernet.setTCPFlag(socketNum, TCP_FLAGS_PUSH);

      sprintf(stringMsg, "login: ");
      this->telnetSendMessage(socketNum, (UINT8*)stringMsg, strlen(stringMsg));
   }
   else
   {
      errorDialogue("\ntelnet socket accept fail\n");
   }
}


void quitTelnet(INT socketNum)
{
   // Bounds check the income data
   if ((socketNum < 0) || (socketNum >= NUM_SOCKETS))       //right here, the socketNum = 8
   {
      diagError("\ntelent socket close failure, invalid socket number");
      return;  // invalid socket we can't continue.
   }
   //log username here
   ethernet.logEthernetConnection( TCP, TELNET, TELNET_PORT,
         telnet_data.socket_data[socketNum].remote_ip_address,
         telnet_data.socket_data[socketNum].remote_ip_port,
         this->telnet_data.socket_data[socketNum].loginStorage,
         LOG_DISCONNECTED,672);         

   this->initializeTelnetSocket(socketNum);
   close(socketNum);
   terminal.sendClosePortSocketMessage(socketNum);
}



int accept(int socketNum, sockaddr * client_address, int * addressLength)
{
   sockaddr_in * in_address = (sockaddr_in *)client_address;
   int         ret_val;
   Uint        counter;

   // verify socket number is value
   if (socketNum < 0 || socketNum >= NUM_SOCKETS)
   {
      diagError("Invalid Socket Number");
      management.saveCallStack(getFramePtr());  // Debug code to find out who is sending us invalid sockets
      return SOCKET_ERROR;
   }

   if((socketData.sockets[socketNum].state == USED) && (socketData.sockets[socketNum].type == SOCK_STREAM))
   {
      if(socketData.sockets[socketNum].protocol.tcp.state == LISTENING)
      {
         // wait for a connection
         while(socketData.sockets[socketNum].protocol.tcp.num_children == 0)
         {
            OSTimeDly(5);
         }

         // get the connections socket number
         ret_val = socketData.sockets[socketNum].protocol.tcp.child_sockets[0];
         OSSchedLock();
         socketData.sockets[socketNum].protocol.tcp.num_children--;
         for(counter = 0; counter < socketData.sockets[socketNum].protocol.tcp.num_children; counter++)
         {
            socketData.sockets[socketNum].protocol.tcp.child_sockets[counter] =
                  socketData.sockets[socketNum].protocol.tcp.child_sockets[counter+1];
         }
         OSSchedUnlock();

         // fill in the client IP data
         in_address->sin_family = AF_INET;
         in_address->sin_addr.s_addr = ((UINT8)(socketData.sockets[ret_val].remote_ip_address[0]) << 24) |
               ((UINT8)(socketData.sockets[ret_val].remote_ip_address[1]) << 16) |
               ((UINT8)(socketData.sockets[ret_val].remote_ip_address[2]) << 8) |
               (UINT8)(socketData.sockets[ret_val].remote_ip_address[3]);
         in_address->sin_port = socketData.sockets[ret_val].remote_port;
         *addressLength = sizeof(sockaddr_in);
      }
      else
      {
         ret_val = SOCKET_ERROR;
      }

   }
   else
   {
      ret_val = SOCKET_ERROR;
   }

   return ret_val;
}

0 个答案:

没有答案