Archived

This topic is now archived and is closed to further replies.

Unix Sockets

This topic is 5145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I''ve got a problem I just can''t figure out. For a project, I need to write a client/server file transfer program where the client connects to a server to recieve files from it. My problem is sending the files. I am sending the file from the server through a call like this:
fstream f;
f.open (buffer, ios::in|ios::binary);

while (!f.eof ())
{
  char temp[100];
  f.read (temp, 100);

  writen (isock, temp, 100); // this is a wrapper for write

}

f.close ();
 
As far as I can tell, the server is sending everthing just fine. The client doesn''t recieve anything though. Here is how I''m receiving the data:
        
fstream f;
f.open (buffer, ios::out|ios::binary);

int i = 1;

while (i == 1)
{
  char temp[100];
  int iread = readline (isock, temp, 100);

  if ((iread == 0) || (iread == -1))
    i = 0;

  f.write (temp, 100);
}

f.close ();
 
The readline function is a wrapper that was provided. I added a select routine to determine if anything recieved on the socket should be reported. When this routine hits the read function, it blocks the client process. Does anyone know how to get this data recieved on the client? Any help is grately appreciated! -brad

Share this post


Link to post
Share on other sites
Heres the readline function:

int readline (int fd, char *ptr, int maxlen)
{
int n, rc;
char c;

for (n = 1; n < maxlen; n++)
{
rc = read (fd, &c, 1);

if (rc == 1)
{
*ptr++ = c;
if (c == ''\n'')
break;
}
else if (rc == 0)
{
if (n == 1)
return 0;
else
break;
}
else
return -1;
}

*ptr = 0;
return n;
}

-brad

Share this post


Link to post
Share on other sites
Are these blocking sockets?

Depending on what your writen() function is doing, what might be happening is that readline() is desyncing on the number of bytes received due to an off by one error in the for loop, causing it to wait on input that hasn''t been sent.

Share this post


Link to post
Share on other sites
It could be that the data has not yet been sent and is still waiting in the send buffer until there are enough data to make it worth sending a packet.

See "Nagle''s algorithm" and the TCP_NODELAY socket option.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites