473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Copy a file over TCP socket

Hi folks,
I have a very strange problem when I try to port my client/server
program to cygwin. It is a simple shell program where the server
executes client's commands + it can send and receive files (something
like ftp server/client).

I implemented all the commands which the server executes from scratch
meaning I don't use fork and exec. It was very educational but my
problem is that when I try to "get <filename>" from server to the
client in cygwin the client doesn't receive the whole file. I tracked
back and saw that the read() in the server returns end of file before
reading the whole file. I think that this might be a Windows problem
or cygwin specific problem because on my home Slackware computer the
program works perfect:
The preparation for the client to receive the file includes getting
the file size:

server: snprintf(buf, 10, "%ld", (long)dir_stat.st_size); // send the
size of the file
server: s_line(buf, sockfd); // to client

So right now I am tryting to copy a file with reported size: 34302723
bytes but read return 0 (end of file) on 34302220 bytes read. This is
a problem since the client stops waiting for data only if filesize ==
numbytes sent.

Server snip:

do
{
numbytes = read(dsc, buf, sizeof(buf));

if(numbytes == -1) {
printf("Error reading source file\n");
break;
}
else if(numbytes 0)
{
if ( (counter = write(sockfd, buf, numbytes)) < 0)
perror("get:write to socket");
total += counter;
}
else if (numbytes == 0)
printf("so far we've got %ld", total);
} while (numbytes != 0);
}
}
printf("Bytes Sent: %ld out of %ld\nFile sent successful\n",
total, (long)dir_stat.st_size);

So the last printf line on the server itself shows that there is
difference between the filesize of the file and the actual bytes sent
to clien:
Filesize: 34302723
Clients Received: 34302220

But still the while loop breaks because read returned 0 and therefore
the numbytes == 0 which is EOF if I am not mistaken.

client snip:

numbytes = read(sockfd, buf, sizeof(buf));
download += numbytes;
kbs += numbytes; // kb/s

gettimeofday(&tp2,NULL);

time +=(tp2.tv_sec - tp1.tv_sec) + (float)(tp2.tv_usec -
tp1.tv_usec)/1e6;

if (time>800) { // probably this is a second by my understandings
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b
\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ");
printf("%.2f[KB/S]\t%.4f/%.4f[MB] \t%.1f/100%%",(double)kbs /
1024, (double)download / (1024 * 1024), (double)kbytes / (1024 *
1024), (double)download * 100 / kbytes);
fflush(stdout);
kbs = 0;
time = 0;
gettimeofday(&tp1, NULL);
}

if (numbytes == -1)
perror("get:read");
else if (download == kbytes) // we match the downloaded chunk with
the filesize
{
if (numbytes 0 && strncmp(buf, "EOF\r\n", 5)) // if there is
more data
write(file, buf, numbytes); // write last bytes before closing the
file
printf("\n%.2f[KB/S]\t%.4f/%.4f[MB] \t%.2f/100%%\n",
(double)kbs / 1024, (double)download / (1024 * 1024), (double)kbytes /
(1024 * 1024), (double)download * 100 / kbytes);
printf("\nDownloading Successful!\n");
close(file);
break;
}
else
write(file, buf, numbytes);

The client receives the file size with the string that server sends
and performs kbytes = atol(buf)

So I see that I am missing the last part of the file I transfer not
because of the socket error
but probably because the server:
1. Doesn't report the file size right
2. Doesn't read correctly until EOF is reached

By the way i am dealing with binary files so I use open().
If the server doesn't report the file size right this means that the
client must receive the whole
file but if I transfer a rar archive and I try to open it it says -
unexpected end of file found so I
guess that there is a problem which I can't figure out.

I don't know why this problem does not exist on my slackware machine!?
And one last thing - the # of bytes the server reports as read are the
number of bytes that
the client actually received - so I don't know why the server read()
doesn't proceed to the end
of the file?

Any help will be appreciated.

Apr 13 '07 #1
4 5678
pr*****@yahoo.com wrote:
Hi folks,
I have a very strange problem when I try to port my client/server
program to cygwin. It is a simple shell program where the server
executes client's commands + it can send and receive files (something
like ftp server/client).
Cygwin has a mailing list. That's probably the best place for porting
questions. In addition, network questions are not generally topical
here, as standard C doesn't define any such mechanisms.


Brian
Apr 13 '07 #2
<pr*****@yahoo.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
I have a very strange problem when I try to port my client/server
program to cygwin. It is a simple shell program where the
server executes client's commands + it can send and receive
files (something like ftp server/client).

... my problem is that when I try to "get <filename>" from server
to the client in cygwin the client doesn't receive the whole file. I
tracked back and saw that the read() in the server returns end of
file before reading the whole file. I think that this might be a
Windows problem or cygwin specific problem because on my
home Slackware computer the program works perfect:
<OT>
I suggest you look at your open() call and make sure you're passing
O_BINARY; Windows forces you to be explicit when you're opening binary files
and Cygwin can't hide that problem from you. More details at:

http://www.cygwin.com/cygwin-ug-net/...extbinary.html

Just using open() doesn't mean you don't have this issue; you have to call
it correctly, just like fopen().
</OT>

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Apr 13 '07 #3
pr*****@yahoo.com wrote:
# do
# {
# numbytes = read(dsc, buf, sizeof(buf));
#
# if(numbytes == -1) {
# printf("Error reading source file\n");
# break;
# }
# else if(numbytes 0)
# {
# if ( (counter = write(sockfd, buf, numbytes)) < 0)
# perror("get:write to socket");
# total += counter;
# }

On unix, write is not guarenteed to write all bytes in the call.

You can check with
if (numbytes>counter) printf("incomplete write %ld %ld\n",numbytes,counter);

# else if (numbytes == 0)
# printf("so far we've got %ld", total);
# } while (numbytes != 0);
# }
# }

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Leave it to the Catholics to destroy existence.
Apr 14 '07 #4
Thanks for the replays guys :). The O_BINARY mode is not available on
my Slackware (it says not defined) but it is available in cygwin and
it fixed my problem (in windows xp under vmware). I still have to
check this out on a true windows machine but I don't see why it is not
going to work.

Thanks Again!

Apr 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
1
by: ADE | last post by:
Hi everyone well from my last post I found what I am looking for I have some code now that transfers files I have added a GUI to it and need some help with two things one my loadtemplate()...
4
by: zelzel.zsu | last post by:
I wrote two simple socket program. one for sending a file and the other for receiving the file. but when I run it, a curious thing happened. The received file was samller that the sent file. $...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
2
by: darthghandi | last post by:
I am creating a listener class that listens for incoming connections. When I get an incoming connection, I want to signal an event to happen and pass that connected socket to a different thread...
10
by: David | last post by:
I have googled to no avail on getting specifically what I'm looking for. I have found plenty of full blown apps that implement some type of file transfer but what I'm specifcally looking for is an...
3
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
9
dmjpro
by: dmjpro | last post by:
HI i am starting Java Networking programming with File Copy over Network. I got a sample code .... but still having some confusions. Socket socket = new Socket(ip,port); OutputStream out =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.