I am trying to figure out sockets. I copied this code out of a book to
see if I could get it to work. However when bind() is called bind returns
a -1 indicating an error, what am I (or the book rather) is doing wrong?
The code follows:
#include <sys/types.h>
#include <sys/socket.h>
main()
{
int sd,ns;
char buf[256];
struct sockaddr sockaddr;
int fromlen;
if((sd=socket(AF_UNIX,SOCK_STREAM,0)) == -1)
{
puts("socket() returned an error.");
exit();
}
printf("the value of sd is %d \n",sd);
/*bind name - don't include null char in the name*/
printf("bind:%d\n",bind(sd,"sockname",sizeof("sockname")-1));
listen(sd,1);
for(;;)
{
ns=accept(sd,&sockaddr,&fromlen);
if(ns== -1)
{
printf("ns is -1\n");
exit();
}
else
{
if(fork()==0)
{
/*child*/
close(sd);
read(ns,buf,sizeof(buf));
printf("server read '%s'\n",buf);
exit(0);
}
close(ns);
}
}
The above is the server part of the socket. I added the checking for errors.Quote:}
I am currently using an Encore Multimax with runs an older BSD based UNIX.
Any suggestions?
--
C(o_o)) _ "The use of COBOL cripples the
(=) (_ -|- -|- is the mind; its teaching should,
--|-- language of choice. therefore, be regarded as a
/^\ Protest the use of BASIC! criminal offense." -E.W. Dijkstra
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-