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(servadd r));
n=recvfrom(sockfd,&r,sizeo f(r),0,NUL L,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
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)
n=recvfrom(sockfd,&r,sizeo
}
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
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.
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
ASKER
i am using tcp (i think):
SOCK_STREAM means tdc yes?
sockfd=socket(AF_INET,SOCK _STREAM,0) ;
bzero(&servaddr,sizeof(ser vaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=i net_addr(a rgv[1]);
servaddr.sin_port=htons(32 000);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
SOCK_STREAM means tdc yes?
sockfd=socket(AF_INET,SOCK
bzero(&servaddr,sizeof(ser
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=i
servaddr.sin_port=htons(32
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.