---
title: "could not able to open socket"
url: "https://maker.wiznet.io/forum/13625"
markdown_url: "https://maker.wiznet.io/forum/13625/md"
type: "Forum topic"
category: "Ethernet Chips"
author: "Yogesh"
created: "2020-01-02T10:23:49+09:00"
last_activity: "2020-01-22T01:19:50+09:00"
language: "en"
views: 2185
replies: 5
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

# could not able to open socket

## Question

Asked by Yogesh on 2020-01-02 in Ethernet Chips.

Hi,

I am using stm32f105vct MCU and w5500 to establish ethernet communication using UDP protocol.
I am able to communicate b/w MCU and W5500 ic using SPI but when i tried to initialise socket for udp protocol , socket is always closed.I tried with all the sockets 0 to 8 but same problem.
I am getting PHYCFGR register value as 0xBB.
I have pasted my program below. Please help me to fine out the probelm .

Program:

uint8_t gate_way={0,0,0,0},sub_net={255,255,255,0},my_ip={192,168,0,6},dest_ip={192,168,0,2};
uint8_t udp_packets={10,20,30,40,50,60,70,80,90,100},read_data=0;
uint16_t prt=5000,dprt=5000;

int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* Configure the system clock */
SystemClock_Config();

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI2_Init();
MX_UART4_Init();

//***UDP PROTOCOL//
//Initialization

setSn_CR(0, Sn_CR_CLOSE);
setSn_CR(1, Sn_CR_CLOSE);
setSn_CR(2, Sn_CR_CLOSE);
setSn_CR(3, Sn_CR_CLOSE);
setSn_CR(4, Sn_CR_CLOSE);
setSn_CR(5, Sn_CR_CLOSE);
setSn_CR(6, Sn_CR_CLOSE);
setSn_CR(7, Sn_CR_CLOSE);

setGAR(gate_way);
setSUBR(sub_net);
setSIPR(my_ip);

setMR(MR_RST);
HAL_Delay(1000);

START:
// OPEN Socket4 Mode UDP MYPORT 5000
setMR(Sn_MR_UDP);
setSn_PORT(4, prt);
setSn_CR(4, Sn_CR_OPEN);

read_data= getSn_SR(4);

if(read_data !=SOCK_UDP)
{
setSn_CR(4, Sn_CR_CLOSE);
goto START;
}
//SET DESTIP 192.168.0.2 DESTPORT 7760
setSn_DIPR(4, dest_ip);
setSn_DPORT(4, dprt);

while (1)
{
wiz_send_data(4,udp_packets,sizeof(udp_packets));
setSn_CR(4, Sn_CR_SEND);
HAL_Delay(1000);
}

}

Thanks in Advance

## Replies

### Reply 1 by Mxyxixptlick, 2020-01-02

![](https://avatars.discourse-cdn.com/v4/letter/y/49beb7/48.png) Yogesh:

> setMR(MR_RST);

Try configuring your IP address, gateway & subnet AFTER resetting the chip rather than before. Sending the reset command (setMR(MR_RST)) resets all registers to default values, undoing your configuration.

### Reply 2 by Yogesh, 2020-01-03

Thank You for your reply,

I have corrected my code as per your suggestion but still i am facing the same problem.
I would like to inform you that I am using w5500.h and w5500.c from standard IO library prescribed by [wiznet.io](http://wiznet.io),([GitHub - Wiznet/ioLibrary_Driver: ioLibrary_Driver can be used for the application design of WIZnet TCP/IP chips as W5500, W5300, W5200, W5100 W5100S.](https://github.com/Wiznet/ioLibrary_Driver)).

### Reply 3 by Yogesh, 2020-01-20

[@midnightcow](https://maker.wiznet.io/u/midnightcow),

Thanks for the response.

I have used sendto() func as you said but still it is not working. please find my code below.

```
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"
#include "wizchip_conf.h"
#include "socket.h"
#include <string.h>

#define GREETING_MSG 		 "Well done guys! Welcome to the IoT world. Bye!\r\n"


SPI_HandleTypeDef 	hspi2;
UART_HandleTypeDef  huart2;
char msg[60];
uint8_t dest_ip[]={192,168,0,6};
uint16_t dest_port=5000;
void SystemClock_Config(void);

void cs_sel() {
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); //CS LOW
}

void cs_desel() {
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); //CS HIGH
}

uint8_t spi_rb(void) {
	uint8_t rbuf;
	HAL_SPI_Receive(&hspi2, &rbuf, 1,1);
	return rbuf;
}

void spi_wb(uint8_t b) {
	HAL_SPI_Transmit(&hspi2, &b, 1, 1);
}
int main(void)
{
  
	uint8_t retVal, sockStatus;
	int16_t rcvLen;
	uint8_t rcvBuf[20], bufSize[] = {2, 2, 2, 2};

 
  HAL_Init();
  SystemClock_Config();

  
  MX_GPIO_Init();
  MX_SPI2_Init();
  MX_USART2_UART_Init();
 

   reg_wizchip_cs_cbfunc(cs_sel, cs_desel);
   reg_wizchip_spi_cbfunc(spi_rb, spi_wb);

   wizchip_init(bufSize, bufSize);
   wiz_NetInfo netInfo = { .mac 	= {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef},	// Mac address
                           .ip 	= {192, 168, 0, 2},					// IP address
                           .sn 	= {255, 255, 255, 0},					// Subnet mask
                           .gw 	= {192, 168, 2, 1}};					// Gateway address
   wizchip_setnetinfo(&netInfo);
   wizchip_getnetinfo(&netInfo);
  

 reconnect:
  
   if((retVal = socket(1, Sn_MR_UDP, 5000, 0)) ==1) 
   {
 	  while(1) 
	  {
 			  
 			  if(sockStatus = getSn_SR(1) == SOCK_UDP) 
			  {
			  /* Let's send a welcome message and closing socket */
 				  if(retVal = sendto(1, GREETING_MSG, strlen(GREETING_MSG),dest_ip,dest_port) == (int16_t)strlen(GREETING_MSG))
 					  PRINT_STR(SENT_MESSAGE_MSG);
 				  else {
 					  snprintf(msg, WRONG_RETVAL_MSG, retVal);
 					  PRINT_STR(msg);
 				  }
 				  break;
 			  }
 			  else { 
 				  snprintf(msg, WRONG_STATUS_MSG, sockStatus);
 				  PRINT_STR(msg);
 				  break;
 			  }
 			
 	}

   } else { 
 	  snprintf(msg, WRONG_RETVAL_MSG, retVal);
 	  PRINT_STR(msg);
   }

   /* We close the socket and start a connection again */
   
   close(1);
   goto reconnect;
 
  
  
}
```

### Reply 4 by Yogesh, 2020-01-21

[@midnightcow](https://maker.wiznet.io/u/midnightcow)
Thanks for the reply.
I am using Nucleo STM32F103RB development board and w5100 Ethernet shield .
STMcubeMX & Atollic true studio for coding ,also I am using wireshark tool to monitor the data.
Hardware configuration in Nucleo board.
1.Peripheral: SPI2
2.Pins : PB13,PB14,PB15 & PB12
3.Baud_rate: 18MBits/sec.

The code which i have got from following link is modified for my board and it is working for TCP, but the same code is modified for UDP it is not working which i already shared with you.
Please find the link below.

[github.com](https://github.com/cnoviello/stm32-nucleof4/blob/master/stm32-nucleof4-ethshield1/src/main.c)

#### [cnoviello/stm32-nucleof4/blob/master/stm32-nucleof4-ethshield1/src/main.c](https://github.com/cnoviello/stm32-nucleof4/blob/master/stm32-nucleof4-ethshield1/src/main.c)

```
/**
  ******************************************************************************
  * @file    Templates/Src/main.c 
  * @author  MCD Application Team
  * @version V1.2.0
  * @date    26-December-2014
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
```

This file has been truncated. [show original](https://github.com/cnoviello/stm32-nucleof4/blob/master/stm32-nucleof4-ethshield1/src/main.c)

### Reply 5 by Yogesh, 2020-01-21

I have shared data packets captured from wireshark tool, also i changed Gateway IP value but still not working.

[![image](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/9/916752fd3e11bf0e366b6539ca016d408f3af021.png) image1675×795 54.9 KB](https://maker.wiznet.io/forum_uploads/wiznet/original/2X/9/916752fd3e11bf0e366b6539ca016d408f3af021.png)

Thank you,

---

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