---
title: "제가 TCP 통신을 하고 있는데 UDP 통신을 추가로 하고 싶습니다."
url: "https://maker.wiznet.io/forum/14112"
markdown_url: "https://maker.wiznet.io/forum/14112/md"
type: "Forum topic"
category: "Ethernet Chips"
author: "yeol2210"
created: "2021-01-08T07:49:36+09:00"
last_activity: "2021-01-08T08:14:25+09:00"
language: "en"
views: 588
replies: 1
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

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

## Question

Asked by yeol2210 on 2021-01-08 in Ethernet Chips.

안녕하십니까?
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;
        }
}
```

## Replies

### Reply 1 by becky, 2021-01-08

안녕하세요

보내주신 코드에서 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;
        }
}
```

---

Source: https://maker.wiznet.io/forum/14112
