troubles with sockets

troubles with sockets

Post by Chri » Fri, 30 Apr 1993 03:14:48



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);
                }
        }

Quote:}

The above is the server part of the socket.  I added the checking for errors.
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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

 
 
 

troubles with sockets

Post by Barry Margol » Fri, 30 Apr 1993 13:48:54



>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?

What book is this, so we can all avoid it?

Quote:>    printf("bind:%d\n",bind(sd,"sockname",sizeof("sockname")-1));

According to my manual, the second argument to bind(2) is a pointer to a
struct sockaddr, not a string.
--
Barry Margolin
System Manager, Thinking Machines Corp.



 
 
 

1. close() trouble with sockets

I'm running a small server on Red Hat Linux 7.0 / Kernel 2.2.16-22smp (2x
i686).

It's using sockets in non blocking mode, with SO_KEEPALIVE set to 1, and
SO_LINGER set to l_onoff=1, l_linger=0.

What happens, is that the server blocks in a close(socket) call sometimes.
It calls close, and doesn't return from it, it's blocking in there for some
reason.

If one runs gdb, you find that it's in the system call for close(). And if
you detach the program and let it keep running, since you knocked it out of
that blocking close call, it proceeds normally and runs fine again for a
while.

There's no breakpoints set, the program seems to stop randomly on that close
call.

What am I doing wrong?

I used to run this little server on Solaris-86 (2.6), and it ran smoothly
with no problems.

Aleksi Asikainen

2. Need help instaling RH 5.0 (odd problem with installer)

3. Trouble closing sockets....

4. problem parsing HTML::EmbPerl files on Apache

5. Trouble with sockets?

6. Pushme-pullyou

7. trouble with socket programming and select()

8. Library help for a newbie

9. Linux - trouble initializing sockets.

10. Having trouble with sockets? Read this...

11. Sockets sockets and more sockets

12. Socket, Socket, whose got the socket?

13. Socket, socket, who's got the socket?