我是Microchip的TCP / IP堆栈的新手,我已经在Harmony文件夹中的一些示例的帮助下成功创建了我自己的基本TCP服务器。我想扩展我的TCP服务器以满足同一端口上的多个客户端连接,但我不完全确定最好的方法。
以下是我的基本服务器应用程序的信息。
void TCP_SERVER_Tasks ( void )
{
//Handle for the network interface
static TCPIP_NET_HANDLE networkInterfaceHandle;
//IP Address assigned to DHCP Client
static IPV4_ADDR hostIPAddress;
//Handle for TCP Client
static TCP_SOCKET TCPSocketHandle = INVALID_SOCKET;
//System Timer Tick Values
static uint32_t statusRequestTick;
static uint32_t timeoutTick;
//Temporary Buffer for Storage of received UDP/TCP Data
uint8_t receivedDataBuffer[128];
uint16_t numberDataBytesToProcess;
//Check if the network interface has become disconnected.
//Close any open sockets, and goto NETWORKCOMS_STATE_INIT state
if (tcp_serverData.state > NETWORKCOMS_STATE_WAIT_FOR_LINK &&
TCPIP_STACK_NetIsLinked(networkInterfaceHandle) == false)
{
if (TCPSocketHandle != INVALID_SOCKET)
{
//Close the Open TCP Socket
TCPIP_TCP_Close(TCPSocketHandle);
TCPSocketHandle = INVALID_SOCKET;
}
tcp_serverData.state = NETWORKCOMS_STATE_WAIT_FOR_LINK;
}
/* Check the application's current state. */
switch ( tcp_serverData.state )
{
/* Application's initial state. */
case TCP_SERVER_STATE_INIT:
{
//Wait for the TCPIP Stack and System Timer Service to become initialized
if (TCPIP_STACK_Status(sysObj.tcpip) == SYS_STATUS_READY &&
SYS_TMR_Status(sysObj.sysTmr) == SYS_STATUS_READY)
{
if (TCPIP_STACK_NumberOfNetworksGet() > 0)
{
//Get a handle to the network interface
//For this application only one interface is used
networkInterfaceHandle = TCPIP_STACK_IndexToNet(0);
}
timeoutTick = SYS_TMR_TickCountGet();
tcp_serverData.state = NETWORKCOMS_STATE_WAIT_FOR_LINK;
}
break;
}
case NETWORKCOMS_STATE_WAIT_FOR_LINK:
{
//Determine if the network interface is connected
if (TCPIP_STACK_NetIsLinked(networkInterfaceHandle))
{
//Check if a DHCP Server can be found on the network
if (TCPIP_DHCP_IsBound(networkInterfaceHandle))
{
hostIPAddress.Val = TCPIP_STACK_NetAddress(networkInterfaceHandle);
tcp_serverData.state = NETWORKCOMS_SETUP_TCP_SERVER;
}
}
break;
}
case NETWORKCOMS_SETUP_TCP_SERVER:
{
//Setup the TCP Server on the port defined by the TCP_SERVER_PORT constant.
//Store the returned socket handle in the TCPSocketHandle variable.
TCPSocketHandle = TCPIP_TCP_ServerOpen(IP_ADDRESS_TYPE_IPV4, TCP_SERVER_PORT, 0);
// Verify that the socket opened correctly
if (TCPSocketHandle != INVALID_SOCKET )
{
tcp_serverData.state = NETWORK_WAIT_FOR_CONNECTION;
}
break;
}
case NETWORK_WAIT_FOR_CONNECTION:
{
// Periodically check to see if a Client has connected
if( TCPIP_TCP_IsConnected(TCPSocketHandle) )
{
tcp_serverData.state = NETWORK_SERVICE_CONNECTION;
}
break;
}
case NETWORK_SERVICE_CONNECTION:
{
// Make sure that the connection is still available
if ( !TCPIP_TCP_IsConnected(TCPSocketHandle) )
{
tcp_serverData.state = NETWORK_WAIT_FOR_CONNECTION;
}
else
{
if ( TCPIP_TCP_PutIsReady(TCPSocketHandle) > 10 )
{
TCPIP_TCP_StringPut(TCPSocketHandle, "HELLO!!\r\n\0");
}
tcp_serverData.state = NETWORK_CLOSE_CONNECTION;
}
break;
}
case NETWORK_CLOSE_CONNECTION:
{
TCPIP_TCP_Close(TCPSocketHandle);
tcp_serverData.state = NETWORKCOMS_SETUP_TCP_SERVER;
break;
}
/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}
`
现在如果端口上有连接,代码会以HELLO响应!然后关闭连接。
在我开始更改代码以服务多个客户端之前,我需要一些澄清。在开始接受多个客户端之前,我是否需要将服务器套接字绑定到本地地址?为什么?
我是否可以将位于Harmony文件夹中的Berkley_Server示例用作如何构建具有多个客户端访问权限的常规TCP服务器的通用模板?
非常感谢任何澄清或见解。