lkml.org 
[lkml]   [1998]   [Aug]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Subjectrecv() error in 2.1.116pre1?
Date
From
While researching another problem, I found an issue with a different code 
segment that works fine under 2.0.x. The dumbed down version attached to this
message will get a socket, and then fork a second process (sender/receiver).
The processes will connect, send the file "input" to the process (writing to
"output"), and then exit. Really simple. Works great on the 2.0.35 kernel
that I wrote it on. With at least 2.1.116pre1 though, I *always* get (through
perror on the recv() call):

Resource temporarily unavailable

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define oops(msg) { perror(msg); exit(1); }

#ifndef MSG_WAITALL /* Most Systems don't tend to have this ... */
#define MSG_WAITALL 0x40 /* wait for full request or error */
#endif

int main(int argc, char *argv[]) {
FILE *file;
unsigned char buffer[64];
int s, fd, port=4356, value, pid;
struct sockaddr_in saddr;
struct hostent *hp;

memset(&saddr,0,sizeof(saddr));
saddr.sin_family=AF_INET;
hp=gethostbyname("localhost");
memcpy(&saddr.sin_addr,hp->h_addr,hp->h_length);
saddr.sin_port=htons(port);

pid = fork(); /* child = sender, parent = receiver */

if ( !pid ) {
sleep(1); /* sleep for 1 second so receiver can start up */
if ( (file=fopen("input","rb")) == NULL )
oops("fopen");
}
else {
if ( (file=fopen("output","wb")) == NULL )
oops("fopen");
}

if ( (s=socket(AF_INET, SOCK_STREAM, 0)) == -1 )
oops("socket");

if ( !pid ) {
if (connect(s,(struct sockaddr *)&saddr,sizeof(saddr)) !=0)
oops("connect");
while( (value=fread(buffer,1,64,file)) > 0 )
if ( send(s,buffer,value,0) < 0 )
perror("send");
}
else {
if (bind(s,(struct sockaddr *)&saddr,sizeof(saddr)) !=0)
oops("bind");
if (listen(s,1)!=0)
oops("listen");
if ( (fd=accept(s,NULL,NULL)) == -1 )
oops("accept");
while ( (value=recv(fd,buffer,64,MSG_WAITALL))>0 )
fwrite(buffer,1,value,file);
if ( value == -1 )
oops("recv");
}

fclose(file);

return 0;
}
Theo Van Dinter UNIX Systems Administrator
Chrysalis Symbolic Design 978/436-9911 x163
\
 
 \ /
  Last update: 2005-03-22 13:44    [W:0.035 / U:0.892 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site