Socket programming in C..trouble with send and receive

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sritejv
    New Member
    • Mar 2008
    • 9

    Socket programming in C..trouble with send and receive

    Hi i have trouble with the following piece of code.The sender sends a file ,a line at a time to the rx.After sending the file the sender closes the socket,therby closing the connection.But rx is not able to receive the entire file.The sender sender sends the file correctly.But the receiver is not able to catch up.The rest of the program works fine.The program doesnt stall.

    fp=fopen(buf,"r "); //open file
    while(fgets(fra me,70,fp)!=NULL )
    {
    c=send(s,frame, strlen(frame)+1 ,0);
    printf("%s frame sent\n",frame);
    //memset(frame,0, 70);
    }
    sleep(2);
    close(i); //close socket after sending file.. initiate break
    FD_CLR(i,&maste r);

    receiver:

    while(1)
    {
    printf("receivi ng file \n");
    c=recv(soc,buff er,70,0);
    printf("receive d (file bytes) % d \n",c);
    if(c<=0) //responds to closure of connection
    {
    close(soc);
    printf("closed socket \n");
    break;
    }
    else
    {
    fp=fopen(query, "a+");
    fprintf(fp,"%s ",buffer);
    printf("%s ",buffer);
    fclose(fp);
    memset(buffer,0 ,70);
    }

    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The sender should send one record and stop. The reveiver should process the record and signal the send that it's OK to send the next record.

    Comment

    Working...