467,888 Members | 1,338 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,888 developers. It's quick & easy.

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
  • viewed: 2168
Share:
2 Replies

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by abdul bari | last post: by
3 posts views Thread by Sells, Fred | last post: by
9 posts views Thread by Miro | last post: by
2 posts views Thread by Danny | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.