📄 udpserv.c
字号:
sprintf (mstring, "CONNECT %d\n\r", devices[0].dv_hw.uart.baud_rate);
NU_Modem_Control_String(mstring, devices[0].dv_name);
/* Get a pointer to the device structure. Only used in null modem 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(8);
}
/**** 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_DGRAM, 0))<0)
{
printf("Could not open a connection via NU_Socket.\n");
DEMO_Exit(9);
}
status = NU_Allocate_Memory (&System_Memory, (void *) &return_ptr,
sizeof(struct addr_struct), NU_SUSPEND);
if (status != NU_SUCCESS)
{
printf("Could not allocate memory for servaddr.\n");
DEMO_Exit(10);
}
servaddr = (struct addr_struct *)return_ptr;
/* fill in a structure with the server address */
servaddr->family = NU_FAMILY_IP;
servaddr->port = 7;
servaddr->id.is_ip_addrs[0] = serv_ip_addr[0];
servaddr->id.is_ip_addrs[1] = serv_ip_addr[1];
servaddr->id.is_ip_addrs[2] = serv_ip_addr[2];
servaddr->id.is_ip_addrs[3] = serv_ip_addr[3];
servaddr->name = "server_name";
status = NU_Allocate_Memory (&System_Memory, (void *) &return_ptr,
sizeof(struct addr_struct), NU_SUSPEND);
if (status != NU_SUCCESS)
{
printf("Coult not allocate memory for cliaddr\n");
DEMO_Exit(11);
}
cliaddr = (struct addr_struct *)return_ptr;
/* initialize client address */
cliaddr->family = NU_FAMILY_IP;
cliaddr->port = 7;
cliaddr->id.is_ip_addrs[0] = cli_ip_addr[0];
cliaddr->id.is_ip_addrs[1] = cli_ip_addr[1];
cliaddr->id.is_ip_addrs[2] = cli_ip_addr[2];
cliaddr->id.is_ip_addrs[3] = cli_ip_addr[3];
cliaddr->name = "client_name";
/* Bind our address to the socket. */
status = NU_Bind(socketd, servaddr, 0);
if (status != NU_SUCCESS)
{
printf("NU_Bind failed.\n");
DEMO_Exit(12);
}
/* Prime the loop. */
buffer[0] = (char) 0;
while(buffer[0] != 'q')
{
/* Get a string from the client. */
bytes_received = NU_Recv_From(socketd, buffer, 1000, 0, cliaddr,
&clilen);
/* If we got back less than zero there is a bad error. */
if (bytes_received < 0)
{
printf("Error trying to receive from UDP client.\n");
DEMO_Exit(13);
}
else
{
printf("Received %d bytes.\n", bytes_received);
printf("Received message: %s\n", buffer);
}
/* NULL terminate the input string. */
buffer[bytes_received] = (char) 0;
/* Send the string back to the client. */
bytes_sent = NU_Send_To(socketd, buffer, strlen(buffer), 0,
cliaddr, clilen);
/* If we got sent less than zero there is a bad error. */
if (bytes_sent < 0)
{
printf("Error trying to send to UDP client.\n");
DEMO_Exit(14);
}
} /* while */
/* close the connection */
status = NU_Close_Socket(socketd);
if (status != NU_SUCCESS)
{
printf("Error from NU_Close_Socket.\n");
DEMO_Exit(15);
}
/* Indicate successful completion of the program. */
printf("Successful completion of the UDP 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 + -