⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpserv.c

📁 nucleus source 源码 全部源码
💻 C
📖 第 1 页 / 共 2 页
字号:
       demo. */
    dev_ptr = DEV_Get_Dev_By_Name (devices[0].dv_name);

    /* Change to SERVER mode */
    NCP_Change_IP_Mode (NCP_SERVER, dev_ptr);

    /* Set the IP address to assign to the client */
    NU_Set_PPP_Client_IP_Address (cli_ip_addr, devices[0].dv_name);

    /* Switch to PPP mode. */
    NU_Change_Communication_Mode(MDM_NETWORK_COMMUNICATION, devices[0].dv_name);

    /* Start the PPP negotiation. This call is only used for the null modem
       demos. It is normally handled by NU_Wait_For_PPP_Client. */
    status = PPP_Lower_Layer_Up(serv_ip_addr, dev_ptr);

    if (status != NU_SUCCESS)
    {
        printf("PPP_Lower_Layer_Up failed.\n");
        DEMO_Exit(13);
    }
    /**** These two functions calls are only needed because ****
     **** this demo is running over null modem. If a real   ****
     **** modem was used this would be taken care of by the ****
     **** PPP service NU_Wait_For_PPP_Client.               ****/

    /* Set our new address */
    DEV_Attach_IP_To_Device (devices[0].dv_name, serv_ip_addr, subnet);

    /* Add a route to this new node on the network. */
    RTAB_Add_Route (dev_ptr, *(UINT32 *)cli_ip_addr, *(UINT32 *)subnet,
                    *serv_ip_addr, (RT_UP | RT_HOST));

    /* open a connection via the socket interface */
    if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) < 0)
    {
        printf("Could not open conncection via NU_Socket.\n");
        DEMO_Exit(14);
    }

    status = NU_Allocate_Memory(&System_Memory, &pointer, 
                                sizeof(struct addr_struct), NU_SUSPEND);
    if (status != NU_SUCCESS)
    {
        printf("Could not allocate memory for seraddr");
        DEMO_Exit(15);
    }

    servaddr = (struct addr_struct *)pointer;

    /* fill in a structure with the server address */
    servaddr->family    = NU_FAMILY_IP;
    servaddr->port      = 7;
    memcpy(&servaddr->id, serv_ip_addr, 4);

    /* make an NU_Bind() call to bind the server's address */
    if ((NU_Bind(socketd, servaddr, 0)) < 0)
    {
        printf("NU_Bind failed.\n");
        DEMO_Exit(16);
    }

    /* be ready to accept connection requests */
    status = NU_Listen(socketd, 10);

    if (status != NU_SUCCESS)
    {
        printf("NU_Listen failed.\n");
        DEMO_Exit(17);
    }

    for (i=0; i < DEMO_MAX_CONNECTIONS; i++)
    {
        /* block in NU_Accept until a client attempts 
           connection */
        newsock = NU_Accept(socketd, &client_addr, 0);
        if (newsock >= 0)
        {
            printf("\nClient has connected.\n ");

            /* process the new connection */
            status = NU_Send_To_Queue(&socketQueue, (UNSIGNED *)&newsock, 
                     1, NU_SUSPEND);
            NU_Sleep(2);
        } /* if */
    } /* for */

    /* close the connection */
    status = NU_Close_Socket(socketd);
    if (status != NU_SUCCESS)
    {
        printf("Error from NU_Close_Socket.\n");
        DEMO_Exit(18);
    }

    /*  Indicate successful completion of the program.  */
    printf("Successful completion of the UDP Server program.\n");
    DEMO_Exit(0);
}

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      str_echo                                                         */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*     Read a stream socket one line at a time                           */
/*     and write each line back to the sender.                           */
/*     Return when the connection is terminated.                         */
/*                                                                       */
/*************************************************************************/
void    str_echo(UNSIGNED argc, VOID *argv)
{
  int       connected = 1;
  int       bytes_recv, bytes_sent;
  int       sockfd;
  STATUS    status;
  UNSIGNED  actSize;

  /*  Remove compilation warnings for unused parameters.  */
  status = (STATUS) argc + (STATUS) argv;


  status = NU_Receive_From_Queue(&socketQueue, (UNSIGNED *) &sockfd, 1,
                                 &actSize, NU_SUSPEND);
  if (status != NU_SUCCESS)
   {
       printf ("Unable to receive a message from socket Queue");
       DEMO_Exit(19);
   }

  /* Set the push bit for faster transmission. */
  NU_Push (sockfd);

  while(connected)
  {
       /* turn on the "block during a read" flag */
       NU_Fcntl(sockfd, NU_SETFLAG, NU_BLOCK);

       bytes_recv = NU_Recv(sockfd, line, ECHO_LENGTH, 0);

       /* turn off the "block during a read" flag -
          other reads may not want to block  */
       NU_Fcntl(sockfd, NU_SETFLAG, NU_FALSE);

       if (bytes_recv == NU_NOT_CONNECTED)
       {
           connected = 0;
           continue;
       }
       else if (bytes_recv < 0)
           printf("\nstr_echo: NU_Recv error\n");
       else
       {
           line[bytes_recv] = 0x0;
           printf("Received from the client: %s\n", line);
       }

       bytes_sent = NU_Send(sockfd, line, bytes_recv, 0);

       if(bytes_sent != bytes_recv)
           printf("\nstr_echo: NU_Send error\n");
  }

  /* close the connection */
  if ((NU_Close_Socket(sockfd)) != NU_SUCCESS)
  {
     printf("\nError from NU_Close_Socket.");
     DEMO_Exit(20);
  }


  /*  Indicate that all went well. */
  printf("\nSuccessful completion of the TCP Server program\n");

  DEMO_Exit(0);
}

/******************************************************************************/
/*                                                                            */
/* FUNCTION                                                                   */
/*                                                                            */
/*      DEMO_Get_Modem_String                                                 */
/*                                                                            */
/* DESCRIPTION                                                                */
/*                                                                            */
/*     This function is used to receive "modem" commands from Windows 95.     */
/*                                                                            */
/******************************************************************************/

CHAR *DEMO_Get_Modem_String(CHAR *response, CHAR *dev_name)
{
    CHAR  c;
    CHAR  *write_ptr;

    write_ptr = response;
    *write_ptr = NU_NULL;

    while (NU_TRUE)
    {
        /* get a character from the port if one's there */
        if (NU_Terminal_Data_Ready(dev_name))
        {
            NU_Get_Terminal_Char(&c, dev_name);

            switch (c)
            {
                case 0xD:                /* CR - return the result string */
                    if (*response)
                        return response;
                        continue;
                default:
                    if (c != 10)
                    {
                        /* add char to end of string */
                        *write_ptr++ = (char)c;
                        *write_ptr = NU_NULL;
                        /* ignore RINGING and the dial string */
                    }
            } /* switch (c) */
        } /* if NU_Terminal_Data_Ready */
        else
            NU_Sleep(5);
    } /* while (NU_TRUE) */

} /* DEMO_Get_Modem_String */

/* This function provides an infinite loop for the program to terminate in.
   The local variable a is an exit code which indicates where the program
   aborted.  */

void DEMO_Exit(INT exit_code)
{
    int b = exit_code;        /* This line avoids "unused parameter" warning */
    while(NU_TRUE)
        NU_Sleep (TICKS_PER_SECOND);
} /* DEMO_exit */

/* This function is stubbed out.  It may be filled in for debugging purposes */

void PRINTF(char* string, ...) { }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -