Sending file over a socket

Discussion in 'C++' started by Sean, Jan 31, 2007.

  1. Sean

    Sean Guest

    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
     
    Sean, Jan 31, 2007
    #1
    1. Advertising

  2. Sean

    Jim Langston Guest

    "Sean" <> wrote in message
    news:...
    > 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
    >
     
    Jim Langston, Jan 31, 2007
    #2
    1. Advertising

  3. Sean

    Sean Guest

    I made that change but again, the code hangs in the middle and it
    doesn't do anything.
     
    Sean, Jan 31, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Liße
    Replies:
    1
    Views:
    1,390
    Knute Johnson
    Feb 23, 2004
  2. Tim Black
    Replies:
    1
    Views:
    1,098
    Alan Kennedy
    Aug 3, 2004
  3. Bill Seitz
    Replies:
    18
    Views:
    1,980
    Bill Seitz
    Sep 16, 2004
  4. Replies:
    1
    Views:
    318
    Alf P. Steinbach
    Jul 9, 2006
  5. Jeffrey Barish
    Replies:
    2
    Views:
    282
    Jeffrey Barish
    Feb 12, 2008
Loading...

Share This Page