Visualizzazione risultati 1 fino 5 di 5

Discussione: Creare una applicazione client server per Linux/Unix [mod]

  1. #1
    Guest

    Smile Help - Applicazione client server Unix

    Devo realizzare una semplice applicazione client server in c sotto unix/linux.
    Due semplici file uno client e uno server.
    Avete qualche idea ?

  2. #2
    L'avatar di binarysun
    binarysun non è connesso Utente storico
    Data registrazione
    02-07-2004
    Messaggi
    2,017

    Predefinito

    Dipende da che devi fare!
    "L'intelligenza è una pianta che va curata continuamente.
    Dovreste vedere com'è bello, il mio bonsai."
    Rat-man®

    [Gradient Text]
    [Su che server sei?]
    ->flickr

  3. #3
    Guest

    Predefinito

    Sposto nella sezione adatta...

  4. #4
    L'avatar di pietrovischia
    pietrovischia non è connesso Utente attivo
    Data registrazione
    16-10-2005
    Residenza
    Padova
    Messaggi
    351

    Predefinito

    Anni fa ho smanettato ricalcando un tutorial che avevo trovato in giro, non ricordo dove purtroppo

    Posto qui il codice: è un minicoso che permette di mandare stringhe di caratteri. Se non ricordo male, funzionava.
    Datti una occhiata e modifica il tutto per arrivare alle features di cui necessiti, e lascia stare le menate sulla gnu
    Non ricordo se è gia' supportata la multiconnessione di piu' utenti, comunque in teoria non dovrebbe essere troppo laborioso. In teoria.
    CLIENT
    Codice:
    /* This code is Copyrighted (C) 2002 by Pietro Vischia.
     * Released under the terms of the GNU General Public License.
     * If you modify my code, please send me a copy of the new code,
     * so I will check the weaknesses of my code.
     * mailto: shadow_shark@libero.it
     *
     * This file is part of SOCK_EX 0.1.
     *
     *   SOCK_EX 0.1 is free software; you can redistribute it and/or modify
     *   it under the terms of the GNU General Public License as published by
     *   the Free Software Foundation; either version 2 of the License, or
     *   (at your option) any later version.
     *
     *   SOCK_EX 0.1 is distributed in the hope that it will be useful,
     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *   GNU General Public License for more details.
     *
     *   You should have received a copy of the GNU General Public License
     *   along with SOCK_EX 0.1; if not, write to the Free Software
     *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  */
    
    
    /* This is a very simple client which establish a TCP connection
     * with a remote server and sends to it a string of characters.
     * It doesn't support DNS lookup yet.
     * Data-encapsulating in defined-size packets is not implemented yet.
     */
    
    /* To do: implementing reading-buffer and sending-buffer, for sending message more of 128 byte long dividing them into 128-bytes-long packets */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <netdb.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    /* Max packet size set to 128 byte */
    #define MAX_DATA_SIZE 128
    
    int main(int argc, char *argv[]){
      
      short int port;
      /* Message is stored in a 128-byte array. */
      char buf[MAX_DATA_SIZE];
      int main_fd;
      struct sockaddr_in server_address;
      
      /* Control on startup syntax */
      if(argc!=3){
        printf("Usage: client hostname port\n");
        exit(1);
      }
      
      /* Convert string into short integer for port number */
      port = atoi(argv[2]);
      
      printf("Please insert the message you would like to send:\n");
      
      /* Control on data input */
      if(fgets(buf, MAX_DATA_SIZE-1, stdin) == NULL){
        perror("Error reading data from standard input");
        exit(1);
      }
      
      /* Control on socket file descriptor creation */
      if((main_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        perror("Error starting up socket file descriptor");
        exit(1);
      }
      
      server_address.sin_family      = AF_INET;
      server_address.sin_port        = htons(port);
      server_address.sin_addr.s_addr = inet_addr(argv[1]);
      memset (&(server_address.sin_zero), '\0', 8);
      
      /* Control on establishing connection with remote host */
      if(connect(main_fd, (struct sockaddr *)&server_address, sizeof(struct sockaddr)) == -1){
        perror("Error connecting to remote host");
        exit(1);
      }
      
      /* Control on data transmission */
      if(send(main_fd, buf, sizeof(buf) , 0) == -1){
        perror("Error sending message");
        exit(1);
      }
      printf("Message sent\n");
      buf[MAX_DATA_SIZE] = '\0';
      
      close(main_fd);
      return(0);
    }
    SERVER:
    Codice:
    /* This code is Copyrighted (C) 2002 by Pietro Vischia
     * Released under the terms of the GNU General Public License.
     * If you modify my code, please send me a copy of the new code,
     * so I will check the weaknesses of my code
     *
     * This file is part of SOCK_EX 0.1.
     *
     *   SOCK_EX 0.1 is free software; you can redistribute it and/or modify
     *   it under the terms of the GNU General Public License as published by
     *   the Free Software Foundation; either version 2 of the License, or
     *   (at your option) any later version.
     *
     *   SOCK_EX 0.1 is distributed in the hope that it will be useful,
     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *   GNU General Public License for more details.
     *
     *   You should have received a copy of the GNU General Public License
     *   along with SOCK_EX 0.1; if not, write to the Free Software
     *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     */
    
    /* This is a very simple server which accepts some TCP
     * connections from a remote client and print on screen
     * the received data.
     * It doesn't allow listening on a port below 1024, so it
     * can be safely used from a non-superuser account.
     * Encapsulating data in defined-size packets is not implemented yet.
     */
    
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    /* Allow 15 simultaneous connections to specified port */
    #define BACKLOG 15
    
    /* Max packet size set to 128 byte */
    #define MAX_DATA_SIZE 128
    
    int main (int argc, char *argv[]){
      
      short int port;
      int main_fd;
      struct sockaddr_in server_address;
      int sin_size;
      struct sockaddr_in client_address;
      int connection_fd;
      char buf[MAX_DATA_SIZE];
      
      /* Control on startup syntax */
      if(argc != 2) {
        printf("Usage: server port");
        exit(1);
      }
        
      /* Convert string into short integer for port number */
      port = atoi(argv[1]);
      
      /* Control on non-allowed ports */
      if(port <= 1024){
        printf("Ports below 1024 are reserved, unless you are SysAdmin.\nSearch http://shadow_shark.tripod.com for the SysAdmin version of this server\n");
        exit(1);
      }
      
      /* Control on socket file descriptor creation */
      if((main_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        perror("Error starting up socket file descriptor");
        exit(1);
      }
      
      /* Waiting if the port is in use is not implemented yet */
    
      /* Defining address of host in which server is running */
      server_address.sin_family      = AF_INET;
      server_address.sin_port        = htons(port);
      server_address.sin_addr.s_addr = htonl(INADDR_ANY);
      memset(&(server_address.sin_zero), '0', 8);
      
      /* Control on binding to specified port */
      if(bind(main_fd, (struct sockaddr *)&server_address, sizeof(struct sockaddr)) == -1){
        perror("Error binding server to specified port");
        exit(1);
      }
      
      /* Control on listening on specified port */
      if(listen(main_fd, BACKLOG) == -1){
        perror("Error listening on specified port");
        exit(1);
      }
      
      /* Code for elimination of zombie processes is not implemented yet */
      
      /* Main connection loop */
      while(1){
        sin_size = sizeof(struct sockaddr_in);
        
        /* Control on accepting connections from a remote client */
        if((connection_fd = accept(main_fd, (struct sockaddr *)&client_address, &sin_size)) == -1){
          perror("Error accepting connection");
          exit(1);
        }
        
        printf("Message from server: finally I got a connection from %s\n\n", inet_ntoa(client_address.sin_addr));
        
        /* Creating child file descriptor for new connection (main_fd continue listening on port) */
        if(!fork()){
          close(main_fd);
          
          /* Control on receiving data. A better definition of packets size and a full control
    	 on receiving packets are not implemented yet. */
          if(recv(connection_fd, buf, MAX_DATA_SIZE, 0) == -1){
    	perror("Error receiving data from client");
    	exit(1);
          }
          printf("Message:\n%s\n", &buf);
          close(connection_fd);
          exit(0);
        }
        /* Why another time? */
        close(connection_fd);
      }
      return(0);
    }

  5. #5
    Guest

    Predefinito

    basta impararsi le socket...

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •