📄 newservers.c
字号:
//--------------------------------------------------------------------------
// IP Stack Server Demonstration Program
//--------------------------------------------------------------------------
// newservers.c
//
// This module demonstrates the use of the server daemon added in NDK 1.7.
//
// It provides all the functionality of:
// echosrv() : echosrv.c
// datasrv() : datasrv.c
// nullsrv() : nullsrv.c
// oobsrv() : oobsrv.c
//
// The original source files are still provided to illustrate a traditional
// server. This file contains only the daemon service task functions
// required when using the server daemon.
//
// Author: Michael A. Denio
// Copyright 2003 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <netmain.h>
//
// dtask_tcp_echo() - TCP Echo Server Daemon Function (SOCK_STREAMNC)
// (SOCK_STREAMNC, port 7)
//
// Returns "1" if socket 's' is still open, and "0" if its been closed
//
int dtask_tcp_echo( SOCKET s, UINT32 unused )
{
struct timeval to;
int i;
char *pBuf;
HANDLE hBuffer;
(void)unused;
// Configure our socket timeout to be 5 seconds
to.tv_sec = 5;
to.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
i = 1;
setsockopt( s, IPPROTO_TCP, TCP_NOPUSH, &i, 4 );
for(;;)
{
i = (int)recvnc( s, (void **)&pBuf, 0, &hBuffer );
// If we read data, echo it back
if( i > 0 )
{
if( send( s, pBuf, i, 0 ) < 0 )
break;
recvncfree( hBuffer );
}
// If the connection got an error or disconnect, close
else
break;
}
fdClose( s );
// Return "0" since we closed the socket
return(0);
}
//
// dtask_udp_echo() - UDP Echo Server Daemon Function
// (SOCK_DGRAM, port 7)
//
// Returns "1" if socket 's' is still open, and "0" if its been closed
//
int dtask_udp_echo( SOCKET s, UINT32 unused )
{
struct sockaddr_in sin1;
struct timeval to;
int i,tmp;
char *pBuf;
HANDLE hBuffer;
(void)unused;
// Configure our socket timeout to be 3 seconds
to.tv_sec = 3;
to.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
for(;;)
{
tmp = sizeof( sin1 );
i = (int)recvncfrom( s, (void **)&pBuf, 0, &sin1, &tmp, &hBuffer );
// Spit any data back out
if( i >= 0 )
{
sendto( s, pBuf, i, 0, &sin1, sizeof(sin1) );
recvncfree( hBuffer );
}
else
break;
}
// Since the socket is still open, return "1"
// (we need to leave UDP sockets open)
return(1);
}
//
// dtask_tcp_datasrv() - TCP Data Server Daemon Function
// (SOCK_STREAM, port 1000)
//
// Returns "1" if socket 's' is still open, and "0" if its been closed
//
int dtask_tcp_datasrv( SOCKET s, UINT32 unused )
{
struct timeval to;
int i,size;
(void)unused;
// Configure our socket timeout to be 5 seconds
to.tv_sec = 5;
to.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
i = 1;
setsockopt( s, IPPROTO_TCP, TCP_NOPUSH, &i, 4 );
for(;;)
{
// There is data available on the active connection
i = (int)recv( s, (char *)&size, sizeof(int), 0 );
if( i==sizeof(int) )
{
if( send( s, (char *)&size, size, 0 ) < 0 )
break;
}
else
break;
}
// Note in dtask_tcp_echo() we close the socket at this
// point. Here we'll leave it open to test the daemon.
// Return "1" since the socket is still open
return(1);
}
//
// dtask_tcp_nullsrv() - TCP Data Server Daemon Function
// (SOCK_STREAMNC, port 1001)
//
// Returns "1" if socket 's' is still open, and "0" if its been closed
//
int dtask_tcp_nullsrv( SOCKET s, UINT32 unused )
{
struct timeval to;
int i;
char *pBuf;
HANDLE hBuffer;
(void)unused;
// Configure our socket timeout to be 5 seconds
to.tv_sec = 5;
to.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
for(;;)
{
// There is data available on the active connection
i = (int)recvnc( s, (void **)&pBuf, 0, &hBuffer );
// If the connection is closed or got an error, close
if( i <= 0 )
break;
else
recvncfree( hBuffer );
}
// Note in dtask_tcp_echo() we close the socket at this
// point. Here we'll leave it open to test the daemon.
// Return "1" since the socket is still open
return(1);
}
//
// dtask_tcp_oobsrv() - TCP Data Server Daemon Function
// (SOCK_STREAMNC. port 999)
//
// Returns "1" if socket 's' is still open, and "0" if its been closed
//
int dtask_tcp_oobsrv( SOCKET s, UINT32 unused )
{
struct timeval to;
int i;
char buf[16];
(void)unused;
// Configure our socket timeout to be 5 seconds
to.tv_sec = 5;
to.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
//
// OOB Data Test
// Will send 10 bytes of data, 1 OOB, and then 10 more data
// Client shoud read as
// 10 bytes (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
// 1 byte (99)
// 10 bytes (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
//
for(i=0; i<10; i++)
buf[i] = i;
buf[10] = 99;
send( s, buf, 11, MSG_OOB );
send( s, buf, 10, 0 );
fdClose( s );
// Return "0" since we closed the socket
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -