DOMANDA [C] TCP Server Client invio stringhe

Pubblicità

mark9

Nuovo Utente
Messaggi
45
Reazioni
3
Punteggio
24
Sto creando un programma in C per la comunicazione TCP tra un client e un server. Il client riceve da tastiera indirizzo e porta a cui connettersi (localhost su porta 9888 casuale) e una volta stabilita la connessione il server invia al client la stringa di connessione avvenuta.

1)Vorrei modificarlo per poter inserire nel client anzichè il nome "localhost" l'indirizzo numerico con utilizzo quindi della gethostbyaddr() suppongo. Come si può fare?

2)Dopodicheè dovrei anche leggere dal client sue stringhe A e B ed inviarle al server mediante una struct. Il server deve concatenarle in una stringa C = A + B e inviarla al client, aggiungendo la C alla struct iniziale ricevuta. Se una delle stringhe A o B è uguale a "quit", il server invia al client la stringa "bye". Quindi il client una volta ricevuta la stringa dal server e stampata, se questa è "bye" chiude la connessione e il programma termina, altrimenti torna a leggere due stringhe.

Il tutto con l'invio di due stringhe consecutive riuscirei a farlo credo, ma con la struct non so come procedere.
Grazie per l'aiuto!

Codice:
//SERVER

#if defined WIN32
#include <winsock.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 30

void ClearWinSock() {
#if defined WIN32
    WSACleanup();
#endif
}

int main(void) {

//---------------------------INIZIALIZZAZIONE WSADATA

#if defined WIN32

    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf ("Error at WSAStartup");
        return 0;
    }

#endif

//-------------------------------CREAZIONE SOCKET

    int Mysocket;
    Mysocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    //Valutare la presenza di errori per assicurarsi che la socket sia valida.
    if (Mysocket < 0) {
        printf("socket creation failed\n");
        return 0;
    }

    //Assegnazione indirizzo alla socket.
    struct sockaddr_in sad;
    memset(&sad, 0, sizeof(sad));
    sad.sin_family = AF_INET;
    sad.sin_addr.s_addr = inet_addr ("127.0.0.1");
    sad.sin_port = htons (9888);

//------------------------ASSEGNAZIONE PORTA E IP ALLA SOCKET

if (bind(Mysocket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
      printf ("bind() failed\n");
       closesocket(Mysocket);
       return 0;
}

//---------------------------SETTAGGIO SOCKET ALL'ASCOLTO

int qlen = 10; //numero client che il server può gestire

if (listen (Mysocket, qlen) < 0) {

    printf("listen() failed\n");
    closesocket(Mysocket);
    return 0;
}

struct sockaddr_in cad;  // struttura per l'indirizzo del cient
int Csocket;             // descrittore socket per il client
int clientlen;             // lunghezza indirizzo del client

//------------------------------ACCETTA LA CONNESSIONE

while (1) {

    printf("In attesa di un client con cui comunicare...\n");
    memset(&cad, 0, sizeof(cad));
    clientlen = sizeof(cad);       // assegna la dimensione dell'indirizzo del client

    if((Csocket = accept(Mysocket, (struct sockaddr*) &cad, &clientlen)) < 0) {
        printf ("accept failed\n");
        closesocket(Mysocket);
        ClearWinSock();
        return 0;
    }


    printf("Connessione stabilita con il client il cui indirizzo e' %s \n\n", inet_ntoa(cad.sin_addr));

//---------------------------------------INVIO STRINGA AL CLIENT

    char* inputString = "Connessione avvenuta!";
    int stringlen = strlen(inputString);


    if (send(Csocket, inputString, stringlen, 0) != stringlen) {
        printf("client-send() sent a different number of bytes than expected");
        closesocket(Csocket);
        ClearWinSock();
        system ("pause");
        return 0;
    }

//CLIENT

#if defined WIN32
#include <winsock.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 30

void ClearWinSock() {
#if defined WIN32
    WSACleanup();
#endif
}

//-----------------------------INIZIALIZZAZIONE WSADATA

int main (void) {
#if defined WIN32

    WSADATA wsaData;
    int iResult = WSAStartup (MAKEWORD (2,2), &wsaData);

    if (iResult !=0) {
        printf ("error at WSASturtup\n");
        return 0;
        }

#endif

//--------------------------------CREAZIONE SOCKET

    int Csocket;

    Csocket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (Csocket < 0) {

    printf ("socket creation failed");
    closesocket (Csocket);
    ClearWinSock();
    return 0;
    }

    char s_name[BUFSIZE];
            printf ("Inserisci il nome dell'host a cui connetterti\n");
            scanf ("%s", s_name);

            int port;
            printf ("Inserisci la porta a cui connetterti\n");
            scanf("%d", &port);


            struct hostent *host;
            host = gethostbyname(s_name);
            if (host == NULL) {
            fprintf(stderr, "gethostbyname() failed.\n");
            return 0;
            }

            struct sockaddr_in sad;
                memset (&sad, 0, sizeof(sad));
                sad.sin_family = AF_INET;
                sad.sin_addr.s_addr = *((long*) host->h_addr);
                sad.sin_port = htons (port);

//------------------------------CONNESSIONE AL SERVER

   if (connect(Csocket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
         printf ("failed to connect\n");
         closesocket (Csocket);
         ClearWinSock();
         return 0;
   }

//-----------------------------RICEZIONE STRINGA DAL SERVER

   char buf[BUFSIZE];

   int read = recv (Csocket, buf, BUFSIZE - 1, 0);

   if (read <=0) {

       printf ("Qualcosa non và!\n");
   }

   else {
       buf[read] = '\0';  //tappo stringa per sicurezza
       printf("Server scrive: %s\n", buf);
   }
 
Sto creando un programma in C per la comunicazione TCP tra un client e un server. Il client riceve da tastiera indirizzo e porta a cui connettersi (localhost su porta 9888 casuale) e una volta stabilita la connessione il server invia al client la stringa di connessione avvenuta.

1)Vorrei modificarlo per poter inserire nel client anzichè il nome "localhost" l'indirizzo numerico con utilizzo quindi della gethostbyaddr() suppongo. Come si può fare?

2)Dopodicheè dovrei anche leggere dal client sue stringhe A e B ed inviarle al server mediante una struct. Il server deve concatenarle in una stringa C = A + B e inviarla al client, aggiungendo la C alla struct iniziale ricevuta. Se una delle stringhe A o B è uguale a "quit", il server invia al client la stringa "bye". Quindi il client una volta ricevuta la stringa dal server e stampata, se questa è "bye" chiude la connessione e il programma termina, altrimenti torna a leggere due stringhe.

Il tutto con l'invio di due stringhe consecutive riuscirei a farlo credo, ma con la struct non so come procedere.
Grazie per l'aiuto!

Codice:
//SERVER

#if defined WIN32
#include <winsock.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 30

void ClearWinSock() {
#if defined WIN32
    WSACleanup();
#endif
}

int main(void) {

//---------------------------INIZIALIZZAZIONE WSADATA

#if defined WIN32

    WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf ("Error at WSAStartup");
        return 0;
    }

#endif

//-------------------------------CREAZIONE SOCKET

    int Mysocket;
    Mysocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    //Valutare la presenza di errori per assicurarsi che la socket sia valida.
    if (Mysocket < 0) {
        printf("socket creation failed\n");
        return 0;
    }

    //Assegnazione indirizzo alla socket.
    struct sockaddr_in sad;
    memset(&sad, 0, sizeof(sad));
    sad.sin_family = AF_INET;
    sad.sin_addr.s_addr = inet_addr ("127.0.0.1");
    sad.sin_port = htons (9888);

//------------------------ASSEGNAZIONE PORTA E IP ALLA SOCKET

if (bind(Mysocket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
      printf ("bind() failed\n");
       closesocket(Mysocket);
       return 0;
}

//---------------------------SETTAGGIO SOCKET ALL'ASCOLTO

int qlen = 10; //numero client che il server può gestire

if (listen (Mysocket, qlen) < 0) {

    printf("listen() failed\n");
    closesocket(Mysocket);
    return 0;
}

struct sockaddr_in cad;  // struttura per l'indirizzo del cient
int Csocket;             // descrittore socket per il client
int clientlen;             // lunghezza indirizzo del client

//------------------------------ACCETTA LA CONNESSIONE

while (1) {

    printf("In attesa di un client con cui comunicare...\n");
    memset(&cad, 0, sizeof(cad));
    clientlen = sizeof(cad);       // assegna la dimensione dell'indirizzo del client

    if((Csocket = accept(Mysocket, (struct sockaddr*) &cad, &clientlen)) < 0) {
        printf ("accept failed\n");
        closesocket(Mysocket);
        ClearWinSock();
        return 0;
    }


    printf("Connessione stabilita con il client il cui indirizzo e' %s \n\n", inet_ntoa(cad.sin_addr));

//---------------------------------------INVIO STRINGA AL CLIENT

    char* inputString = "Connessione avvenuta!";
    int stringlen = strlen(inputString);


    if (send(Csocket, inputString, stringlen, 0) != stringlen) {
        printf("client-send() sent a different number of bytes than expected");
        closesocket(Csocket);
        ClearWinSock();
        system ("pause");
        return 0;
    }

//CLIENT

#if defined WIN32
#include <winsock.h>
#else
#define closesocket close
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFSIZE 30

void ClearWinSock() {
#if defined WIN32
    WSACleanup();
#endif
}

//-----------------------------INIZIALIZZAZIONE WSADATA

int main (void) {
#if defined WIN32

    WSADATA wsaData;
    int iResult = WSAStartup (MAKEWORD (2,2), &wsaData);

    if (iResult !=0) {
        printf ("error at WSASturtup\n");
        return 0;
        }

#endif

//--------------------------------CREAZIONE SOCKET

    int Csocket;

    Csocket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (Csocket < 0) {

    printf ("socket creation failed");
    closesocket (Csocket);
    ClearWinSock();
    return 0;
    }

    char s_name[BUFSIZE];
            printf ("Inserisci il nome dell'host a cui connetterti\n");
            scanf ("%s", s_name);

            int port;
            printf ("Inserisci la porta a cui connetterti\n");
            scanf("%d", &port);


            struct hostent *host;
            host = gethostbyname(s_name);
            if (host == NULL) {
            fprintf(stderr, "gethostbyname() failed.\n");
            return 0;
            }

            struct sockaddr_in sad;
                memset (&sad, 0, sizeof(sad));
                sad.sin_family = AF_INET;
                sad.sin_addr.s_addr = *((long*) host->h_addr);
                sad.sin_port = htons (port);

//------------------------------CONNESSIONE AL SERVER

   if (connect(Csocket, (struct sockaddr*) &sad, sizeof(sad)) < 0) {
         printf ("failed to connect\n");
         closesocket (Csocket);
         ClearWinSock();
         return 0;
   }

//-----------------------------RICEZIONE STRINGA DAL SERVER

   char buf[BUFSIZE];

   int read = recv (Csocket, buf, BUFSIZE - 1, 0);

   if (read <=0) {

       printf ("Qualcosa non và!\n");
   }

   else {
       buf[read] = '\0';  //tappo stringa per sicurezza
       printf("Server scrive: %s\n", buf);
   }
Gran bel problema, purtroppo in C mi sono fermato un pochino prima delle reti , vediamo se @Marechiaro ne sà qualcosa.
 
Con l'aiuto di un utente sono arrivato a questo punto:

struct stringab {
char a[30];
char b[30];
char c[70];
} ab;


void* abptr = &ab;


printf ("Inserisci prima stringa");
scanf ("%s", ab.a);


printf ("Inserisci seconda stringa");
scanf ("%s", ab.b);



if (send(Csocket, (char*)abptr, sizeof(ab), 0) != sizeof(ab)) {
printf("client-send() sent a different number of bytes than expected");
closesocket(Csocket);
ClearWinSock();
system ("pause");
return 0;
}

Dovrebbe così inviare la struttura di stringhe al server che poi deve operare come segue:

"Il server riceve le stringhe A e B e le concatena in un'unica stringa C uguale ad “A + B” e la
invia indietro al client, aggiungendo il valore C alla struct inizialmente ricevuta; altrimenti,
se almeno una delle due stringhe A o B è uguale a “quit” il server invia al client la stringa
“bye”."

Ed è qui che ora sono bloccato..
 
Pubblicità
Pubblicità
Indietro
Top