Tool/software: TI-RTOS
HI all ,
I would like write a program which include 2 task . one server task , another is client task .
Here is the task's function .
client : send data to client via localhost UDP
Server: echo the client task's data to client via localhost.
But the problem comes : Server could get data from client but client couldn't get the data from Server !! .
Server code :
void netOpenHook()
{
hEchoUdp = DaemonNew( SOCK_DGRAM, 0, 7, dtask_udp_echo,
OS_TASKPRINORM, OS_TASKSTKNORM, 0, 1 );
}
int dtask_udp_echo( SOCKET s, UINT32 unused )
{
struct sockaddr_in sin1;
struct timeval to;
int i,tmp;
char *pBuf;
HANDLE hBuffer;
int ret = 0;
char IPString[16];
(void)unused;
// Configure our socket timeout to be 3 seconds
to.tv_sec = 3;
to.tv_usec = 0;
setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
for(;;)
{
tmp = sizeof( sin1 );
i = (int)recvncfrom( s, (void **)&pBuf, 0,(struct sockaddr *)&sin1, &tmp, &hBuffer );
NtIPN2Str (sin1.sin_addr.s_addr, IPString);
System_printf("get data from %s:%d\n",IPString,sin1.sin_port);
// Spit any data back out
if( i >= 0 )
{
// DumpRouteTable();
System_printf("[%d]%s send %d byte %s to TestConsole\n",Clock_getTicks(),Task_Handle_name(Task_self()),i,pBuf);
ret = sendto( s, pBuf, i, 0,(struct sockaddr *)&sin1, sizeof(sin1) );
recvncfree( hBuffer );
}
else
break;
}
// Since the socket is still open, return "1"
// (we need to leave UDP sockets open)
return(1);
}
client code :
DRV_RET sendUDPpacket()
{
DRV_RET ret = 0;
struct sockaddr_in addr;
char IPString[16];
SOCKET s;
char buf[]="hello world";
HANDLE hBuffer;
int i,tmp;
char *pBuf;
fd_set readfds;
struct timeval timeout;
fdOpenSession(TaskSelf());
addr.sin_family = AF_INET;
addr.sin_port = htons(7);
addr.sin_addr.s_addr=inet_addr(LOCAL_HOST);
NtIPN2Str (addr.sin_addr.s_addr, IPString);
System_printf("local ip = %s\n",IPString);
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//
ret = sendto(s, buf, strlen(buf), 0,(struct sockaddr *) &addr, sizeof(addr) );
/* Wait for the reply */
FD_ZERO(&readfds);
FD_SET(s, &readfds);
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
if (fdSelect(s , &readfds, NULL, NULL, &timeout) != 1) {
System_printf( " timed out waiting for reply\n");
// status = errno;
return -1 ;
}
tmp = sizeof( addr );
System_printf("[%d]%s recieve ready!\n",Clock_getTicks(),Task_Handle_name(Task_self()));
i = (int)recvncfrom( s, (void **)&pBuf, 0,(struct sockaddr *)&addr, &tmp, &hBuffer );
if (i >=0 )
{
System_printf("return string = %s\n",pBuf);
recvncfree( hBuffer );
}
fdClose( s );
fdCloseSession(TaskSelf());
return ret;
}
Are there anything wrong in my client code ?
Brenden