Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sending file over a socket

Reply
Thread Tools

Sending file over a socket

 
 
Sean
Guest
Posts: n/a
 
      01-31-2007
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

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-31-2007

"Sean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
 
 
 
Sean
Guest
Posts: n/a
 
      01-31-2007
I made that change but again, the code hangs in the middle and it
doesn't do anything.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
socket.sendall(), non-blocking sockets, and multi-threaded socket sending Tim Black Python 1 08-03-2004 01:11 PM



Advertisments