socket state = SOCKET_ESTABLISHED


#1

hi, dear developers!
i make some device with HTTP - server with W5500.
when i connect with telnet to my Server - that’s all OK.
but when i connect from browser - i have some trouble.
my server send some data, and then close connection.
and in the next time browser open new connection, and don’t send
any data… it’s bad for me because i have only 7 sockets.

thats some trasing data:

###HTTP_process
---socket:
00000
---Size
00429 - this counter for bytes RX from browser
--- no Content Length
login.php

login page
write...
write end.
HTTP/1.1 200 OK
Content-Type: text/html; charset=windows-1251
Connection: close
Server: aHata

<http><body>▒▒▒▒▒▒▒▒▒▒:<form action="login.php" method="                                  post"><br>User <input type="text" name="user" value=""><                                  Br>Password <input type="text" name="pass" value=""><Br>                                  <input type="submit" value="▒▒▒▒"/></form><a href="login                                  .php">test</a></body></html>


---IP_SATE_CLOSE
00000
---closing begin
---socket closed

socket state
00001
###HTTP_process
---socket:
00000
---Size
00000   - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
###HTTP_process
---socket:
00000
---Size
00000 - this counter for bytes RX from browser
---IP_SATE_CLOSE
00000
---closing begin
---socket closed

socket state
00001

how can i stop keep silent from browser? or it’s problems with my code


#2

even if i do

     disconnect(socketn); 
     close(socketn);

it takes many time. browser does not want to shut off.
my browser Opera, or Chrome, or Vivaldi. all this browsers keep-alive connection. (i send header Connection: close). In the fire fox all OK.


#3

Those browsers you mentioned used to create spare connections to the web server, occupying the sockets for quite a time. Firefox has option to limit concurrent connections in its configuration tab.

You did not publish your code, but you must ensure that after server completes communicating from its side it performs DISCON and then (without waiting) performs CLOSE, and then OPEN, and LISTEN. Faster you do this process, less time you will have with socket being offline.

If you perform as I explained above, your clinet really connects (and you can confirm it wit Wireshark) but server does not send a data then the fault is in server’s code.


#4

ok, it is my code
/*
* w5500my.c
*
* Created: 04.08.2018 14:04:51
* Author: belaz
*/

#define DDR_MOSI 2
#define DDR_SCK  1
#define DDR_MISO 3
#define DDR_SS 0
#define WIZNET_resetpin 5
#define WIZNET_select PORTB&=0b111111110
#define WIZNET_cancel   PORTB|=0b000000001

#define F_CPU 8000000 
#include <avr/io.h>

#include <avr/interrupt.h>
//#include <avr/iom328p.h>
#include <util/delay.h>
#include "w5500/w5500.h"
#include  "socket.h"
#include "httpServer/httpmy.c"

#define IP_STATE_NO_SOCKET	0
#define IP_STATE_SOCKET		1
#define IP_STATE_LISTEN     2
#define IP_STATE_CLOSE		3
#define IP_STATE_ECHO		4
#define IP_STATE_GOODBYE    5
#define IP_STATE_DATA		IP_STATE_ECHO
#define IP_STATE_WRITE_answer 7
#define IP_STATE_READ_AND_WRITE 8
#define WRITE_answer 1
#define READ  0
#define led_green         PORTC|=0b00100000

volatile httpRequest myRequest;
volatile uint8_t socket0st,socket1st,socket2st,socket3st,socket4st,socket5st,socket6st,socket7st,socketst[8]={0,0,0,0,0,0,0,0};
volatile uint8_t socket_read_count;

uint8_t write_buffer_len = 0;
volatile char *READ_HTTP,WRITE_HTTP[1000],*TEMP_HTTP;
volatile uint16_t httppagesize,READ_point=0,WRITE_point_end=0,WRITE_point_s=0;
void reset_wiznet()
{
	DDRB|=(1<<WIZNET_resetpin); 
	PORTB&=~(1<<WIZNET_resetpin);
	_delay_ms(1);
	PORTB|=(1<<WIZNET_resetpin);
	_delay_ms(1);
	return;
}

void SPI_MasterInit(void)
{
	/* Set MOSI and SCK output, all others input */
	DDRB |= (1<<DDR_MOSI||1<<DDR_SCK||1<<DDR_SS); //x x x 0 miso mosi sck sc
	PORTB|=1<<DDR_SS; // ss=1
	/* Enable SPI, Master, set clock rate fck/16 */
	SPCR = (1<<SPE)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA); // SPIE=0 SPE=1 DORD=0 MSTR=1 CPOL=0 CPHA=0 SPR01=0 SPR00=0
	
	SPSR= 0b00000000;
}



//------------------------------------------------ SEARCH DOUBLE ENTER IN REQUEST
int16_t search_denter(char *buff,uint16_t size) // \r=13 \n 0a 
{
	uint16_t i;
	for(i=0;i<size;i++) { 
		if (buff[i]==10) { 
			if (buff[i+1]==13&&buff[i+2]==10) return i-1;
			if (buff[i+1]==10) return i-1;
			//if (buff[i]==10&&buff[i+2]==10) return i;
			}
		 } return -1; //0a
}

//------------------------------------------------ WRITE DATA TO ETHERNET w5500
void WRITE_answer_to_eth(uint8_t sock, httpRequest *request)
{
		// WRITE_answer:
		//if (httppagesize-WRITE_point_end>1000) {
		//	size2=httppagesize-WRITE_point_end;
		//	} else size2=1000;
		WRITE_point_end += send(sock, WRITE_HTTP+WRITE_point_end, request->headerlength+request->contentlength);  // отправляем ответ
		UART_Send_Str("\r\nwrite...",100);
		//UART_Send_Str(WRITE_HTTP,myRequest.contentlength);
		if (WRITE_point_end == request->headerlength+request->contentlength )
			{ READ_point=0; WRITE_point_end=0; UART_Send_Str("\r\nwrite end.",100); return 1; }
}


//------------------------------------------------ READ & WRITE HTTP DATA
uint8_t HTTP_process(uint8_t sockn){
	int16_t enter,i; 
uint16_t size,size2;	
char *writeptr;
        //read
		UART_Send_Str("\r\n###HTTP_process\r\n---socket:",100);
		 // размер данных
		size = getSn_RX_RSR(sockn);
		UART_Send_Int(sockn);
		UART_Send_Str_Back("---Size",12);
		UART_Send_Int(size);
		if (size>0) {
			socket_read_count=0;
			if(size > 1000) size = 1000;
			READ_point+=recv(sockn,READ_HTTP+READ_point,size); // получаем данные
			
			if (search_denter(READ_HTTP,READ_point)>=0)
			    {
				enter=search_denter(READ_HTTP,READ_point);
				myRequest.contentlengthreq=getcontentlen(READ_HTTP,READ_point,&myRequest);
				if (myRequest.contentlengthreq==0) {
					// обработка запроса
					
					
					HTTP_pars(READ_HTTP,enter,&myRequest,READ_HTTP+enter+4);
					//UART_Send_Str_Back(READ_HTTP,READ_point);
				    //led_green;
					make_http_page(&myRequest,WRITE_HTTP);
					WRITE_answer_to_eth(sockn,&myRequest);
					//UART_Send_Str_Back(WRITE_HTTP,myRequest.contentlength+myRequest.headerlength);
					
					return IP_STATE_CLOSE;
					} else {  
					// обработка запроса если ПОСТ запрос рассчитываем , когда конец данных
					//READ_point+=recv(sockn,READ_HTTP+READ_point,(enter+4)+myRequest.contentlengthreq-(READ_point));  
					//UART_Send_Str("---read POST tree",100);
					make_http_page(&myRequest,WRITE_HTTP);
					WRITE_answer_to_eth(sockn,&myRequest); 
					return IP_STATE_CLOSE;
					}
				}
			}
 socket_read_count++;
 if (socket_read_count>16) { socket_read_count=0; return IP_STATE_CLOSE; }
return IP_STATE_READ_AND_WRITE;
}


//------------------------------------------------ MAANAGE SOCKETs STATUS
void http_task(uint8_t socketn){
	int8_t a;
	switch(socketst[socketn]){
		//------------------------------------------------
		case IP_STATE_NO_SOCKET:
		 if(socket(socketn, Sn_MR_TCP, 80, 0) == socketn)
		 socketst[socketn] = IP_STATE_SOCKET;
		   break;
		//------------------------------------------------
		case IP_STATE_SOCKET:
		a=listen(socketn);
		switch (a){
			case SOCK_OK :
			UART_Send_Str_Back("\r\nsocket state",100);
			UART_Send_Int(a);
			socketst[socketn] = IP_STATE_LISTEN;
			break;
			case SOCKERR_SOCKCLOSED :
			socketst[socketn] = IP_STATE_CLOSE;
		    }
		break;
		//------------------------------------------------
		case IP_STATE_LISTEN :
		switch(getSn_SR(socketn)){
			case SOCK_LISTEN :
			_delay_ms(10);
			break;
			case SOCK_ESTABLISHED:
			socketst[socketn] = IP_STATE_READ_AND_WRITE;
			break;
			default:
			socketst[socketn] = IP_STATE_CLOSE;
		}
		break;
		//------------------------------------------------
		case IP_STATE_READ_AND_WRITE:
		if (getSn_SR(socketn) == SOCK_ESTABLISHED) {
			_delay_ms(10);
			socketst[socketn] = HTTP_process(socketn);
			} else {
			socketst[socketn] = IP_STATE_CLOSE;
		    }
		break;
		//------------------------------------------------

		//------------------------------------------------
		case IP_STATE_CLOSE:
			UART_Send_Str_Back("---IP_SATE_CLOSE ",33);
		   if (disconnect(socketn)==SOCK_BUSY) UART_Send_Str("\r\nbusy",100);
		   close(socketn);
		   UART_Send_Str_Back("---socket closed",100);
		   socketst[socketn]=IP_STATE_NO_SOCKET; 
		break;

	}
	return socketst[socketn];
}


int main(void)
{
 char test3[]="hello world!                           ";
	wiz_NetInfo myCong={
		.mac={12,13,56,57,58,59},
		.ip={192,168,1,19},
		.sn={255,255,255,0},
		.gw={192,168,1,1},
		.dhcp=NETINFO_STATIC, 
		.dns={192,168,1,1}
		};
netmode_type mytype={0};
READ_HTTP=WRITE_HTTP;
wiz_NetTimeout mynettimeout={.retry_cnt=15,.time_100us=50};
DDRA=0;
DDRD=0b00000000;//
DDRB=0b10100111; // 0 0 1 0 0miso 1mosi 1sck 1sc
DDRC=0b10100000; //c7-en c5-data
DDRE=0;
DDRG=0;
DDRF=0;
rfinit();

reset_wiznet();
_delay_ms(10);
SPI_MasterInit();
	UART_Init();
	UART_Send_Str("\r\nHELLO",10);


	
	
reg_wizchip_cs_cbfunc(0,0);
reg_wizchip_spi_cbfunc(0,0);
reg_wizchip_spiburst_cbfunc(0,0);
		ctlnetwork(CN_SET_NETINFO, &myCong);
		ctlnetwork(CN_SET_NETMODE, &mytype );
		ctlnetwork(CN_SET_TIMEOUT, &mynettimeout );
		



	while (1) {
	    
		do { http_task(0); }
		while (socketst[0]==IP_STATE_READ_AND_WRITE);
		do { http_task(1); }
		while (socketst[1]==IP_STATE_READ_AND_WRITE);
		do { http_task(2); }
		while (socketst[2]==IP_STATE_READ_AND_WRITE);

		};