Problems with file descriptor


 
Thread Tools Search this Thread
Top Forums Programming Problems with file descriptor
# 1  
Old 04-29-2005
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
# 2  
Old 05-01-2005
Matteo,

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

Cheers!
# 3  
Old 05-03-2005
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;
}
# 4  
Old 05-03-2005
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.
# 5  
Old 05-03-2005
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
# 6  
Old 05-03-2005
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?
# 7  
Old 05-06-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

redirecting with file descriptor

hello, Someone can help me with redirectors? I am writing this script in bash enviroment on Fedora: exec 4<> /dev/tcp/10.10.11.30/5000 #open socket in input/output strings<&4 >file.txt & I send file descriptor 4 to string command to purge data stream from special char while come from... (3 Replies)
Discussion started by: rattoeur
3 Replies

2. Shell Programming and Scripting

Variable File Descriptor

Greetings. I am happily using constructs like the following; I have set -x to show fine distinctions. $ exec 4> afile + exec + 4> afile $ print -u4 This is the first line in afileAs you can see from the -x expansion, the shell performed the exec command and redirected file descriptor to... (4 Replies)
Discussion started by: rpaskudniak
4 Replies

3. Shell Programming and Scripting

Help with File Descriptor in a While loop

Hi, I am trying to read a file line-by-line in a while loop, and perform some tasks which involves non-interactive SSH to a remote server. The code looks something like this -- #!/usr/bin/ksh export myFile=/path/to/my/file.load while read line do do something ## Adding the SSH... (2 Replies)
Discussion started by: Subu1987
2 Replies

4. Shell Programming and Scripting

file descriptor KSH

Hello, How can i use file descriptor in a script to read 2 files at the same time and extract line 200 from file 1 and line 500 from file 2. Thanks. (6 Replies)
Discussion started by: LiorAmitai
6 Replies

5. UNIX for Dummies Questions & Answers

File Descriptor

Hi What the below path contains? /proc/<pid>/fd (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

6. Shell Programming and Scripting

File Descriptor

Hello All, Im opening a file desciptor in perl and sending data using print CMD "$xyz". is there a limit to the length of the string that I can give to this CMD at a time. (3 Replies)
Discussion started by: rimser9
3 Replies

7. UNIX for Advanced & Expert Users

File Descriptor Table

Im working on writing a small operating system. I am currently working on implementing dup, dup2, pipe, and close and I need to implement some type of file descriptor table in my PCB. I was wondering if there is anyone who is familiar with linux/unix implementation of these tables who could... (6 Replies)
Discussion started by: Ashaman0
6 Replies

8. Programming

File descriptor constant

I have a requirement to close all the file descriptors from 3 to 1024 for a particular application. Right now, this is how I do it .. for ( int i = 3 ; i <= 1024; ++i ) close(i); The change I am looking at is, I want to do away with the number 1024 and replace it with a constant which... (4 Replies)
Discussion started by: vino
4 Replies

9. UNIX for Dummies Questions & Answers

File Descriptor Help

What is a file descriptor in Unix?? How to find a file descriptor of a file in Unix?? Does it have anything to do with the Inode numbers?? (3 Replies)
Discussion started by: rahulrathod
3 Replies

10. UNIX for Dummies Questions & Answers

file activity (open/closed) file descriptor info using KORN shell scripting

I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies
Login or Register to Ask a Question