Thread: write and read system calls

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    write and read system calls

    Hello everyone.
    I am trying to write a server-client program (INET)
    I have all the connections ok but i have on e problem. When i try to pass binary data from the client to the server I dont know how to recognize whether I reached the EOF.

    in my code:
    ns is the file descriptor generated by accept() system call.
    BUFSIZE is 256
    buf is declared as char buf[BUFSIZE]
    and fp is my FILE pointer. (the one I want to write.)

    SERVER CODE(one part):
    Code:
    	
    while( read(ns, buf, BUFSIZE) ){ 
    		//printf("%s\n", buf); 
    		fwrite(buf,sizeof(char),BUFSIZE,fp);
    }
    In the client buf and BUFSIZE are similar to the ones declared in the server. fp is the FILE pointer to the file i am trying to read. sockfd is the file descriptor generated by socket() system call. and the code is:
    Code:
    	while(fread(buf,sizeof(char),BUFSIZE,fp)!=0){
    		write(sockfd, buf, BUFSIZE); 
    		//printf("%s\n", buf); 
    	}
    I should say that in this part of code the server's read() waits for the client's write().

    The problem is when i try to pass a file like this: (example.txt)
    Code:
    FILE FROM THE CLIENT 
    FILE FROM THE CLIENT
    FILE FROM THE CLIENT
    i have as a result:
    Code:
    FILE FROM THE CLIENT 
    FILE FROM THE CLIENT
    FILE FROM THE CLIENT????????m??c???t??t??`???Z?????????????????0`H?Z?Z?????????|?????????????`????1?#0?m???????????h??????m?????funcs
    And i suspect fwrite() function in the server needs something more.
    Help please and thanks in advance
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		write(sockfd, buf, BUFSIZE);
    You probably want to write the count of bytes from read() instead of "BUFSIZE", as you will be writing out whatever else happens to be in buffer this way, which is exactly what it looks like you are doing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    OHHHHH!! sorry!, such a mistake.
    so, should i get the number of bites read in an var (for example n) and for writing them i should write
    Code:
     write(sockfd, buf, n);
    is this correct?
    Mac OS 10.6 Snow Leopard : Darwin

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes. Although I would make sure it's "greater than zero", rather than "not equal to zero" too - assuming your bufsize isn't bigger than 2GB...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    thanks matsp
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: Unix System Calls ???
    By Paul22000 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 04:28 PM
  2. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  3. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM