tcpserv.c
来自「基于nucleus操作系统的GPRS无线数据传输终端全套源文件。包括支持ARM7」· C语言 代码 · 共 440 行 · 第 1/2 页
C
440 行
/* Receive a MODEM command. */
DEMO_Get_Modem_String(mstring, devices[0].dv_name);
/* Respond with OK. */
NU_Modem_Control_String("OK\n\r", devices[0].dv_name);
/* If the command received was a command to dial then Windows 95 now
thinks that it has a modem connection to a remote HOST. Get out of
this loop because data exchanged beyond this point will be in the
form of IP packets. */
if (strncmp(mstring, "ATDT", 4) == 0)
break;
}
/* Act like a modem. */
sprintf (mstring, "CONNECT %ld\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 (client_ip_address, 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(server_ip_address, dev_ptr);
if (status == NU_SUCCESS)
{
/**** 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 ("PPP_Link", server_ip_address, (UINT8 *)subnet_mask);
/* Add a route to this new node on the network. */
RTAB_Add_Route (dev_ptr, *(UINT32 *)client_ip_address,
*(UINT32 *)subnet_mask, *server_ip_address,
(RT_UP | RT_HOST));
/**** End of null modem specific code. ****/
/* Create the socket on which the server will listen for client
connections.
*/
if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) >=0 )
{
/* Fill in a structure with the server address. */
servaddr.family = NU_FAMILY_IP;
servaddr.port = 7;
*((UINT32 *)servaddr.id.is_ip_addrs) = IP_ADDR_ANY;
/* make an NU_Bind() call to bind the server's address */
if ((NU_Bind(socketd, &servaddr, 0))>=0)
{
/* be ready to accept connection requests */
status = NU_Listen(socketd, 10);
if (status == NU_SUCCESS)
{
/* Listen for up to 2 connections. */
for (i=0; i < 2; i++)
{
/* Accept a connection. This service blocks by default until
a client has connected.
*/
newsock = NU_Accept(socketd, &client_addr, 0);
/* If a client has connected then send the socket descriptor
to one of the echo tasks. */
if (newsock >= 0)
{
/* process the new connection */
status = NU_Send_To_Queue(&socketQueue,
(UNSIGNED *)&newsock,
1, NU_SUSPEND);
NU_Sleep(2);
} /* end successful NU_Accept */
} /* end for loop */
} /* end successful NU_Listen */
} /* end successful NU_Bind */
} /* end successful NU_Socket */
} /* end successful connection */
NU_Suspend_Task(NU_Current_Task_Pointer());
}
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* str_echo */
/* */
/* DESCRIPTION */
/* */
/* This function waits for the server task to send it a socket */
/* descriptor via a queue. It then communicates over the socket */
/* with the client. This task first waits for data from the client. */
/* It then echos the received data back to the client. */
/* */
/*************************************************************************/
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)
{
Error_Loop (11);
}
/* Turn on the "block during a read" flag. NU_Receive is non-blocking by
default. This will cause the receive call to block until data is
received.
*/
NU_Fcntl(sockfd, NU_SETFLAG, NU_BLOCK);
while(connected)
{
/* Receive some data. */
bytes_recv = NU_Recv(sockfd, line, ECHO_LENGTH, 0);
/* Check to see if the client closed his end of the connection. If so
then we need to close this end as well. */
if (bytes_recv < 0)
{
connected = 0;
continue;
}
else
/* Echo the data back to the client. */
bytes_sent = NU_Send(sockfd, line, bytes_recv, 0);
}
/* close the connection */
NU_Close_Socket(sockfd);
}
/******************************************************************************/
/* */
/* 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 (1)
{
/* 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 */
}
}
}
else
NU_Sleep(5);
}
} /* DEMO_Get_Modem_String */
VOID Error_Loop (INT error_num)
{
INT x, y;
while (NU_TRUE)
{
x++;
y++;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?