Link to home
Create AccountLog in
Avatar of loki982
loki982

asked on

linux tcp sockets help

hello all:
i am trying to send a tar file across a socket and i am getting anything but consistant results...

client:
      /* read tar file */
      while (!feof(fptr)) {
            tar_dat[count] = fgetc(fptr);
            count++;
      }
      fclose(fptr);

      /* send tar file */
      for(i=0;i<count-1;i++) {
            c = tar_dat[i];
            sendto(sockfd,&c,sizeof(c),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
            n=recvfrom(sockfd,&r,sizeof(r),0,NULL,NULL);
      }


server:
      do {
            bzero(&c,sizeof(c));
            n = recvfrom(s, &c, sizeof(c), 0, (struct sockaddr *) &client_addr, client_socklen);
            /* error */
            if(n < 0) {
                  exit(1);
            }
            /* write */
            if(n > 0) {
                  sendto(s, &c, sizeof(c), 0, (struct sockaddr *) &client_addr, sizeof(client_addr));
                  fputc(c, fptr);
            }
      } while(n > 0);
      fclose(fptr);

the client always completes its portion of the code but the server is another story. some times it exits, other times it breaks from the do and still others i get a Broken pipe when the client moves on... i am using md5sum to check the files and they are inconsistant as well (though sometimes correct). am i perhaps to use different flags in the send & recieve functions? i was also wondering about using recvfrom and sendvs read and send what is the difference between the two sets?

1000 thanx in advance
~loki
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

what type of socket are you using - for this type of task you should use SOCK_STREAM. it looks to me like you are using a DATAGRAM socket which has a fixed upper limit on packet size.

when using a stream socket both send and receive can return less than the buffer size you specify - they both return the number of bytes sent so you may need to loop working your way through your buffer.
Avatar of grg99
grg99

Is this a TCP or UDP connection?

If it's UDP, delivery isnt guaranteed.  The packets may arrive, or may get lost, or may arrive out of original ordering.

  You're also sending one byte per packet, which is very inefficient, as each packet has at least 40 bytes of header.  Better send hundreds of bytes at a time.

Also you're sending back an ACK packet it looks like for each bte received.  The AQCk packets can get lost, just as the data packets can, so this code will hang on the first packet dropped.

Also you're waiting for the ACK before sending the next byte, which is going to lead to very very very slow data transfer, maybe 1/10000th of the average rate.

You need to do some major rethinking of your algorithm, taking into account the good and bad properties of TCP/IP.
USE TCP
Avatar of loki982

ASKER

i am using tcp (i think):
SOCK_STREAM means tdc yes?

sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(32000);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
ASKER CERTIFIED SOLUTION
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.