123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /********************************************************************************
- * File Name: TcpEchoServer.c
- * Function Describe:Echo Server by TCP
- * Relate Module:
- * Explain:
- * Writer: slw
- * Date:
- *******************************************************************************/
- #include "includes.h"
- #include "socket.h"
- /**
- * @brief Loopback Test Example Code using ioLibrary_BSD
- * @retval None
- */
- int32_t loopback_tcps(uint8_t sn, uint8_t* buf,uint16_t buflen, uint16_t port)
- {
- char tacebuf[100];
- int32_t ret;
- uint16_t size = 0, sentsize=0;
- switch(getSn_SR(sn))
- {
- case SOCK_ESTABLISHED :
- if(getSn_IR(sn) & Sn_IR_CON)
- {
- snprintf(tacebuf,sizeof(tacebuf),"%d:Connected\r\n",sn);
- SlwTrace(DEBUG,tacebuf);
- setSn_IR(sn,Sn_IR_CON);
- }
- if((size = getSn_RX_RSR(sn)) > 0)
- {
- if(size > buflen) size = buflen;
- ret = recv(sn,buf,size);
- if(ret <= 0) return ret;
- sentsize = 0;
- while(size != sentsize)
- {
- ret = send(sn,buf+sentsize,size-sentsize);
- if(ret < 0)
- {
- close(sn);
- return ret;
- }
- sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
- }
- }
- break;
- case SOCK_CLOSE_WAIT :
- snprintf(tacebuf,sizeof(tacebuf),"%d:CloseWait\r\n",sn);
- SlwTrace(DEBUG,tacebuf);
- if((ret=disconnect(sn)) != SOCK_OK) return ret;
- snprintf(tacebuf,sizeof(tacebuf),"%d:Closed\r\n",sn);
- SlwTrace(DEBUG,tacebuf);
- break;
- case SOCK_INIT :
- snprintf(tacebuf,sizeof(tacebuf),"%d:Listen, port [%d]\r\n",sn, port);
- SlwTrace(DEBUG,tacebuf);
- if( (ret = listen(sn)) != SOCK_OK) return ret;
- break;
- case SOCK_CLOSED:
- snprintf(tacebuf,sizeof(tacebuf),"%d:LBTStart\r\n",sn);
- SlwTrace(DEBUG,tacebuf);
- if((ret=socket(sn,Sn_MR_TCP,port,0x00)) != sn)
- return ret;
- snprintf(tacebuf,sizeof(tacebuf),"%d:Opened\r\n",sn);
- SlwTrace(DEBUG,tacebuf);
- break;
- default:
- break;
- }
- return 1;
- }
|