473,398 Members | 2,427 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,398 software developers and data experts.

Sending file over a socket

Hi guys,
I have a question. I am trying to write a simple echo program. The
objective is to read in a file in chuncks into the buffer and transfer
the buffer over the socket. We repeat this until the entire file is
transfered.

The problem that I have right now is that I can only read the data to
the buffer one time and one time only. So for instance if my file is 1
MG. I can only read the first 256 char which is the size of my buffer.

Here is a code snippet.
memset(&socketChannel, 0, sizeof(socketChannel)); //INITILIZE ALL THE
FIELDS TO 0
socketChannel.sin_family = AF_INET; //IP 4 FAMILY
socketChannel.sin_port = htons(PORT); //PUT THE PORT TO CONNECT TO
inet_pton(AF_INET, argv[1], &socketChannel.sin_addr); //PUT THE IP
ADDRESS OF THE SERVER TO CONNECT
sd = socket (AF_INET, SOCK_STREAM, 0); //WE USE TCP CONNECTION HERE
FD_ZERO(&fileDesc); //WE INITILIZE THE DESCRIPTOR HERE
descValue = fileno(inputFile);

if (connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr)) == -1 ){
cout << "Cannot connect... \n"; //WE CONNECT TO THE SERVER HERE
exit(1);
}
if (descValue < sd)
descValue = sd + 1 ;

for(int i = 0; i < 2; ){ //WE ONLY WANT TWO ITERATION
FD_SET(fileno(inputFile), &fileDesc); //WE SET THE DESCRIPTOR VAL
FD_SET(sd,&fileDesc);
select(descValue, &fileDesc, NULL, NULL, NULL);

if (FD_ISSET(sd, &fileDesc)){
if ((byteIn = recv(sd, recBuf, BUFSIZE, 0)) < 0){
cout << "Error reading the socket...\n";
exit(1);
}
else {
byteOut = write(fileno(outputFile), recBuf, byteIn);
charIn += byteIn;
fragIn++;
if((charIn >= charOut) ) {
cout << "The number of buffers transmitted is " << fragOut <<
endl;
cout << "The number of bytes transmitted is " << charOut << endl;
cout << "The number of buffers recieved is " << fragIn << endl;
cout << "The number of bytes received is " << charIn << endl;
//close(inputFile);
//close(outputFile);
i++;
break;
}
}
}

if (FD_ISSET(fileno(inputFile), &fileDesc) && !(feof(inputFile)))){
if ((byteIn = read(fileno(inputFile), sndBuf, BUFSIZE )) < 0){
cout << "Error reading the input file...\n";
exit(1);sizeof(socketChannel)
}
else if (byteIn 0 ) {
if (byteOut = send(sd, sndBuf, byteIn, 0) < 0){
cout << "Error sending data...\n";
exit(1);
}
charOut += byteOut;
fragOut++;
}
byteIn = 0;
memset(&sndBuf, 0, BUFSIZE);
}
}
}

any help would be much appreciated.

Thanks

J

Jan 31 '07 #1
2 2637

"Sean" <ac*******@hotmail.comwrote in message
news:11*********************@m58g2000cwm.googlegro ups.com...
Hi guys,
I have a question. I am trying to write a simple echo program. The
objective is to read in a file in chuncks into the buffer and transfer
the buffer over the socket. We repeat this until the entire file is
transfered.

The problem that I have right now is that I can only read the data to
the buffer one time and one time only. So for instance if my file is 1
MG. I can only read the first 256 char which is the size of my buffer.

Here is a code snippet.
memset(&socketChannel, 0, sizeof(socketChannel)); //INITILIZE ALL THE
FIELDS TO 0
socketChannel.sin_family = AF_INET; //IP 4 FAMILY
socketChannel.sin_port = htons(PORT); //PUT THE PORT TO CONNECT TO
inet_pton(AF_INET, argv[1], &socketChannel.sin_addr); //PUT THE IP
ADDRESS OF THE SERVER TO CONNECT
sd = socket (AF_INET, SOCK_STREAM, 0); //WE USE TCP CONNECTION HERE
FD_ZERO(&fileDesc); //WE INITILIZE THE DESCRIPTOR HERE
descValue = fileno(inputFile);

if (connect (sd, (struct sockaddr*) &socketChannel, sizeof(struct
sockaddr)) == -1 ){
cout << "Cannot connect... \n"; //WE CONNECT TO THE SERVER HERE
exit(1);
}
if (descValue < sd)
descValue = sd + 1 ;

for(int i = 0; i < 2; ){ //WE ONLY WANT TWO ITERATION
Why are you only iterating twice? You say the file is 1mb. If your buffer
size is 256 chars it's going to take quite a bit more than 2 iterations
(about 3,000) more.

Maybe you want
while ( charIn < charOut )
or something.
FD_SET(fileno(inputFile), &fileDesc); //WE SET THE DESCRIPTOR VAL
FD_SET(sd,&fileDesc);
select(descValue, &fileDesc, NULL, NULL, NULL);

if (FD_ISSET(sd, &fileDesc)){
if ((byteIn = recv(sd, recBuf, BUFSIZE, 0)) < 0){
cout << "Error reading the socket...\n";
exit(1);
}
else {
byteOut = write(fileno(outputFile), recBuf, byteIn);
charIn += byteIn;
fragIn++;
if((charIn >= charOut) ) {
cout << "The number of buffers transmitted is " << fragOut <<
endl;
cout << "The number of bytes transmitted is " << charOut << endl;
cout << "The number of buffers recieved is " << fragIn << endl;
cout << "The number of bytes received is " << charIn << endl;
//close(inputFile);
//close(outputFile);
i++;
break;
}
}
}

if (FD_ISSET(fileno(inputFile), &fileDesc) && !(feof(inputFile)))){
if ((byteIn = read(fileno(inputFile), sndBuf, BUFSIZE )) < 0){
cout << "Error reading the input file...\n";
exit(1);sizeof(socketChannel)
}
else if (byteIn 0 ) {
if (byteOut = send(sd, sndBuf, byteIn, 0) < 0){
cout << "Error sending data...\n";
exit(1);
}
charOut += byteOut;
fragOut++;
}
byteIn = 0;
memset(&sndBuf, 0, BUFSIZE);
}
}
}

any help would be much appreciated.

Thanks

J

Jan 31 '07 #2
I made that change but again, the code hangs in the middle and it
doesn't do anything.

Jan 31 '07 #3

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

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
2
by: abdul bari | last post by:
What's the simplest way of sending XML from a client to a server? thanks abz
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
3
by: Sells, Fred | last post by:
I'm using MSW XP Pro with Python 2.4 to develop but production will be Linux with Python 2.3. (could upgrade to 2.4 if absolutely necessary) I can also switch to Linux for development if...
1
by: Eric Sheu | last post by:
Greetings, I have been searching the web like mad for a solution to my SMTP problem. I am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send out e-mails from a web site I have created to...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
2
by: Danny | last post by:
Hi all, Trying to send mail with System.Net.SmtpClient, using very simple code just for testing: SmtpClient smtp = new SmtpClient("mail.server.com", 25); smtp.Credentials = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.