Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Not able to receive all/complete packet from UDP server

Dear Friends,

I am trying to read data from UDP server....i am sending 7 packet of size 1448 bytes. from server and at receiver end i am collecting them in a array namelly "ImgBuff[0][1448]"

and at second read i am again write my income bit stream in same memory location but before that i print the privious received data.....but it won't print my second (and second onward ) received data. even i am able to receive this first packet after enabling "zero_copy" enable through bsp_editar

My Client Code is as below.................# include <stdio.h> # include <string.h> # include <ctype.h>

/* RS232 Related Files....*/ # include "alt_types.h" # include "altera_avalon_pio_regs.h" # include "sys/alt_irq.h" # include "system.h" # include <unistd.h>

/* MicroC/OS-II definitions */ # include "includes.h"

/* Simple Socket Server definitions */ # include "simple_udp_server.h"

/* Nichestack definitions */ # include "ipport.h" # include "tcpport.h"

# define CLI_PORT 6089 # define SERVER_PORT 6090 # define SERV_HOST_ADDR "192.168.5.2"

# define MaxPkt 0xff # define PktSize 1448

//unsigned char DataPacket[0][PktSize];

unsigned char ImgBuff[0][PktSize];

struct sockaddr_in Cliaddress, Servaddress;

int sockid;

char msg[32];

size_t len;

int LocalPktCount;

int retcode = 0;

void ConnectServer()

{

printf("Client: creating socket\n");

if ( (sockid = socket(AF_INET, SOCK_DGRAM, 0)) < 0)

{ printf("Client: socket failed: %d\n",errno); exit(0); }

printf("Client: binding my local socket\n");

bzero((char *) &Cliaddress, sizeof(Cliaddress));

Cliaddress.sin_family = AF_INET;

Cliaddress.sin_addr.s_addr = htonl(INADDR_ANY);

Cliaddress.sin_port = htons(CLI_PORT);

if ( (bind(sockid, (struct sockaddr *) &Cliaddress,sizeof(Cliaddress)) < 0) )

{ printf("Client: bind fail: %d\n",errno); exit(0); }

printf("Client: creating addr structure for server\n");

bzero((char *) &Servaddress, sizeof(Servaddress));

Servaddress.sin_family = AF_INET;

Servaddress.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);

Servaddress.sin_port = htons(SERVER_PORT);

printf("\nConnection Establish...\n");

}

void RecvFromServer(void)

{

int PktCount1[1];

int Count = 0;

int SecondCount = 0;

LocalPktCount = 0;

fflush(stdin); //Clean the input and output buffer

len = sizeof(Servaddress);

//receive total no. of pkt to be recived from remote client, and store it in "PktCount1" variable

retcode = recvfrom(sockid,PktCount1,sizeof(PktCount1),0,(struct sockaddr *)&Servaddress,&len);

if (retcode >0)

printf("\nClient: No. of Packet to be received is: %.d\n\n",PktCount1[0]);

do

{

LocalPktCount++;

alt_dcache_flush();

fflush(stdin);

retcode = recvfrom(sockid,ImgBuff[SecondCount],sizeof(ImgBuff[SecondCount]),0,(struct sockaddr *) &Servaddress, &len);

if(retcode <= 0)

{

printf("recv call failed");

break;

}

else {

//SecondCount++;

printf("\n Packet No. received %d\n", LocalPktCount);

}

if(ImgBuff[SecondCount][0] == MaxPkt && ImgBuff[SecondCount][1] == MaxPkt)

{

if(LocalPktCount != PktCount1[0]+1)

{

printf("\n Last Packet Received...!!\n");

//SecondCount++;

continue;

}

break;

}

Count = 0;

do

{

// DataPacket[SecondCount][Count] = ImgBuff[SecondCount][Count];

printf("%c",ImgBuff[SecondCount][Count]);

Count++;

}while(Count != PktSize);

//Count = 2; // Initilize data address.....

/*

Count = 0;

do

{

printf("%c",ImgBuff[SecondCount][Count]);

//IOWR_8DIRECT(SDRAM_BASE+LocalPktCOunt+Count,0,DataPacket[1][Count]);

Count++;

}while(Count != PktSize);

*/

}while(LocalPktCount != PktCount1[0]+1);//compair total no. of pkt+1 recived with count value which increment after each received pkt.

printf("\nLocalPktCount = %d",LocalPktCount-1);

printf("\nFile received Completed..!!\n\n");

}

void SSSSimpleUDPServerTask()

{

ConnectServer();

printf("\nClient: Write message and sending\n");

sprintf(msg, "Hello Server This is UDP Client");

retcode = sendto(sockid,msg,32,0,(struct sockaddr *) &Servaddress,sizeof(Servaddress));

if (retcode <= -1)

{

printf("client: sendto failed: %d\n",errno);

exit(0);

}

len = sizeof(Servaddress);

printf("Server: starting blocking message read\n");

retcode = recvfrom(sockid,msg,32,0,(struct sockaddr *) &Servaddress, &len);

printf("Client: retrun code from read is %d\n",retcode);

if (retcode >0) printf("\nClient: Received message is: %.32s\n\n",msg);

RecvFromServer();

/* close socket */

printf("\n..........Close the UDP Connection..........!!\n");

close(sockid);

}

please see the attached printscreen of received file along with first packet of received data.

I am not able to increase the size of buffer as i increase the size it will start displaying garbage data see the second attachment "error5" file

kaushal

1 Reply