Newsgroup: it.comp.lang.c
Titolo: socket..........2

scusate sono sempre io .....
ho abbozzato i seguenti file.....
qlk mi può dare qlk consiglio o consigliarmi delle modifike????
ciao a tutti.........
               CLIENT
#include <windows.h>
extern "C"
{
#include <sys/types.h>
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <conio.h>
}
#include <iostream>
using namespace std;
#define PROTOPORT       5193            /* default protocol port number
*/
extern  int             errno;
char    localhost[] =   "localhost";    /* default host name
*/
/*------------------------------------------------------------------------
 * Program:   client
 *
 * Purpose:   allocate a socket, connect to a server, and print all
output
 *
 * Syntax:    client [ host [port] ]
 *
 *               host  - name of a computer on which server is
executing
 *               port  - protocol port number server is using
 *
 * Note:      Both arguments are optional.  If no host name is
specified,
 *            the client uses "localhost"; if no protocol port is
 *            specified, the client uses the default given by
PROTOPORT.
 *
*------------------------------------------------------------------------
 */
main(int argc,char *argv[])
{
        struct  hostent  *ptrh;  /* pointer to a host table entry
*/
        struct  protoent *ptrp;  /* pointer to a protocol table entry
*/
        struct  sockaddr_in sad; /* structure to hold an IP address
*/
        int     sd;              /* socket descriptor
*/
        int     port;            /* protocol port number
*/
        char    *host;           /* pointer to host name
*/
        int     n;               /* number of characters read
*/
        char    buf[1000];       /* buffer for data from the server
*/
        char    pkt[] =   "ping";
        int start;
#ifdef WIN32
        WSADATA wsaData;
        WSAStartup(0x0101, &wsaData);
#endif
        memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure
*/
        sad.sin_family = AF_INET;         /* set family to Internet
*/
        /* Check command-line argument for protocol port and extract
*/
        /* port number if one is specified.  Otherwise, use the default
*/
        /* port value given by constant PROTOPORT
*/
        if (argc > 2) {                 /* if protocol port specified
*/
                port = atoi(argv[2]);   /* convert to binary
*/
        } else {
                port = PROTOPORT;       /* use default port number
*/
        }
        if (port > 0)                   /* test for legal value
*/
                sad.sin_port = htons((u_short)port);
        else {                          /* print error message and exit
*/
                fprintf(stderr,"bad port number %s\n",argv[2]);
                exit(1);
        }
        /* Check host argument and assign host name. */
        if (argc > 1) {
                host = argv[1];         /* if host argument specified
*/
        } else {
                host = localhost;
        }
        /* Convert host name to equivalent IP address and copy to sad.
*/
        ptrh = gethostbyname(host);
        if ( ((char *)ptrh) == NULL ) {
                fprintf(stderr,"invalid host: %s\n", host);
                exit(1);
        }
    memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
        /* Map TCP transport protocol name to protocol number. */
        if ( (int)(ptrp = getprotobyname("tcp")) == 0) {
                fprintf(stderr, "cannot map \"tcp\" to protocol
number");
                exit(1);
        }
        /* Create a socket. */
        sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
        if (sd < 0) {
                fprintf(stderr, "socket creation failed\n");
                exit(1);
        }
        /* Connect the socket to the specified server. */
        if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
                fprintf(stderr,"connect failed\n");
                exit(1);
        }
        int i;
        int tempo [4];
        int byte=0;
        int stop;
        int media=0;
        int riusciti=0;
        //  PEZZOTTAMMA
        for (i=0;i<=3;i++){
             start = time (0);
             n = send(sd,pkt,strlen(pkt),0);
             byte=0;
             if(n=4){
                 while ( byte<4 ) {
                    n = recv(sd, buf, sizeof(buf), 0);
                    byte=byte+n;
                    }
                 stop= time (0);
                 tempo[i]= stop-start;
                 cout<<"il tempo di RTT nella connessione "<<i+1<<" e':
"<<tempo[i]<<endl;
                 riusciti=riusciti +1;
                 media=media+tempo[i];
             }
             else
                 fprintf(stderr,"invio non possibile\n");
        }
       media=media/riusciti;
       cout<<"il tempo medio di RTT e' : "<<media<<endl;
        /* Close the socket. */
        closesocket(sd);
        /* Terminate the client program gracefully. */
        getchar();
       exit(0);
  }
                       SERVER
/* server.c - code for example server program that uses TCP */
#ifndef unix
#include <windows.h>
#include <winsock.h>
#else
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#include <stdio.h>
#include <string.h>
#define PROTOPORT       5193            /* default protocol port number
*/
#define QLEN            6               /* size of request queue
*/
int     visits      =   0;              /* counts client connections
*/
/*------------------------------------------------------------------------
 * Program:   server
 *
 * Purpose:   allocate a socket and then repeatedly execute the
following:
 *              (1) wait for the next connection from a client
 *              (2) send a short message to the client
 *              (3) close the connection
 *              (4) go back to step (1)
 *
 * Syntax:    server [ port ]
 *
 *               port  - protocol port number to use
 *
 * Note:      The port argument is optional.  If no port is specified,
 *            the server uses the default given by PROTOPORT.
 *
*------------------------------------------------------------------------
 */
main(argc, argv)
int     argc;
char    *argv[];
{
        struct  hostent  *ptrh;  /* pointer to a host table entry
*/
        struct  protoent *ptrp;  /* pointer to a protocol table entry
*/
        struct  sockaddr_in sad; /* structure to hold server's address
*/
        struct  sockaddr_in cad; /* structure to hold client's address
*/
        int     sd, sd2;         /* socket descriptors
*/
        int     port;            /* protocol port number
*/
        int     alen;            /* length of address
*/
        char    buf[1000];       /* buffer for string the server sends
*/
        int n= 0;
#ifdef WIN32
        WSADATA wsaData;
        WSAStartup(0x0101, &wsaData);
#endif
        memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure
*/
        sad.sin_family = AF_INET;         /* set family to Internet
*/
        sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address
*/
        /* Check command-line argument for protocol port and extract
*/
        /* port number if one is specified.  Otherwise, use the default
*/
        /* port value given by constant PROTOPORT
*/
        if (argc > 1) {                 /* if argument specified
*/
                port = atoi(argv[1]);   /* convert argument to binary
*/
        } else {
                port = PROTOPORT;       /* use default port number
*/
        }
        if (port > 0)                   /* test for illegal value
*/
                sad.sin_port = htons((u_short)port);
        else {                          /* print error message and exit
*/
                fprintf(stderr,"bad port number %s\n",argv[1]);
                exit(1);
        }
        /* Map TCP transport protocol name to protocol number */
        if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
                fprintf(stderr, "cannot map \"tcp\" to protocol
number");
                exit(1);
        }
        /* Create a socket */
        sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
        if (sd < 0) {
                fprintf(stderr, "socket creation failed\n");
                exit(1);
        }
        /* Bind a local address to the socket */
        if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
                fprintf(stderr,"bind failed\n");
                exit(1);
        }
        /* Specify size of request queue */
        if (listen(sd, QLEN) < 0) {
                fprintf(stderr,"listen failed\n");
                exit(1);
        }
        /* Main server loop - accept and handle requests */
        memset(buf,0,sizeof(buf));
        while (1) {
        int count=0;
                alen = sizeof(cad);
                if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) <
0) {
                        fprintf(stderr, "accept failed\n");
                        exit(1);
                }
                 while ( count<4 ) {
                  int byte = 0;
                while ( byte<4 ) {
                    n = recv(sd2, buf, sizeof(buf), 0);
                    byte=byte+n;
                    }
             n = send(sd2,buf,4,0);
             memset(buf,0,sizeof(buf));
             count++;
              }  
                closesocket(sd2);
        }
}

 wrote
credo tu dovresti trovarti un newsgroup piu' adatto dove la
programmazione socket non e' off topic.
inoltre, in generale, su questo NG non si spediscono due programmi
interi buttati li ed una domanda estremamente vaga.
Di solito si postano solo alcune righe e si pone una o piu' domande
specifiche.
Rispondendo alla tua domanda, presumendo che il programma funzioni e che
il codice sia scritto giusto e senza errori, beh, potresti modificare il
programma in modo che accetti piu' clients in contemporanea. Dando uno
sguardo al volo noto che il tuo server accetta un solo client.
Questa credo sia la prima modifica importante da fare al lato server. Un
server che "serve" un solo client e' raramente utile (ma in rari casi lo
e').
Tommaso
-- 
Posted via Mailgate.ORG Server - 
http://www.Mailgate.ORG

