Linux-Bulgaria.ORG
навигация

 

начало

пощенски списък

архив на групата

семинари ...

документи

как да ...

 

 

Предишно писмо Следващо писмо Предишно по тема Следващо по тема По Дата По тема (thread)

lug-bg: fork, programirane


  • Subject: lug-bg: fork, programirane
  • From: victor.bg@xxxxxxxxxxxx (victor.bg@xxxxxxxxxxxx)
  • Date: Wed, 03 May 2000 22:25:05 +0300



 Zdraveite,
Niakoi da znae dobur tutorial za UNIX programirane, samo specificnite za
UNIX nesta, procesi, pipes i t.n. Vsusnost, izteglih edin example za
socket i tam programata raboteshe ok, samo ce ne exitva i ne mi e iasno
zasto i iskam da proceta nesto po vuprosa. Mislia, ce vuv fork()vaneto e
problema. Ako niakoi go interesuva dolu e source code

// sledva koda

       #include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>

#define BACKLOG 10 // How many pending connections queue will hold

main (int argc, char* argv[])
{
        int myPort;

 int sockfd, new_fd; // Listen on sock_fd, new connections on new_fd
 struct sockaddr_in my_addr; // my address information
 struct sockaddr_in their_addr; // connector's address information
 int sin_size;

 myPort = atoi(argv[1]);
 if ((sockfd = socket(AF_INET,  SOCK_STREAM, 0)) == -1)
 {
  perror("bind");
  exit(1);
 }

 my_addr.sin_family = AF_INET;  // host byte order
 my_addr.sin_port = htons(myPort);  // short, network byte order
 my_addr.sin_addr.s_addr = INADDR_ANY;  // auto fill with my ip
 bzero(&(my_addr.sin_zero), 8);  // zero the rest of the struct

 if (bind (sockfd, (struct sockaddr *) &my_addr, sizeof(struct
sockaddr)) == -1)
 {
  perror("bind");
  exit(1);
 }

 if (listen (sockfd, BACKLOG) == -1)
 {
  perror("listen");
  exit(1);
 }

 while(1)
 { /* main accept loop */

  sin_size = sizeof(struct sockaddr_in);
  if((new_fd = accept (sockfd, (struct sockaddr *)&their_addr,
&sin_size)) == -1)
  {
   perror ("accept");
   continue;
  }

  printf("server: got connection from %s\n", inet_ntoa
(their_addr.sin_addr));

               if (!fork())
               { /* this is the child process */
                if (send (new_fd, "Hello, world! \n" , 14, 0) == -1 )
                {
                 perror ("send");
                 close(new_fd);
                 exit(0);
                }
                close(new_fd); /* parent doesn't need this */

                while (waitpid(-1, NULL, WNOHANG) > 0 ); // clean up
child process

         }
  }

   }

==================================================================
A mail-list of Linux Users Group - Bulgaria (bulgarian linuxers)
Otpiswaneto RABOTI !!! : Majordomo@xxxxxxxxxxxxxxxxxx UNSUBSCRIBE LUG-BG
http://www.linux-bulgaria.org/ Hosted by Internet Group Ltd. - Stara Zagora



 

наши приятели

 

линукс за българи
http://linux-bg.org

FSA-BG
http://fsa-bg.org

OpenFest
http://openfest.org

FreeBSD BG
http://bg-freebsd.org

KDE-BG
http://kde.fsa-bg.org/

Gnome-BG
http://gnome.cult.bg/

проект OpenFMI
http://openfmi.net

NetField Forum
http://netField.ludost.net/forum/

 

 

Linux-Bulgaria.ORG

Mailing list messages are © Copyright their authors.