WIZnet Developer Forum

제가 TCP 통신을 하고 있는데 UDP 통신을 추가로 하고 싶습니다.

안녕하십니까?
TCP 통신과 UDP 통신을 선택해서 사용하고 싶습니다.

TCP 통신은 되는데 UDP 통신을 안됩니다.

어떻게 하면 되는지 부탁드립니다. 감사드립낟.

제가 사용하는 함수는 아래와 같습니다.

//////////////////////////////////////////////////////////////////////////////
u8 gu8_Tcp_Tx_Buf[DATA_BUF_SIZE]; // TX Buffer for applications

void loopback_tcpc(SOCKET s, uint16_t port)
{
	uint16_t RSR_len;
	uint16_t received_len;

   switch (getSn_SR(s))
   {
	   case SOCK_ESTABLISHED:                 /* if connection is established */
		      if(ch_status[s]==1)
		      {
		         //printf("\r\n%d : Server IP : %d.%d.%d.%d  , Port : %d  Connected",s,Destination_Setup.destip[0],Destination_Setup.destip[1],Destination_Setup.destip[2],Destination_Setup.destip[3],Destination_Setup.port);
		         ch_status[s] = 2;
		      }

			if ((RSR_len = getSn_RX_RSR(s)) > 0)         /* check Rx data */
			{
				if (RSR_len > DATA_BUF_SIZE) RSR_len = DATA_BUF_SIZE;   /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */
				                                                                /* the data size to read is MAX_BUF_SIZE. */
				gu8_Tcp_RxBuf_Length = recv(s, gu8_Tcp_Rx_Buf, RSR_len);         /* read the received data */
//				TCP_Parsing(s);
				gu8_SerialOrNetwork = 2;//Network Mode
				RxData_Parsing(gu8_Tcp_Rx_Buf,gu8_Tcp_RxBuf_Length);

			}
			break;
	   case SOCK_CLOSE_WAIT:                                 /* If the client request to close */
		      printf("\r\n%d : CLOSE_WAIT", s);
		      if ((RSR_len = getSn_RX_RSR(s)) > 0)         /* check Rx data */
		      {
		         if (RSR_len > DATA_BUF_SIZE) RSR_len = DATA_BUF_SIZE;   /* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */
		                                                                                    /* the data size to read is MAX_BUF_SIZE. */
		         received_len = recv(s, gu8_Tcp_Rx_Buf, RSR_len);         /* read the received data */
		      }
		      disconnect(s);
		      ch_status[s] = 0;
		      break;
	   case SOCK_CLOSED:                                               /* if a socket is closed */
		      if(!ch_status[s])
		      {
		         //printf("\r\n%d : Loop-Back TCP Client Started. port: %d", s, port);
		         ch_status[s] = 1;
		      }
		      if(socket(s, Sn_MR_TCP, Destination_Setup.port, 0x00) != s)    /* reinitialize the socket */
		      {
		         //printf("\a%d : Fail to create socket.",s);
		         ch_status[s] = 0;
		      }
		      break;
        case SOCK_INIT:     /* if a socket is initiated */
                if(sys_tick_cnt == 0)
                {
                        if(connect(s, Destination_Setup.destip, Destination_Setup.port) == 1) /* Try to connect to TCP server(Socket, DestIP, DestPort) */
		         { 
		         	//printf("\r\nconnect port: %d", port); 
		         }
                        sys_tick_cnt = 100;
                }
                break;
        default:
                break;
        }
}

안녕하세요

보내주신 코드에서 tcp 부분을 udp로 간단히 수정했습니다.
recvfrom에서 destip, destport에 데이터를 전송한 상대의 ip와 port가 저장돕니다.

u8 gu8_Udp_Tx_Buf[DATA_BUF_SIZE]; // TX Buffer for applications

void loopback_udps(SOCKET s, uint16_t port)
{
	uint16_t RSR_len;
	uint16_t received_len;

   switch (getSn_SR(s))
   {
	   case SOCK_UDP:             

			if ((RSR_len = getSn_RX_RSR(s)) > 0)         /* check Rx data */
			{
				if (RSR_len > DATA_BUF_SIZE) RSR_len = DATA_BUF_SIZE;   
				                                                              
				gu8_Udp_RxBuf_Length = recvfrom(s, gu8_Udp_Rx_Buf, RSR_len, destip, destport);      
				RxData_Parsing(gu8_Udp_Rx_Buf,gu8_Udp_RxBuf_Length);

			}
			break;

	   case SOCK_CLOSED:                                               /* if a socket is closed */

		      if(socket(s, Sn_MR_UDP, Destination_Setup.port, 0x00) != s)    /* reinitialize the socket */
		      {

		      }
		      break;
        default:
                break;
        }
}

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.