WIZnet Developer Forum

Tcp cinnection problem

I am going trying to implement a simple tcp server. I am using the library socket.c and socket.h. i followed the tutorial infact the client succeses to connect with me. But the Sn_SR does not go in the connect_extabilished.

Your text is too short. I can’t judge the problem by looking at your writing. I hope you see loopback_tcps() in loopback.c and refer to the process for extabilishing.

int32_t loopback_tcps(uint8_t sn, uint8_t* buf, uint16_t port)
{
   int32_t ret;
   uint16_t size = 0, sentsize=0;

#ifdef _LOOPBACK_DEBUG_
   uint8_t destip[4];
   uint16_t destport;
#endif

   switch(getSn_SR(sn))
   {
      case SOCK_ESTABLISHED :
         if(getSn_IR(sn) & Sn_IR_CON)
         {
#ifdef _LOOPBACK_DEBUG_
			getSn_DIPR(sn, destip);
			destport = getSn_DPORT(sn);

			printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
			setSn_IR(sn,Sn_IR_CON);
         }
		 if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
         {
			if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
			ret = recv(sn, buf, size);

			if(ret <= 0) return ret;      // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
			size = (uint16_t) 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 :
#ifdef _LOOPBACK_DEBUG_
         //printf("%d:CloseWait\r\n",sn);
#endif
         if((ret = disconnect(sn)) != SOCK_OK) return ret;
#ifdef _LOOPBACK_DEBUG_
         printf("%d:Socket Closed\r\n", sn);
#endif
         break;
      case SOCK_INIT :
#ifdef _LOOPBACK_DEBUG_
    	 printf("%d:Listen, TCP server loopback, port [%d]\r\n", sn, port);
#endif
         if( (ret = listen(sn)) != SOCK_OK) return ret;
         break;
      case SOCK_CLOSED:
#ifdef _LOOPBACK_DEBUG_
         //printf("%d:TCP server loopback start\r\n",sn);
#endif
         if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
#ifdef _LOOPBACK_DEBUG_
         //printf("%d:Socket opened\r\n",sn);
#endif
         break;
      default:
         break;
   }
   return 1;
}

when debug, everything is good and working. but If I shut down the computer then I cannot connect to the processor again. When you enter the ethernet settings from the control panel and turn the ethernet off and on, it works normally again. how to solve this error. can you help me. my mcu is working tcp server mode. ı am using tcp comunication.

ı am using stm32f407vg .w5500 module .

my code

/* Open socket 0 as TCP_SOCKET with port 5000 /

if((retVal = socket(0, Sn_MR_TCP, 80, SF_TCP_NODELAY)) == 0) {

/ Put socket in LISTEN mode. This means we are creating a TCP server /

if((retVal = listen(0)) == SOCK_OK) {

/ While socket is in LISTEN mode we wait for a remote connection */

while(sockStatus = getSn_SR(0) == SOCK_LISTEN)

delay(600);

/ OK. Got a remote peer. Let’s send a message to it /

while (1) {

/ If connection is ESTABLISHED with remote peer /

if (sockStatus = getSn_SR(0) == SOCK_ESTABLISHED) {

/ Retrieving remote peer IP and port number /

getsockopt(0, SO_DESTIP, remoteIP);

getsockopt(0, SO_DESTPORT, ( uint8_t *)&remotePort);

sprintf(msg, CONN_ESTABLISHED_MSG, remoteIP[0], remoteIP[1], remoteIP[2], remoteIP[3], remotePort);

PRINT_STR(msg);

while (received_len==0)

{

if ((RSR_len = getSn_RX_RSR(0) )> 0)

{

received_len = recv(0, data_buf, RSR_len);

if (data_buf[0] == 1)

{

HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);

sprintf(msg,“adc degeri %d”,adc_ntc[0]);

send(0, msg, strlen(msg));

}

else

{

sprintf(msg,“adc degeri %d”,adc_ntc[0]);

send(0, msg, strlen(msg));

HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);

}

}

}

received_len =0;

/ Let’s send a welcome message and closing socket /

if (retVal = send(0, GREETING_MSG, strlen(GREETING_MSG)) == ( int16_t )strlen(GREETING_MSG))

PRINT_STR(SENT_MESSAGE_MSG);

else { / Ops: something went wrong during data transfer /

sprintf(msg, WRONG_RETVAL_MSG, retVal);

PRINT_STR(msg);

}

break ;

}

else { / Something went wrong with remote peer, maybe the connection was closed unexpectedly /

sprintf(msg, WRONG_STATUS_MSG, sockStatus);

PRINT_STR(msg);

break ;

}

}

} else / Ops: socket not in LISTEN mode. Something went wrong /

PRINT_STR(LISTEN_ERR_MSG);

} else { / Can’t open the socket. This means something is wrong with W5100 configuration: maybe SPI issue? /

sprintf(msg, WRONG_RETVAL_MSG, retVal);

PRINT_STR(msg);

}

/ We close the socket and start a connection again /

// disconnect(0);

close(0);

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