Unix Sockets

Started by
4 comments, last by Galapaegos 20 years, 5 months ago
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
-brad
Advertisement
What does the contents of the readline() function look like?
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
-brad
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.
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
If no packets had been sent, why would select() have triggered?

This topic is closed to new replies.

Advertisement