I am creating client-server application.
client sends text file through sendto() function to server.
Problems are following--

1>At the server i am writing into new abc.txt file.
but problem is it writes some garbage data also....

server side function ----

char RecvBuf[22610];
int  BufLen = 22610;


recvfrom(RecvSocket,RecvBuf,BufLen, 0,(SOCKADDR *)&SenderAddr, &SenderAddrSize);
FILE * pFile;


pFile = fopen ("abc.txt" , "w");
fwrite (RecvBuf , 1 ,sizeof(RecvBuf) , pFile);
fclose (pFile);

2> When i am execute exe by using system command it creates info.txt
some time this file size is 27kb otherwise 23/30/15/5 kb
is problem occurs because of i m not sending correct file size through socket???

client side function----

char SendBuf[22610];
int BufLen = 22630;
char buffer[25000];


pFile = fopen ("info.txt" , "r");
while(!feof(pFile))
{
fread (SendBuf , 1 ,sizeof(SendBuf) , pFile);


sendto(SendSocket, SendBuf,BufLen,0,(SOCKADDR *) &RecvAddr,sizeof(RecvAddr));
}
fclose (pFile);

How to solve this problem???

Recommended Answers

All 11 Replies

soory, I'll read 1/2 hour later. Got to go.

The client needs to specify how much data it sends, i.e. you have to use the value you get from fread(). The server must write only the amount it gets from recvfrom(). The return value of recvfrom() gives you that information. And last but not least, you must add error checking code, so that you not just blindly write/send something, for example, you have to check that recvfrom() is not returning an error or zero.
See the documentation for each of the functions you use, to figure out the correct error checking code.

When i am execute clientinfo.exe by using system command it creates info.txt
some time this file size is 27kb otherwise 23/30/15/5 kb

in code....
char SendBuf[22610];
int BufLen = 22630;
char buffer[25000];
system("C:\\clientinfo.exe");
pFile = fopen ("info.txt" , "r");
while(!feof(pFile))
{
fread (SendBuf , 1 ,sizeof(SendBuf) , pFile);

sendto(SendSocket, SendBuf,BufLen,0,(SOCKADDR *) &RecvAddr,sizeof(RecvAddr));
}
fclose (pFile);

fread& fwrite are used when, reading files in binary mode I presume. try using fgets(), fputs or open the file in binary mode & reply with what you have.

sizeof ( Recvbuff ) is always 22610. You cant assure that. Use the value returned by readfrom insted.

Oh! I was out to lunch & I didnt see the replies you recived. Ya Its the same point.

As previously mentioned you should not use feof() to control that loop. Here is the correct way. You also have a problem by assuming that each read of the file will read exactly BufLen bytes. That may or may not be true. fread() will return the number of bytes read so you should only send that many bytes.

const int BufLen = 22610;
unsigned char RecvBuf[BufLen];

pFile = fopen ("info.txt" , "r");
unsigned int size = 0;
while( (size = fread (SendBuf , 1 ,sizeof(SendBuf) , pFile)) > 0)
{
        sendto(SendSocket, SendBuf, size ,0, (SOCKADDR *) &RecvAddr,sizeof(RecvAddr));
}
fclose (pFile);

I am creating client-server application. In that server is continuously running and client sends Information to server.
When client exe executes... it calls clientinfo.exe& create info.txt
This clientinfo.exe about codecs, ip, system info.
But on some clients no codes are available.
So size of info.txt is varies i.e. size is 24kb/23kb/15kb/4kb


I am trying to send text file through the sendto() function from Client..
But I am still getting info.txt file data + garbage data on the Server side...
How to get data without garbage value???

I am glad that the first is solved.

What function to you use to get the data from info.txt. in client side

And thanks for the comment.

[info.txt] 1.810000000000000e+003 1.804000000000000e+003
-6.470518565393491e-016 8.242640687119284e+000
-4.000000000000000e+000 0.000000000000000e+000
-8.284271247461894e-001 -5.071067811865476e+000
-2.000000000000000e+000 4.000000000000000e+000
6.470518565393491e-016 -2.426406871192848e-001
4.440892098500626e-016 -1.200000000000000e+001
4.828427124746190e+000 9.071067811865476e+000
€ÿ" öF„| >= (>= @= @= C : \ New e r v . e x [/info.txt]

This is my file ..
How to remove garbage from file??

I think you have not paid attention to the posts you have received in this thread, so take the time to read and understand what for example Ancient Dragon's reply to you (see above) says.
Especially pay attention to the manner in which the file is read and how the data is sent in the example he gave.

Ya. You should give importance to the no of bytes you recive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.