Sorry, the code is below:
#include "ipport.h" /* from Interniche directory */
#include "osport.h"
#include "msock.h"
//*****************************************************************************
// Declare Task Object
//*****************************************************************************TK_OBJECT(to_keludpclient);
TK_OBJECT(to_tcpserver);
TK_ENTRY(tk_tcpserver);
struct inet_taskinfo tcp_listener_task={ &to_tcpserver,
"TCP passivo",
tk_tcpserver,
NET_PRIORITY,
APP_STACK_SIZE
};
void TCP_check(void);
int TCP_Callback(int code, M_SOCK so, void * data);
int TCP_init(void);
M_SOCK tcp_server_socket;
int tcp_semaphore=0;
#define RECV_BUFFER_SIZE 0x400
#define TCP_PORT_NUMBER 1024
int TCP_Closed=1;
struct sockaddr_in tcp_sin;
TK_ENTRY(tk_tcpserver)
{
int err;
// Wait for the network to be ready
while (!iniche_net_ready)
{
TK_SLEEP(1);
}
err = TCP_init();
if (err != SUCCESS)
{
dtrap(); // TCP_init() failed!!!
}
// Endless loop - this is the task running.
#ifdef NPDEBUG
printf("Initiating loop for TCP_check()...\n");
#endif
for (;
{
// Check for incomming traffic.
TCP_check();
// Yield to CPU for other tasks to run.
tk_yield();
// Check for system shutdown
if (net_system_exit)
{
break;
}
}
TK_RETURN_OK();
}
void TCP_check(void)
{
// Create a 32-bit alignment buffer
char buffer[RECV_BUFFER_SIZE];
int length=0;
int j;
int k;
if(MCF_GPIO_PORTTC<0x07)
MCF_GPIO_PORTTC++;
else
MCF_GPIO_PORTTC = 0x00;
if (tcp_server_socket == INVALID_SOCKET)
{
return;
}
if(TCP_Closed==0)
while ((length = m_recv(tcp_server_socket, &buffer[0], RECV_BUFFER_SIZE ))>0)
{
tcp_semaphore = 1;
// Read tcp_msring data and process
if (length > 0)
{
#ifdef NPDEBUG
printf("TCP Received...\n");
#endif
m_send(tcp_server_socket, &buffer[0], length);
}
tcp_semaphore = 0;
}
}
int TCP_Callback(int code, M_SOCK so, void * data)
{
int e = 0;
switch (code)
{
// socket open complete
case M_OPENOK:
if (TCP_Closed)
{
printf("OPEN\n");
TCP_Closed = 0;
}
break;
// socket has closed
case M_CLOSED:
m_close(so);
TCP_Closed = 1;
printf("CLOSED\n");
TCP_init();
break; // let stale conn timer catch these
// passing received data
// blocked transmit now ready
case M_RXDATA: // received data packet, let recv() handle it
case M_TXDATA: // ready to send more, loop will do it
e = -1; // return nonzero code to indicate we don't want it
break;
default:
printf("dtrap %d\n", code);
dtrap(); // not a legal case
return 0;
}
TK_WAKE(&to_tcpserver); // wake server task
USE_VOID(data);
return e;
}
int TCP_init()
{
int e;
tcp_semaphore = 0;
// Init a socket structure with our Port Number
tcp_sin.sin_addr.s_addr = (INADDR_ANY);
tcp_sin.sin_port = (TCP_PORT_NUMBER);
tcp_server_socket = m_listen(&tcp_sin, TCP_Callback, &e);
printf("Listening...\n");
if (tcp_server_socket == INVALID_SOCKET)
{
printf("error %d starting listen on TCP server\n", e);
}
return SUCCESS ;
}