LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2010, 09:12 AM   #16
xixonga
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0

thanks i will try your solution.
 
Old 11-28-2010, 12:20 PM   #17
xixonga
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0
Quote:
Originally Posted by dwhitney67 View Post
You are not using the return value from fread(). This return value indicates whether you read any data, and if so, how many bytes.

You should be doing something like this for the "uploader":
Code:
char   buffer[1024];
size_t bytes = 0;

while ((bytes = fread(buffer,sizeof(char), sizeof(buffer),fp)) > 0)
{
   send(sockfd, buffer, bytes, 0);
}
Btw, bzero() was deprecated as of POSIX-2001. You do not need to use it, but if you did need similar functionality, you should use memset().

For the receiver:
Code:
char    buffer[1024];
ssize_t bytes = 0;

while ((bytes = recv(sock, buffer, sizeof(buffer), 0)) > 0)
{
   fwrite(buffer, sizeof(char), bytes, fp);
}
P.S. The code above is sorely missing a lot of error checking.


thank you dwhitney...you were right. in fact the problem was the last piece of chunk.it was always sending 150 bytes, including garbage...
thank you all for the help

Best regards
 
Old 11-28-2010, 04:19 PM   #18
xixonga
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0
hi sorry but only know i have noticed that is something wrong with that code dwhitney...

ther receiver never stops... it's a no end cycle...because the receiveir won't know when to stop receiving data.

is there any way around this? i want both while cycles to end when the transfer is done.
 
Old 11-28-2010, 07:06 PM   #19
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Have you considered closing the client (receiver) socket on the "uploader" side? Once the entire file has been sent to the receiver, it would seem like the logical thing to do.

Code:
...

char   buffer[1024];
size_t bytes = 0;

while ((bytes = fread(buffer,sizeof(char), sizeof(buffer),fp)) > 0)
{
   send(sockfd, buffer, bytes, 0);
}

fclose(fp);
close(sockfd);
Btw, you should avoid using recv() from a non-blocking socket, unless you either 1) set a maximum timeout period to some sane setting, such as 2-3 seconds, or 2) use select() before attempting to call recv().

When the "uploader" closes the client socket, recv() should fail, but at this late hour, I'm not 100% sure of that. Thus I would recommend that you at least try using select(). I know that select() returns a 0 (zero) when the socket has been closed.

Code:
...

struct timeval timeout = {1, 0};   // one second

while (activityDetected(sock, timeout) != 0)
{
   char    buffer[1024];
   ssize_t bytes = recv(sock, buffer, sizeof(buffer), 0);

   if (bytes > 0)
   {
      fwrite(buffer, sizeof(char), bytes, fp);
   }
}
Code:
int activityDetected(int sd, struct timeval timeout)
{
  fd_set readfds;
  FD_ZERO(&readfds);
  FD_SET(sd, &readfds);

  int sel = -1;

  while (sel < 0)
  {
    sel = select(sd + 1, &readfds, 0, 0, &timeout);

    if (sel >  0)
    {
      return FD_ISSET(sd, &readfds);
    }

    if (sel < 0 && errno != EINTR)
    {
      return 0;
    }
  }

  /* timeout */
  return 0;
}

Last edited by dwhitney67; 11-28-2010 at 07:20 PM.
 
Old 11-29-2010, 06:51 AM   #20
xixonga
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0
but the thing is that i wan't to receive one last data from the server, after uploading the file...i can't close the socket.
i'm trying to send 0 bytes to the server, so he can break the while cycle, but i can't make that work...
 
Old 11-29-2010, 07:33 AM   #21
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by xixonga View Post
but the thing is that i wan't to receive one last data from the server, after uploading the file...i can't close the socket.
i'm trying to send 0 bytes to the server, so he can break the while cycle, but i can't make that work...
What is it that you want the uploader (server) to send to the client? How will the client know that this is a status message from the server versus some data part of the file? Unless you have set up a particular protocol that each understands, you are going to have trouble with your requirements.

Consider defining a protocol; for example
Code:
enum MsgType
{
   REQUEST,
   DATA,
   DONE
};

struct Message
{
   MsgType        type;
   int            status;    // 1 = ok, 0 = bad
   size_t         dataSize;  // number of bytes of data
   unsigned char* data;
};
Here, you define the type of data that is being flung around. For instance, the client would initially send the uploader (server) a message containing {REQUEST, 1, namesize, <filename>}. Then the server would respond; if the file is found, then one or more {DATA, 1, datasize, data} messages are sent. If the file is not found, or the data transmission has completed, then the server can send a {DONE, status, 0, 0} message, depending on the outcome of the request.

Anyhow, this is merely something to consider. You are free to define your own protocol. Just remember that with structs, you will need to serialize them before sending the data, and then deserialize when receiving it. You can Google for information on these subjects.

Last edited by dwhitney67; 11-29-2010 at 07:36 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Raw socket programming with C arabindav Programming 17 06-09-2011 11:58 AM
raw socket programming bigboss360 Linux - Networking 2 10-17-2009 09:11 AM
Socket Programming Send File htabesh Programming 11 06-23-2009 12:52 PM
Help with raw socket programming tuxfood Programming 2 07-25-2005 01:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration