The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File Descriptor Table Ashaman0 UNIX for Advanced & Expert Users 6 12-03-2007 11:54 AM
File descriptor constant vino High Level Programming 4 05-17-2005 10:18 PM
File Descriptor Help rahulrathod UNIX for Dummies Questions & Answers 3 10-14-2004 02:08 AM
file activity (open/closed) file descriptor info using KORN shell scripting Gary Dunn UNIX for Dummies Questions & Answers 3 06-07-2004 10:54 AM
bad file descriptor? ftb UNIX for Dummies Questions & Answers 1 02-20-2002 04:19 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-29-2005
teo teo is offline
Registered User
 

Join Date: Mar 2005
Posts: 8
Problems with file descriptor

Hi,
look at the following code:
The client after estabilishing a connection with the server does the following:
if ((peter = fopen(argv[1], "r")) == NULL){
printf("errore\n");
exit(0);
}

send_file(peter, sockfd); /* transfers the file to the server */
fclose(peter);
printf("File spedito\n");

read(sockfd, (char *) &res, sizeof(res)); /* here it waits for a receipt */
res = ntohs(res);
if(res == RECEIPT)
printf("RECEIPT received\n");
else
printf("RECEIPT not received\n");

close(sockfd);
}

The server after accepting the connection does the following:
if ((fp = fopen("transazione", "w")) == NULL){
printf("errore\n");
exit(0);
}
get_file(fp, connfd); /* obtain the file from the client */
fclose(fp);
printf("File received\n");

shutdown(connfd, SHUT_RD);
res = htons(RECEIPT);
writen(connfd, (char *) &res, sizeof(res));
printf("RECEIPT sent\n");
}
The whole program compile but when I try to execute it the client acrtually send the file and blocks on the read call on the socket and the server bloks on the get file function, never returning? Anyone can help?
By the way the two function get_file and send_file read from the socket and write to the file(get_file) or the opposite(read from the file e write to the socket, send_file)
Thanx in advance!!!
Matteo
Reply With Quote
Forum Sponsor
  #2  
Old 04-30-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
Matteo,

It would help if you posted the code for the getfile function.

Cheers!
Reply With Quote
  #3  
Old 05-03-2005
teo teo is offline
Registered User
 

Join Date: Mar 2005
Posts: 8
Sorry...
here it goes the getfile and sendfile function:

void
get_file(FILE *fp, int sockfd){
int n;
char buf[MAXSIZE];

/* read from the socket until i receive EOF */
while ((n = recv(sockfd, buf, MAXSIZE, MSG_WAITALL)) > 0){
if (fwrite(buf, sizeof(char), n, fp) != n){
printf("error\n");
exit(0);
}
}
return;
}

void
send_file(FILE *fp, int sockfd){
int n;
char buf[MAXSIZE];

/* write the file on socket */
while ((n = fread(buf, sizeof(char), MAXSIZE, fp)) > 0){
if (writen(sockfd, buf, n) != n){
printf("socket error\n");
exit(0);
}
}
return;
}
Reply With Quote
  #4  
Old 05-03-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
MSG_WAITALL is a new one on me. Looking it up with google I see: "If your system supports MSG_WAITALL, recv() will not return until required length of data is recieved entirely". I didn't see where MAXSIZE is defined, so let's say it is 1024. It sounds to me like recv must block until it gets 1024 bytes. But what if the file is 1025 bytes? The first recv would work. But the second recv would get the final byte and wait forever for 1023 more bytes.

But again, I have no experience with this MSG_WAITALL flag, so I'm not sure if this analysis is correct. Easy to test though... Try sending a file that is a multiple of MAXSIZE in length.
Reply With Quote
  #5  
Old 05-03-2005
teo teo is offline
Registered User
 

Join Date: Mar 2005
Posts: 8
MSG_WAITALL beaves exactly as you said, wait for the exact amount of data you have requested, but can return fewer data if an error occur, a signal is caught, or for connection shutdown. In the latter case it returns 0, but beaves the same(return 0) also when an EOF is reached. In my case when the EOF is reached(that means the sender file's is finished) recv returns 0 and break the loop.
It would work properly if i didn't use the code that follow getfile sendfile in client and server code, but I don't know why when I try to send again data after the getfile call the client blocks!
Hope someone can help me.
Thanx for the attention!
Ciao
Reply With Quote
  #6  
Old 05-03-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Why would the socket see an EOF? You do a bunch of writen() calls. I don't know what writen is. Does it somehow know when the last writen occurred and do a half close on the socket?
Reply With Quote
  #7  
Old 05-06-2005
teo teo is offline
Registered User
 

Join Date: Mar 2005
Posts: 8
Don't mind about writen you can get rid of it and replace with write, it will work fine. When read from the file and gets EOF the send_file function send it through the socket to get_file and recv return 0 as if it were interrupted or shutdown the connection. It is a normal beavior I think...
Comments?
Ciao
Matteo
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0