Sending file over a socket

S

Sean

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
 
J

Jim Langston

Sean said:
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.
 
S

Sean

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top