📄 tcpserv.c
字号:
}
devices[0].dv_name = "PPP_0";
devices[0].dv_init = PPP_Initialize;
devices[0].dv_flags = (DV_POINTTOPOINT | DV_NOARP);
devices[0].dv_hw.uart.com_port = SMC1;
devices[0].dv_hw.uart.baud_rate = 115200;
memcpy (devices[0].dv_ip_addr, null_ip_addr, 4); /* Not use by PPP. */
memcpy (devices[0].dv_subnet_mask, subnet, 4); /* Not use by PPP. */
if (NU_Init_Devices(devices, 1) != NU_SUCCESS)
{
printf("NU_Init_Devices failed.\n");
DEMO_Exit(14);
}
/* open a connection via the socket interface */
if ((socketd = NU_Socket(NU_FAMILY_IP, NU_TYPE_STREAM, 0)) < 0 )
{
printf("Could not open socket via NU_Socket.\n");
DEMO_Exit(15);
}
status = NU_Allocate_Memory(&System_Memory, &pointer,
sizeof(struct addr_struct), NU_SUSPEND);
if (status != NU_SUCCESS)
{
printf("Could not allocate memory for seraddr.\n");
DEMO_Exit(16);
}
servaddr = (struct addr_struct *)pointer;
/* fill in a structure with the server address */
servaddr->family = NU_FAMILY_IP;
servaddr->port = 7;
memcpy(&servaddr->id, null_ip_addr, 4);
servaddr->name = "server_name";
/* 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(17);
}
/* be ready to accept connection requests */
status = NU_Listen(socketd, 10);
if (status != NU_SUCCESS)
{
printf("NU_Listen failed.\n");
DEMO_Exit(18);
}
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("Client has connected.\n ");
/* 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 */
/* close the connection */
status = NU_Close_Socket(socketd);
if (status != NU_SUCCESS)
{
printf("Error from NU_Close_Socket.\n");
DEMO_Exit(19);
}
/* 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(20);
}
/* 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("str_echo: NU_Recv error\n");
DEMO_Exit(21);
}
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("str_echo: NU_Send error\n");
DEMO_Exit(22);
}
}
/* close the connection */
if ((NU_Close_Socket(sockfd)) != NU_SUCCESS)
{
printf("Error from NU_Close_Socket.\n");
DEMO_Exit(23);
}
/* Indicate that all went well. */
printf("Successful completion of the TCP Server program\n");
DEMO_Exit(0);
}
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* Controller */
/* */
/* DESCRIPTION */
/* */
/* This function waits for a caller. When a connection is made it */
/* waits until the connection is lost and then waits for another */
/* caller. */
/* */
/*************************************************************************/
void Controller (UNSIGNED argc, VOID *argv)
{
/* Modem Initialization string. */
NU_Modem_Control_String("ATm0S0=0V1X4&K0^M", devices[0].dv_name);
NU_Set_PPP_Client_IP_Address (cli_ip_addr, devices[0].dv_name);
while (NU_TRUE)
{
/* Wait for slow modem */
NU_Sleep (2);
/* Wait for a client to call. */
NU_Wait_For_PPP_Client(serv_ip_addr, devices[0].dv_name);
/* Wait until the session is ended before we go on */
while (NU_PPP_Still_Connected(devices[0].dv_name))
NU_Relinquish();
}
}
/* 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);
}
/* 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 + -