[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Problemer med linux socket programmering



Allan Nielsen <sslug@sslug>, 2011-06-26 17:48 (+0200):

> Foelgende er et lille test program jeg har skrevet til at illusterer
> problemet:
>
> #include <sys/socket.h>
> #include <sys/types.h>
> #include <sys/wait.h>
> #include <linux/un.h>
> #include <unistd.h>
> #include <string.h>
> #include <stdio.h>
> #include <errno.h>
> #include <fcntl.h>
>
> struct sockaddr_un a;
> static const char * path = "/tmp/socket";
>
> int server() {
>     int fd = socket (AF_UNIX, SOCK_STREAM, 0);
>     fcntl(fd, F_SETFL, O_RDWR|O_NONBLOCK);
>
>     unlink(path);
>     bind(fd, (const struct sockaddr *)&a, sizeof(a));
>     listen(fd, 0);
>
>     sleep(2); // wait for client to connect
>     return 0;
> }

Jag förstår inte riktigt vad ditt problem är.

Du skall göra bind() och listen() tidigt i din server. Du gör bind() och
listen() på den socket du fått från socket(). Den backlog du sätter i
listen() vill du typiskt ha stor som möjligt, till exempel med
SOMAXCONN:

    if (0 != listen(listensock, SOMAXCONN))
    {
        ...
    }

Sedan skall du göra accept() (NB! Kommer att blocka!) eller möjligen
select() + accept() för att slippa blockeringen i accept(). Från
accept() får du då en *annan* socket som du kan göra read() från. Din
första socket skall fortsätta användas för att lyssna efter inkommande
koppel.

> Dette ville jeg ikke have forventet... Er der nogen work-around paa dette?

Du verkar ha missat något i beteendet hos systemanropen. Prova att läsa
manualsidorna.

Typisk enkel server:

  bind(listensock, (struct sockaddr *)&listenaddr, sizeof (struct sockaddr_in));
  listen(listensock, SOMAXCONN);

  ...

  /* Main loop. */
  for (;;)
  {
      FD_ZERO(&inset);
      FD_SET(listensock, &inset);

      /* Do FD_SET on all your client sockets as well. */
      /* Set maxfd to be max() of all your sockets */

      found = select(maxfd + 1, &inset, NULL, NULL, NULL);

      /* 
        Go through all your sockets and check if they're set, first the
        listening socket to find new connections 
      */

      if (FD_ISSET(listensock, &inset))
      {
          addrsize = sizeof (struct sockaddr_in);
          sock = accept(listensock, (struct sockaddr *)&clientaddr, &addrsize);
      }

      /* Then check all established client sockets with FD_ISSET()... */
   }

Nota bene! Det här skrev jag just nu som ett exempel. Helt otestat.

Kolla också på setsockopt() och SO_REUSEADDR som du troligen vill ha i
din server.

-- 
http://hack.org/mc/
Use plain text e-mail, please. OpenPGP welcome, 0xE4C92FA5.


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2011-08-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *