Задача: на любой вопрос TCP клиента (отправляю с браузера) ответить буквально несколькими байтами. Самый-самый простой случай. Но в связи с тем, что я не нашел исходника сервера с использованием кейловского стека, приходится выдумывать....
Если посмотреть на main, там есть довольно подозрительное tcp_listen() в бесконечном цикле. Его быть не должно. Думаю, раз я в процедуре send_data_tcp() делаю tcp_close(), то потом я должен опять открыть сокет на прослушку.
В общем, как-то заработало, но иногда виснет. Отрисовка на TFT длится около 0,5 сек. Если убрать, но не виснет никогда. Но что-то метод не научный совсем. Хотелось бы не угадывать, а узнать как надо. Буду рад любому совету.
Спасибо.
CODE
/**************************************************************************//**
//TCP test
//CPU: LPC1768
//Compiler: uVision v.4.21, RTL v.4.13
******************************************************************************/
#include "LPC17xx.h"
#include "RTL.h"
#include "lcd.h"
#include "timer.h"
#include <string.h>
U8 tcp_soc; //socket name
U8 buff_recieve[1024]; //receive buffer
U8 *sendbuf; //buffer to be sent
U32 global_i = 0;
U32 flag_receive = 0;
unsigned char GetResponse[] = // 1st thing our server sends to a client
{
"HTTP/1.0 200 OK\r\n" // protocol ver 1.0, code 200, reason OK
"Content-Type: text/html\r\n" // type of data we want to send
"\r\nStatus=OK_res_" // indicate end of HTTP-header
};
void send_data_tcp (void)
{
U16 maxlen; //max size
U16 len=0;
if (tcp_check_send (tcp_soc) == __TRUE) { //is it ready for send?
maxlen = tcp_max_dsize (tcp_soc);
sendbuf = tcp_get_buf (maxlen);
memcpy (sendbuf,GetResponse,sizeof(GetResponse)-1);
len=sizeof(GetResponse)-1;
*(sendbuf+len) = (global_i)/1000 + 0x30; len++;
*(sendbuf+len) = ((global_i)%1000)/100 + 0x30; len++;
*(sendbuf+len) = ((global_i)%100)/10 + 0x30; len++;
*(sendbuf+len) = (global_i++)%10 + 0x30; len++;
tcp_send (tcp_soc, sendbuf,len); //send data
tcp_close(tcp_soc); //close the connection
}
}
U16 tcp_callback(U8 soc, U8 evt, U8 *ptr, U16 par){ // This function is called by the TCP module on TCP event
// Check the 'Net_Config.h' for possible events.
switch (evt) {
case TCP_EVT_DATA: // TCP data frame has arrived, data is located at *par1,
memcpy(&buff_recieve, ptr, par); // data length is par2. Allocate buffer to send reply.
flag_receive = 1;
break;
case TCP_EVT_CONREQ: // Remote peer requested connect, accept it
return (1);
case TCP_EVT_CONNECT: // The TCP socket is connected
return (1);
}
return (0);
}
int main (void)
{
SystemInit (); //Init system
LCD_Init(); // Init Lcd
BACK_COLOR = Blue;
LCD_Clear(Blue);
LCD_ShowString(10,10,"TCP test:");
init_TcpNet();
tcp_soc = tcp_get_socket (TCP_TYPE_SERVER, 0, 30, tcp_callback);
if (tcp_soc != 0) {
tcp_listen (tcp_soc, 80); //Start listening on TCP port 80
}
while (1)
{
main_TcpNet();
send_data_tcp(); //send!
if (tcp_soc != 0) { // WHY???
tcp_listen (tcp_soc, 80); // listening on TCP port 80
}
if (flag_receive != 0) {
LCD_ShowString(100,10,buff_recieve); // exec time is about 0.5sec!!!
flag_receive = 0;
}
}
}
//TCP test
//CPU: LPC1768
//Compiler: uVision v.4.21, RTL v.4.13
******************************************************************************/
#include "LPC17xx.h"
#include "RTL.h"
#include "lcd.h"
#include "timer.h"
#include <string.h>
U8 tcp_soc; //socket name
U8 buff_recieve[1024]; //receive buffer
U8 *sendbuf; //buffer to be sent
U32 global_i = 0;
U32 flag_receive = 0;
unsigned char GetResponse[] = // 1st thing our server sends to a client
{
"HTTP/1.0 200 OK\r\n" // protocol ver 1.0, code 200, reason OK
"Content-Type: text/html\r\n" // type of data we want to send
"\r\nStatus=OK_res_" // indicate end of HTTP-header
};
void send_data_tcp (void)
{
U16 maxlen; //max size
U16 len=0;
if (tcp_check_send (tcp_soc) == __TRUE) { //is it ready for send?
maxlen = tcp_max_dsize (tcp_soc);
sendbuf = tcp_get_buf (maxlen);
memcpy (sendbuf,GetResponse,sizeof(GetResponse)-1);
len=sizeof(GetResponse)-1;
*(sendbuf+len) = (global_i)/1000 + 0x30; len++;
*(sendbuf+len) = ((global_i)%1000)/100 + 0x30; len++;
*(sendbuf+len) = ((global_i)%100)/10 + 0x30; len++;
*(sendbuf+len) = (global_i++)%10 + 0x30; len++;
tcp_send (tcp_soc, sendbuf,len); //send data
tcp_close(tcp_soc); //close the connection
}
}
U16 tcp_callback(U8 soc, U8 evt, U8 *ptr, U16 par){ // This function is called by the TCP module on TCP event
// Check the 'Net_Config.h' for possible events.
switch (evt) {
case TCP_EVT_DATA: // TCP data frame has arrived, data is located at *par1,
memcpy(&buff_recieve, ptr, par); // data length is par2. Allocate buffer to send reply.
flag_receive = 1;
break;
case TCP_EVT_CONREQ: // Remote peer requested connect, accept it
return (1);
case TCP_EVT_CONNECT: // The TCP socket is connected
return (1);
}
return (0);
}
int main (void)
{
SystemInit (); //Init system
LCD_Init(); // Init Lcd
BACK_COLOR = Blue;
LCD_Clear(Blue);
LCD_ShowString(10,10,"TCP test:");
init_TcpNet();
tcp_soc = tcp_get_socket (TCP_TYPE_SERVER, 0, 30, tcp_callback);
if (tcp_soc != 0) {
tcp_listen (tcp_soc, 80); //Start listening on TCP port 80
}
while (1)
{
main_TcpNet();
send_data_tcp(); //send!
if (tcp_soc != 0) { // WHY???
tcp_listen (tcp_soc, 80); // listening on TCP port 80
}
if (flag_receive != 0) {
LCD_ShowString(100,10,buff_recieve); // exec time is about 0.5sec!!!
flag_receive = 0;
}
}
}