Ejemplo n.º 1
0
int main(int argc, char *argv[]){
	int sockfd, newsockfd, portno, clilen;
	char buffer[256];
	struct sockaddr_in serv_addr, cli_addr;
	int n, pid;

	sockfd = socket(AF_INET, SOCK_STREAM, 0);

	if (sockfd<0){
		perror("ERROR OPENING SOCKET");
		exit(1);
	}

	bzero((char *) &serv_addr, sizeof(serv_addr));
	portno = 5001;

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(portno);

	if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
		perror("ERROR ON BINDING");
		exit(1);
	}

	listen(sockfd,5);
	clilen = sizeof(cli_addr);

	while(1){
		newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

		if(newsockfd < 0){
			perror("ERROR ON ACCEPT");
			exit(1);
		}

		pid = fork();
		
		if(pid < 0){
			perror("ERROR ON FORK");
			exit(1);
		}

		if(pid == 0){
			close(sockfd);
			doprocessing(newsockfd);
			exit(0);
		}

		else{
			close(newsockfd);
		}
	}
	return 0;
}	
Ejemplo n.º 2
0
int main( int argc, char *argv[] ) {
    int sockfd, newsockfd, portno, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int  n;
	int pid;
    /* First call to socket() function */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("ERROR opening socket");
        exit(1);
    }
    /* Initialize socket structure */
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 5001;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
 
    /* Now bind the host address using bind() call.*/
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
         perror("ERROR on binding");
         exit(1);
    }
    /* Now start listening for the clients, here 
     * process will go in sleep mode and will wait 
     * for the incoming connection
     */
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    while (1) {
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0) {
            perror("ERROR on accept");
            exit(1);
        }
        /* Create child process */
        pid = fork();
        if (pid < 0) {
            perror("ERROR on fork");
	    	exit(1);
        }
        if (pid == 0) {
            /* This is the client process */
            close(sockfd);
            doprocessing(newsockfd);
            exit(0);
        }
        else {
            close(newsockfd);
        }
    } /* end of while */
}
Ejemplo n.º 3
0
int main(void)
{
	struct sockaddr_in servaddr, cliaddr;

	int listenfd, connfd, pid;
	socklen_t cliaddr_len = sizeof(cliaddr);


	// first call to socket() function
	listenfd = socket(AF_INET, SOCK_STREAM, 0);
	if (listenfd < 0) {
		perror("Error opening socket!");
		exit(-1);
	}	

	// initialize socket structure
	memset(&servaddr, 0, sizeof(struct sockaddr_in));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = inet_addr("222.31.79.131");
	servaddr.sin_port = htons(SERV_PORT);	

	bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

	// start listening for the clients
	listen(listenfd, 2000);

	printf("王正一的server服务器开始等待客户端连接 ...\n");

	while (1) {
		// 三次握手完成后,服务器调用accept()接受客户端连接
		// accept()参数:
		// sockfd = listenfd, 服务器端监听的套接字描述符
		// cliaddr = &cliaddr, 指向sockaddr结构体指针,客户端的地址信息
		// addrlen = &cliaddr_len,确定客户端地址结构体的大小
		connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);

		pid = fork();

		if (pid == -1) {
			perror("call to fork");
			exit(-1);
		} else if (pid == 0) {
			close(listenfd);
			doprocessing(connfd, cliaddr);
			exit(0);
		} else {
			close(connfd);
		}
	}

	return 0;
}
Ejemplo n.º 4
0
int main()
{
    initW32(); // Necesaria para compilar en Windows
    int fd, fd2; // los descriptores de archivos

    struct sockaddr_in server; // para la información de la dirección del servidor
    struct sockaddr_in client; // para la información de la dirección del cliente

    unsigned int sin_size;
    pid_t pid;

    if ((fd=socket(AF_INET, SOCK_STREAM, 0)) == -1 )
        {
            printf("error en socket()\n");
            exit(-1);
        }

    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.s_addr = INADDR_ANY; // INADDR_ANY coloca nuestra dirección IP automáticamente */

   //bzero(&(server.sin_zero),8);
   memset(&(server.sin_zero), '0', 8);// escribimos ceros en el resto de la estructura


   if(bind(fd,(struct sockaddr*)&server, sizeof(struct sockaddr))==-1) // A continuación la llamada a bind()
    {
      printf("error en bind() \n");
      exit(-1);
    }

   if(listen(fd,BACKLOG) == -1)
    {  // llamada a listen()
      printf("error en listen()\n");
      exit(-1);
    }

   while(1)
    {
      sin_size=sizeof(struct sockaddr_in);
      if ((fd2 = accept(fd,(struct sockaddr *)&client, &sin_size))==-1)// A continuación la llamada a accept()
        {
         printf("error en accept()\n");
         exit(-1);
        }

      printf("Connecting with Java: %s\n", inet_ntoa(client.sin_addr) );// que mostrará la IP del cliente
      send(fd2,"Server_N",99,0);//Muestra que enviará el mensaje de bienvenida al cliente
      doprocessing(fd2);

   }
}
Ejemplo n.º 5
0
int main() {

  initW32(); /* Necesaria para compilar en Windows */
  int fd, fd2; /* los descriptores de archivos */
  struct sockaddr_in server; /* para la información de la dirección del servidor */
  struct sockaddr_in client;/* para la información de la dirección del cliente */

  unsigned int sin_size;

  pid_t pid;

  /* A continuación la llamada a socket() */
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("error en socket()\n");
    exit(-1);
  }

  server.sin_family = AF_INET;
  server.sin_port = htons(PORT);
  server.sin_addr.s_addr = INADDR_ANY;/* INADDR_ANY coloca nuestra dirección IP automáticamente */
  //bzero(&(server.sin_zero),8);

  memset(&(server.sin_zero), '0', 8); /* escribimos ceros en el reto de la estructura */

  /* A continuación la llamada a bind() */
  if (bind(fd, (struct sockaddr*) &server, sizeof(struct sockaddr)) == -1) {
    printf("error en bind() \n");
    exit(-1);
  }

  if (listen(fd, BACKLOG) == -1) { /* llamada a listen() */
    printf("error en listen()\n");
    exit(-1);
  }

  while (1){
    sin_size = sizeof(struct sockaddr_in);
    /* A continuación la llamada a accept() */
    if ((fd2 = accept(fd, (struct sockaddr *) &client, &sin_size)) == -1) {
      printf("error en accept()\n");
      exit(-1);
    }

    printf("Se obtuvo una conexion desde %s\n", inet_ntoa(client.sin_addr));
    /* que mostrará la IP del cliente */
    //send(fd2,"Bienvenido a mi servidor Lain.\n",99,0);
    /* que enviará el mensaje de bienvenida al cliente */
    doprocessing(fd2);

  } /* end while */
}
Ejemplo n.º 6
0
void *operation(void *port) {
   int sockfd, newsockfd, portno, clilen;
   char buffer[256];
   struct sockaddr_in serv_addr, cli_addr;
   int n, pid;

   /* First call to socket() function */
   sockfd = socket(AF_INET, SOCK_STREAM, 0);

   if (sockfd < 0) {
      perror("ERROR opening socket");
      exit(1);
   }

   /* Initialize socket structure */
   bzero((char *) &serv_addr, sizeof(serv_addr));
   portno = *((int *) port);

   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;
   serv_addr.sin_port = htons(portno);

   /* Now bind the host address using bind() call.*/
   if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
      perror("ERROR on binding");
      exit(1);
   }


   /* Now start listening for the clients, here
      * process will go in sleep mode and will wait
      * for the incoming connection
   */

   listen(sockfd,5);
   clilen = sizeof(cli_addr);
   newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
   printf("server is spawn for port %d\n", portno);

   struct sockaddr *id = 0;
   while (1) {
         int stop = doprocessing(newsockfd);
		 if (stop) break;
   } /* end of while */
   close(sockfd);
}
Ejemplo n.º 7
0
/**
* Simulate a PMU server/dispatcher C37.118-2011
*/
int main( int argc, char *argv[] )
{
    int sockfd, newsockfd, portno, clilen;
    int pid;
    char buffer[SIZE_BUFFER];
    struct sockaddr_in serv_addr, cli_addr;
    int  n;

    CONFIG_Frame *my_config2 = new CONFIG_Frame();
    CONFIG_1_Frame *my_config1 = new CONFIG_1_Frame();
    DATA_Frame *my_dataframe = new DATA_Frame(my_config2);
    HEADER_Frame *my_header = new HEADER_Frame("PMU VERSAO 1.0 LSEE");
    
    // Create config packet
    my_config2->IDCODE_set(7);
    my_config2->SOC_set(1149577200);
    my_config2->FRACSEC_set(1443303576);
    my_config2->TIME_BASE_set(1000000);
    my_config2->DATA_RATE_set(15);
    
    my_config1->IDCODE_set(7);
    my_config1->SOC_set(1149577200);
    my_config1->FRACSEC_set(1443303576);
    my_config1->TIME_BASE_set(1000000);
    my_config1->DATA_RATE_set(15);
    
    my_dataframe->IDCODE_set(7);
    my_dataframe->SOC_set(1149577200);
    my_dataframe->FRACSEC_set(0);
 
    //Create a ONE pmu instance
    //name, idcode, freq,analog,phasor,coord
    // true = float		    
    // coord 0 = rect
    PMU_Station pmu ("PMU LSEE",666,false,true,true,true);
    PMU_Station pmu2 ("PMU2 LSEE",667,false,false,false,false);

    // 2 phasors
    pmu.PHASOR_add("FasorTensaoA",915527,VOLTAGE);
    pmu.PHASOR_add("FasorCorrenteA",45776,CURRENT);
    pmu.ANALOG_add("Ana_TensaoA",100,0);
    pmu.ANALOG_add("Ana_CorrenteA",100,PEAK_ANALOG_INPUT);
    vector<string> namesdig;
    namesdig.push_back("Bit0");
    namesdig.push_back("Bit1");
    namesdig.push_back("Bit2");
    namesdig.push_back("Bit3");
    namesdig.push_back("Bit4");
    namesdig.push_back("Bit5");
    namesdig.push_back("Bit6");
    namesdig.push_back("Bit7");
    namesdig.push_back("Bit8");
    namesdig.push_back("Bit9");
    namesdig.push_back("Bit10");
    namesdig.push_back("Bit11");
    namesdig.push_back("Bit12");
    namesdig.push_back("Bit13");
    namesdig.push_back("Bit14");
    namesdig.push_back("Bit15");
    
    pmu.DIGITAL_add(namesdig,0x0000,0xFFFF);
    // Set nominal Freq
    pmu.FNOM_set(FN_60HZ);        
    // default config = 0;
    pmu.CFGCNT_set(1);
    pmu.STAT_set(2048);
    pmu.PHASOR_VALUE_set(Complex(3E4,3E4),0);
    pmu.PHASOR_VALUE_set(Complex(1E4,1E4),1);
    pmu.ANALOG_VALUE_set(34.15,0);
    pmu.ANALOG_VALUE_set(-100.10,1);
    pmu.DIGITAL_VALUE_set(true,0,1);
    pmu.DIGITAL_VALUE_set(true,0,3);
    pmu.FREQ_set(60.5); //in Hz here
    pmu.DFREQ_set(1.2);
    
    // 2 analogs
    pmu2.ANALOG_add("TensaoA",1,RMS_ANALOG_INPUT);
    pmu2.ANALOG_add("CorrenteA",1,PEAK_ANALOG_INPUT);
    // Set nominal Freq
    pmu2.FNOM_set(FN_50HZ);        
    // default config = 0;
    pmu2.CFGCNT_set(1);
    pmu2.STAT_set(2048);
    pmu2.ANALOG_VALUE_set(3.1415,0);
    pmu2.ANALOG_VALUE_set(6.2830,1);
    pmu2.FREQ_set(2000);
    pmu2.DFREQ_set(200);
    
    //Add PMU to Config Packet
    my_config2->PMUSTATION_ADD(&pmu);
  //  my_config2->PMUSTATION_ADD(&pmu2);
    
      //Add PMU to Config Packet
    my_config1->PMUSTATION_ADD(&pmu);
 //   my_config1->PMUSTATION_ADD(&pmu2);
    
	//std::cout << pmu.STN_get();

    /* First call to socket() function */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    int on =1;
     int status = setsockopt(sockfd, SOL_SOCKET,SO_REUSEADDR, (const char *) &on, sizeof(on));
    if (sockfd < 0) 
    {
        perror("ERROR opening socket");
        exit(1);
    }
    /* Initialize socket structure */
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = TCP_PORT;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
 
    /* Now bind the host address using bind() call.*/
    if (bind(sockfd, (struct sockaddr *) &serv_addr,
                          sizeof(serv_addr)) < 0)
    {
         perror("ERROR on binding");
         exit(1);
    }
    /* Now start listening for the clients, here 
     * process will go in sleep mode and will wait 
     * for the incoming connection
     */
    printf("Connected. Waiting for message:\n") ;
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    // While true
    while (1) 
    {
    	// Accept all requests
        newsockfd = accept(sockfd, 
                (struct sockaddr *) &cli_addr, (socklen_t*)&clilen);
        if (newsockfd < 0)
        {
            perror("ERROR on accept");
            exit(1);
        }
        
        /* Create child process */
        pid = fork();
        if (pid < 0)
        {
            perror("ERROR on fork");
	    exit(1);
        }
        if (pid == 0)  
        {
            /* This is the client process */
            close(sockfd);
            doprocessing(newsockfd,my_config1,my_config2,my_dataframe,my_header);
            exit(0);
        }
        else
        {
            close(newsockfd);
        }
    } /* end of while */
}
Ejemplo n.º 8
0
int main( int argc, char *argv[] ) {
   if(argc==2){
      p=1;
   }



   int sockfd, newsockfd, portno, clilen;
   struct sockaddr_in serv_addr, cli_addr;
   int n, pid;
   
   portno = 9876;
   /* First call to socket() function */
   sockfd = socket(AF_INET, SOCK_STREAM, 0);   
   //printf("Abriendo Socket");
   //mensajeEstado(sockfd);

   
   /* Initialize socket structure */
   bzero((char *) &serv_addr, sizeof(serv_addr));
   
   
   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;
   serv_addr.sin_port = htons(portno);
   
   /* Now bind the host address using bind() call.*/
   int r=bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
   //printf("Binding Socket");
   //mensajeEstado(r);
   
   /* Now start listening for the clients, here
      * process will go in sleep mode and will wait
      * for the incoming connection
   */
   
   n=listen(sockfd,5);
   //printf("Listen");
   //mensajeEstado(n);
   clilen = sizeof(cli_addr);
   
   while (1) {
      newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
     // printf("Accept");
      //mensajeEstado(newsockfd);
            
      /* Create child process */
      pid = fork();
      
      if (pid < 0) {
         perror("ERROR en el fork\n");
         exit(1);
      }
      
      if (pid == 0) {
         /* This is the client process */
         close(sockfd);
         doprocessing(newsockfd);
         exit(0);
      }
      else {
         close(newsockfd);
      }
      
   } /* end of while */
}
Ejemplo n.º 9
0
int main( int argc, char *argv[] )
{
    pid_t pid;
    int sockfd, newsockfd, portno;
    struct sockaddr_in serv_addr, cli_addr;

    /* First call to socket() function */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
    {
        perror("ERROR opening socket");
        exit(1);
    }
    /* Initialize socket structure */
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = SERVER_PORT;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
 
    /* Now bind the host address using bind() call.*/
    if (bind(sockfd, (struct sockaddr *) &serv_addr,
                          sizeof(serv_addr)) < 0)
    {
         perror("ERROR on binding");
         exit(1);
    }
    /* Now start listening for the clients, here 
     * process will go in sleep mode and will wait 
     * for the incoming connection
     */
    listen(sockfd, 5);
    socklen_t size = sizeof(struct sockaddr_in);      
    
    while (1)  {
        char ip_address[15];
        newsockfd = accept(sockfd, 
                (struct sockaddr *) &cli_addr, &size);
        if (newsockfd < 0)
        {
            perror("ERROR on accept");
            exit(1);
        }
        printf("---------------------------------------------------------------------\n");    
        printf("New connection from %s on port %d\n",
            inet_ntoa(cli_addr.sin_addr), htons(cli_addr.sin_port));   
        strcpy(ip_address, inet_ntoa(cli_addr.sin_addr));
        /* Create child process */
        pid = fork();
        if (pid < 0)
        {
            perror("ERROR on fork");
            exit(1);
        }
        if (pid == 0)  
        {
            /* This is the client process */
            close(sockfd);
            if (doprocessing(newsockfd, ip_address) < 0) {
                exit(1);
            }
            exit(0);
        }
        else
        {
            close(newsockfd);
        }
    } /* end of while */
}
Ejemplo n.º 10
0
int main( int argc, char *argv[] ) {
    int sockfd, newsockfd, portno;
    unsigned int clilen;
    struct sockaddr_in serv_addr, cli_addr;
    int n, pid;

    // register a SIGCHLD signal handler
    register_handle_sigchld();

    // First call to socket() function
    printf("sockfd = socket(AF_INET, SOCK_STREAM, 0)\n");

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) {
        perror("ERROR opening socket");
        exit(1);
    }

    sockfd_global = sockfd;

    // Initialize socket structure
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = SERVERPORT;

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    // Now bind the host address using bind() call
    printf("bind(sockfd, &serv_addr)\n");
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        perror("ERROR on binding to server port");
        exit(1);
    }

    /* Now start listening for the clients, here
     * process will go in sleep mode and will wait
     * for the incoming connection
     */

    printf("listen(sockfd, MAX_CONN) on %d\n", SERVERPORT);

    listen(sockfd, 5);
    clilen = sizeof(cli_addr);

    /* Before we start accepting requests
     * Initialize the trie
     */
    zen_trie_init();

    while (1) {
	if (debug || debugnetwork)
	    printf("newsockfd = accept(sockfd, &client_addr)\n");

        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

        if (newsockfd < 0) {
            perror("ERROR on accept");
            continue;
        }

	if (debug || debugnetwork)
	    printf("New incoming connection\n");

        // Create child process
        pid = fork();

        if (pid < 0) {
            perror("ERROR on fork");
	    close(newsockfd);
            continue;
        }

        if (pid) {
            // This is the parent
            close(newsockfd);
        } else {
            // This is the child
            close(sockfd);

	    // prepare for signal handling in case of ^C
	    newsockfd_global = newsockfd;
	    register_handle_sigchld();

	    // process traffic
            while(doprocessing(newsockfd));

	    // process connection closure
            close(newsockfd);
            exit(0);
        }
    }
}