Altera_Forum
Honored Contributor
15 years agoProblem with LWIP_ UDP can receice can't send!
My project is to transmit data to pc as quick as possible . Now I can transfer data to PC with lwip tcp/ip ,the max rate is about 920KB/s, So I think if I use the udp ,the speed will be much high. But now ,the problem is I make a udp server I can get the data from the PC, but I can’t transmit my data to the PC!
Here is the code with the UDP Task! Somebody can help me ! And I am sorry for my English . I use quartus 8.1 DM9000A EP3C25F324
# include <stdio.h># include <stdlib.h># include <string.h># include <ctype.h># include <sys/param.h># include <sys/fcntl.h># include "lwip/netif.h"# include "lwip/sockets.h"# include "arch/sys_arch.h"# include "sys/alt_alarm.h"# include "alt_types.h"# include "http.h"# include "user.h"
# include "system.h"# include <string.h># include "io.h"# include "altera_avalon_pio_regs.h"# include "alt_types.h"# include "sys/alt_irq.h"# include "priv/alt_busy_sleep.h"# include "sys/alt_dev.h"
volatile alt_u32 done=0; // SRH
alt_u32 data1=0; // SRH
alt_u32 data_a,data_i;
alt_u32 data={0};//gsl
void http_task()
{
int i, fd_listen, max_socket;
int socket_send,len_send; //srh
int socket_recv,len_recv; //srh
struct sockaddr_in addr,sDestAddr;
struct timeval select_timeout;
struct sockaddr_in send_rem; //srh
struct sockaddr_in recv_rem; //srh
fd_set readfds, writefds;
static http_conn conn;
unsigned char *dddata; //srh
unsigned char err; //srh
unsigned char buff="12345"; //srh
len_send = sizeof(send_rem); //srh
len_recv = sizeof(recv_rem); //srh
//if ((fd_listen = socket(AF_INET, SOCK_STREAM, 0)) < 0)
if ((fd_listen = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
die_with_error(" Listening socket creation failed");
}
addr.sin_family = AF_INET;
addr.sin_len=sizeof(addr);
addr.sin_port = htons(12345); // 80 HTTP_PORT
addr.sin_addr.s_addr = INADDR_ANY;
sDestAddr.sin_family=AF_INET;
sDestAddr.sin_len=sizeof(sDestAddr);
sDestAddr.sin_port=htons(23456);
sDestAddr.sin_addr.s_addr=htonl(INADDR_BROADCAST);
if ((bind(fd_listen,(struct sockaddr *)&addr,sizeof(addr))) < 0)
{
die_with_error(" Bind failed");
}
/*
* Sockets primer, continued...
* The listen socket is a socket which is waiting for incoming connections.
* This call to listen will block (i.e. not return) until someone tries to
* connect to this port.
*/
//if ((listen(fd_listen,1)) < 0)
//{
// die_with_error(" Listen failed");
//}
/*
* At this point we have successfully created a socket which is listening
* on HTTP_PORT for connection requests from any remote address.
*/
// for(i=0; i<HTTP_NUM_CONNECTIONS; i++)
//{
// http_reset_connection(&conn, i);
//}
while(1)
{
/*****************************************SRH**********************************************/
/* printf("receive test\n");
socket_recv=accept(fd_listen,(struct sockaddr*)&recv_rem,&len_recv);
while(1)
{
printf("test0");
if(0!=done)
{
printf("I am working!");
recv(socket_recv,data1,strlen(data1),0);
send(socket_send,data1,strlen(data1),0);
msleep(500);
done--;
}
}
close(socket_send);*/
/*****************************************SRH**********************************************/
/**************************srh****************************/
printf("\nreceive test\n");
//send(socket_send,(unsigned char *)&data,60,0);
// socket_send= accept(fd_listen,(struct sockaddr*)&send_rem,&len_send);
//sprintf(dddata,"welcome to cugb");
char buffer;
int nbytes;
while(1)
{
nbytes=lwip_recv(fd_listen, buffer, sizeof(buffer),8);
if(nbytes>0)
{ printf("test1->%s\n",buffer);
lwip_sendto(fd_listen,"UDP_DATA", sizeof("UDP_DATA")+1, 0, (struct sockaddr *)&sDestAddr, sizeof(sDestAddr));
msleep(500);
}
//lwip_sendto(fd_listen, "udp_data", sizeof("udp_data"), 0, (struct sockaddr *)&sDestAddr, sizeof(sDestAddr));
// if(0!=done)
//{
//send(socket_send,buff,strlen(buff),0);
// data=data_i;
// send(socket_send,(unsigned char *)&data,4000,0);//60
// data_i++;
// for(i=0;i<500;i++);
printf("waiting.....");
msleep(50);
// done--;
// }
}
close(fd_listen);
}
}