Invio/ricezione dati tra socket

mark9

Nuovo Utente
45
3
Mi sto esercitando con la programmazione basilare in c delle socket. Qui di seguito un programmino client/server che si scambiano stringhe ed elaborazioni (il server riceve una stringa da cui rimuove le vocali e la rispedisce al client). Dopo varie correzioni il programma sembra funzionare, la mia domanda ora è:

1)se volessi continuare con l'invio e la ricezione di altre stringhe, utilizzo buffer diversi o pulisco quelli usati per riutilizzarli? Se sì, come?
2)in entrambi i casi le funzioni send() e recv() si possono utilizzare in sequenza senza particolari problemi?


Codice:
***SERVER***


[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] defined WIN32[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<winsock2.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]
[/COLOR][/SIZE][/COLOR][/SIZE]
[B]

[SIZE=2][COLOR=#7f0055]

[SIZE=2][COLOR=#7f0055]

#else


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#define[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] closesocket close[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<sys/socket.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<arpa/inet.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<unistd.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]
[/COLOR][/SIZE][/COLOR][/SIZE]
[B]

[SIZE=2][COLOR=#7f0055]

[SIZE=2][COLOR=#7f0055]

#endif


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<stdio.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<stdlib.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<string.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]



[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#define[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] BUFSIZE 30[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]ClearWinSock[/B]() {[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] defined WIN32[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]WSACleanup[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]();[/SIZE]
[SIZE=2]
[/SIZE]
[B]

[SIZE=2][COLOR=#7f0055]

[SIZE=2][COLOR=#7f0055]

#endif


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2]}




[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]main[/B]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]) {[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---------------------------INIZIALIZZAZIONE WSADATA


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] defined WIN32[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]WSADATA[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] wsaData;[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] iResult = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]WSAStartup[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](MAKEWORD(2,2), &wsaData);[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (iResult != 0) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Error at WSAStartup"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

    }




[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#endif


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2]

[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//-------------------------------CREAZIONE SOCKET


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Mysocket;[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]  Mysocket = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]socket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](PF_INET, SOCK_STREAM, IPPROTO_TCP);[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Mysocket < 0) {[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"socket creation failed\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

  }




[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]struct[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]sockaddr_in[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sad;[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]memset[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](&sad, 0, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](sad));[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]  sad.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sin_family[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = AF_INET;[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]  sad.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sin_addr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].s_addr = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]inet_addr[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"127.0.0.1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]  sad.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sin_port[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]htons[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (9888);[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//------------------------ASSEGNAZIONE PORTA E IP ALLA SOCKET


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]bind[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Mysocket, ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]struct[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]sockaddr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]*) &sad, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](sad)) <0) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"bind() failed\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Mysocket);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

}




[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---------------------------SETTAGGIO SOCKET ALL'ASCOLTO


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] qlen = 10;[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]listen[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Mysocket, qlen) < 0) {[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"listen() failed\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Mysocket);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

}




[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]struct[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]sockaddr_in[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cad;[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Csocket;[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] clientlen;[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//------------------------------ACCETTA LA CONNESSIONE


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]while[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (1) {[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"In [U]attesa[/U] [U]di[/U] [U]un[/U] client [U]con[/U] [U]cui[/U] [U]comunicare[/U]\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]memset[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](&cad, 0, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](cad));[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    clientlen = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](cad);[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]((Csocket = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]accept[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Mysocket, ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]struct[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]sockaddr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]*) &cad, &clientlen)) < 0) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"accept failed\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Mysocket);[/SIZE]


[SIZE=2]

        ClearWinSock();


[/SIZE][SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

        }




[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"[U]connesso[/U] [U]con[/U] [U]il[/U] client\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]


[SIZE=2]



 


[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---------------------------------------INVIO STRINGA AL CLIENT


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]* inputString = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"[U]connessione[/U] [U]avvenuta[/U]"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] stringlen = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]strlen[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](inputString);[/SIZE]


[SIZE=2]



 


[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]send[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket, inputString, stringlen, 0) != stringlen) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"client-send() sent a different number of bytes than expected"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket);[/SIZE]


[SIZE=2]

        ClearWinSock();


[/SIZE][SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]system[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

    }




 


[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//-------------------------------------RICEZIONE STRINGA DAL CLIENT


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] str1[BUFSIZE]; [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] str1des[BUFSIZE];[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i,j=0;[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//[U]recv[/U] ([U]Csocket[/U], str1, BUFSIZE - 1, 0);[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] read = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]recv[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket, str1, BUFSIZE-1, 0); str1[read] = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'\0'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]


[SIZE=2]



 


[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//-------------------------------ELIMINAZIONE VOCALI DALLA STRINGA RICEVUTA


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](i=0;i<=[/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]strlen[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](str1)+1;i++) {[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'a'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'e'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'i'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'o'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'u'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'A'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'E'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'I'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'O'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]||str1[i]==[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'U'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]                        str1[i]=[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]' '[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]                [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE][/B]
[SIZE=2]

                        str1des[j++]=str1[i];


            }




[/SIZE][SIZE=2]            str1des[j]=[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'\0'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//-----------------------------------INVIO STRINGA ELABORATA AL CLIENT


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2]            [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]send[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket, str1des, [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]strlen[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](str1des), 0) != [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]strlen[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](str1des)) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]                [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"client-send() sent a different number of bytes than expected"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]                [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket);[/SIZE]


[SIZE=2]

                ClearWinSock();


[/SIZE][SIZE=2]                [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]system[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]                [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

            }




}




[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//------------------------------------------CHIUSURA CONNESSIONE


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]closesocket[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Csocket);[/SIZE]


[SIZE=2]

ClearWinSock();


[/SIZE][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]printf[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]system[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]return[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;[/SIZE]


[SIZE=2]



}


***CLIENT***


[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] defined WIN32[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<winsock2.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]
[/COLOR][/SIZE][/COLOR][/SIZE]
[B]

[SIZE=2][COLOR=#7f0055]

[SIZE=2][COLOR=#7f0055]

#else


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#define[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] closesocket close[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<sys/socket.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<arpa/inet.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<unistd.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]
[/COLOR][/SIZE][/COLOR][/SIZE]
[B]

[SIZE=2][COLOR=#7f0055]

[SIZE=2][COLOR=#7f0055]

#endif


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<stdio.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<stdlib.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#include[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]<string.h>[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]



[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#define[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] BUFSIZE 30[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]ClearWinSock[/B]() {[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] defined WIN32[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]WSACleanup[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]();[/SIZE]
[SIZE=2]
[/SIZE]
[B]

[SIZE=2][COLOR=#7f0055]

[SIZE=2][COLOR=#7f0055]

#endif


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2]}




[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//-----------------------------INIZIALIZZAZIONE WSADATA


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]int[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [B]main[/B] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]) {[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]#if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] defined WIN32[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]WSADATA[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] wsaData;[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] iResult = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]WSAStartup[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (MAKEWORD (2,2), &wsaData);[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (iResult !=0) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"error at WSASturtup\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]        [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

        }




[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#endif


[/COLOR][/SIZE]

[/COLOR][/SIZE]

[/B][SIZE=2]

 


[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//--------------------------------CREAZIONE SOCKET


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Csocket;[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    Csocket = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]socket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (PF_INET, SOCK_STREAM, IPPROTO_TCP);[/SIZE]


[SIZE=2]



[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Csocket < 0) {[/SIZE]


[SIZE=2]



[/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"socket creation failed"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Csocket);[/SIZE]


[SIZE=2]

    ClearWinSock();


[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

    }




[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//--------------------------COSTRUZIONE INDIRIZZO SERVER[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]



[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]struct[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]sockaddr_in[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sad;[/SIZE]
[SIZE=2]
[/SIZE]
[B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]memset[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (&sad, 0, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](sad));[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]   sad.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sin_family[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = AF_INET;[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]   sad.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sin_addr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].s_addr = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]inet_addr[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"127.0.0.1"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]   sad.[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]sin_port[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]htons[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (9888);[/SIZE]


[SIZE=2]



 


[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//------------------------------CONNESSIONE AL SERVER


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]connect[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket, ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]struct[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]sockaddr[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]*) &sad, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](sad)) < 0) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]      [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"failed to connect\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]      [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Csocket);[/SIZE]


[SIZE=2]

      ClearWinSock();


[/SIZE][SIZE=2]      [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

  }




 


[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//-----------------------------RICEZIONE DATI DAL SERVER


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]char[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] buf[BUFSIZE];[/SIZE]


[SIZE=2]



[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] read = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]recv[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Csocket, buf, BUFSIZE - 1, 0);[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (read <=0) {[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"[U]Qualcosa[/U] non [U]và[/U]!\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]


[SIZE=2]

}




[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]else[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    buf[read] = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]'\0'[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Server [U]scrive[/U]: %s\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], buf);[/SIZE]


[SIZE=2]

}




 


[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//----------------------------INVIO PRIMA STRINGA AL SERVER


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]char[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] str1[BUFSIZE]; [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] stringlen;[/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]printf[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"[U]inserisci[/U] [U]prima[/U] [U]stringa[/U]:\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]fgets[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (str1, BUFSIZE,stdin);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]stringlen = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]strlen[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](str1);[/SIZE]


[SIZE=2]



 


[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]send[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket, str1, stringlen, 0) != stringlen) {[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"client-send() sent a different number of bytes than expected"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]closesocket[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](Csocket);[/SIZE]


[SIZE=2]

    ClearWinSock();


[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]system[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]
[/SIZE]
[SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]return[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] 0;[/SIZE]


[SIZE=2]

}




[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//------------------------------RICEZIONE STRINGA ELABORATA DAL SERVER


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]char[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] buf2[BUFSIZE];[/SIZE]


[SIZE=2]



[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] read2 = [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]recv[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Csocket, buf2, BUFSIZE - 1, 0);[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]if[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (read2 <=0) {[/SIZE]


[SIZE=2]



[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"[U]Qualcosa[/U] non [U]và[/U]!\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]


[SIZE=2]

}




[/SIZE][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]else[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] {[/SIZE]


[SIZE=2]

    buf2[read2] = 0;


[/SIZE][SIZE=2]    [/SIZE][B][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]printf[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Server [U]scrive[/U]: %s\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], buf2);[/SIZE]


[SIZE=2]

}




[/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---------------------------------------CHIUSURA CONNESSIONE


[/COLOR][/SIZE]

[/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]closesocket[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Csocket);[/SIZE]


[SIZE=2]

ClearWinSock();


[/SIZE][SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]printf[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#642880][SIZE=2][COLOR=#642880][B]system[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"pause"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]return[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 0;[/SIZE]


[SIZE=2]



}




 



 

[/SIZE]

[/SIZE]
 

hot_blaster

Utente Attivo
117
7
Prima di tutto sistema la formattazione del codice, elimina tutti quegli spazi che rendono difficile leggere il programma.

Spero di aver letto bene, all'interno del while(1) (fondamentale per realizzare un server demone), dopo la chiusura delle connessione il server termina tramite return 0;
Se desideri "continuare con l'invio e la ricezione di altre stringhe", quella istruzione va tolta.

Non è necessario pulire i buffer per utilizzarli nuovamente, quando alla funzione recv() viene fornito un array di caratteri (con indicazione di lunghezza e offset), l'array viene automaticamente riempito dalla funzione, quindi cancellando il messaggio precedente. Non vorrei sbagliarmi, ma credo che la funzione recv() prende il puntatore passato (str1 nel tuo caso) e vi assegni un array correttamente istanziato e riempito del messaggio letto.

Spero di essermi chiarito, per favore formatta meglio il codice.
 

mark9

Nuovo Utente
45
3
In realtà il mio codice è ben formattato, è quando l'ho incollato qui che si sono creati quegli spazi e non me li ha fatti neanche cancellare. Devo chiedere qualcosa a riguardo in un'altra discussione con gli admin.
Pe quanto riguarda il mio scopo, tu dici di utilizzare in caso gli stessi buffer tanto vengono sovrascritti?
 

hot_blaster

Utente Attivo
117
7
Prima rispondo alla domanda due.
La send e la recv sono due primitive che dialogano direttamente con la driver (nel tuo caso tcp).
In particolare la send invia alla driver il messaggio passato come argomento, sarà la driver ad occuparsi della bufferizzazione del messaggio e relativo invio attraverso il canale tra cliente e servitore. Di conseguenza invocazioni multipli della send causano l'invio di messaggi che verranno accumulati nella driver del destinatario.
Parlando invece della recv, questa primitiva si occupa di leggere tutti i dati dal buffer della driver TCP, dati che vengono messi nel puntatore fornito. Quindi invocazioni multiple della recv provocano la lettura (e quindi cancellazione) dei dati dalla driver tcp. Sapendo questo invocazioni multiple di send e recv non causano problemi se gestite correttamente
 

hot_blaster

Utente Attivo
117
7
Nessun problema, appena torno a casa mi riguardo come funziona esattamente la recv riguardo il l'arrary passato come parametro
 

mark9

Nuovo Utente
45
3
Sto provando a modificare il pezzo di codice che rimuove le vocali, per fargli rimuovere le consonanti. Come potete vedere ho pensato di fare solo la modifica della condizione dell'if. il programma và però in crash...

for(i=0;i<=strlen(str);i++) {
if(str!='a'||str!='e'||str!='i'||str!='o'||str!='u' ||str!='A'||str!='E'||str!='I'||str!='O'||str!='U')
str=' ';
else
s[j++]=str;
}
s[j]='\0';

Sbaglio qualcosa relativa al codice o sbaglio completamente l'idea?
 

mark9

Nuovo Utente
45
3
Codice completo provato separatamente:

Codice:
#include<stdio.h>
#include<string.h>
int main(){
    char str[20],s[20];
    int i,j=0;
    printf("Enter any string->");
    scanf("%s",str);
    printf("The string is->%s",str);
   for(i=0;i<=strlen(str);i++) {
        if(str[i]!='a'||str[i]!='e'||str[i]!='i'||str[i]!='o'||str[i]!='u' ||str[i]!='A'||str[i]!='E'||str[i]!='I'||str[i]!='O'||str[i]!='U')
                str[i]=' ';
        else
                s[j++]=str[i];
    }
    s[j]='\0';
    printf("\nThe string without vowel is->%s\n",s);

    system("pause");

    return 0;
}


Il programma si blocca e smette di funzionare...Comunque 'i' è il contatore solo del ciclo, mentre la j è dove memorizzo la stringa modificata ;)
 

hot_blaster

Utente Attivo
117
7
In questo momento non ho modo di fare una ricerca approfondita, ma forse lo sai.
Nell'istruzione
s[j++]=str
Viene eseguita prima il comando di assegnamento o di incremento?
 

hot_blaster

Utente Attivo
117
7
Per quanto riguarda l'istruzione s[j++]=str, se il j++ viene eseguito prima serebbe un grosso problema, perché significherebbe che s[0] è sempre vuoto. Ma visto che il programma originale funziona, è probabile che prima venga eseguita l'assegnazione.

L'errore è un errore LOGICO, spero di spiegarmi bene.
Nel primo programma l'istruzione if verificava la presenza di una vocale, e la condizione OR è giusta. Cioè la condizione OR dice che se ALMENO una delle condizioni è verificata, l'intera condizione è vera, quindi se il carattere str è una vocale, si procede con le istruzioni nell'if.
Modificare tutte le uguaglianze con delle disuguaglianze (!=) mantenendo le condizioni di OR è sbagliato perché poniamo che s sia uguale a "e", la prima condizione dell'if (str!='a') è VERA, quindi non viene eseguito l'else, ma l'istruzione dentro l'if.
Lo stesso discorso è uguale per TUTTI i caratteri tranne "a", ma per str=='a' risulterà vera la seconda condizione!!!
Quindi qualunque stringa venga fornita, il risultato è una stringa Vuota.

Infine riguardando il programma, mi sfugge la necessità dell'istruzione
str=' ';
Qual è il suo scopo?
 

mark9

Nuovo Utente
45
3
L'idea originale era che se il carattere letto fosse stata un vocale, con str=' ' inserivo un carattere null al suo posto. Quindi poi ho pensato che, per eliminare invece le consonanti, potesse bastare verificare che ogni carattere fosse diverso da una vocale.
Ma forse la seconda idea è totalmente sbagliata..
 

hot_blaster

Utente Attivo
117
7
L'idea originale era che se il carattere letto fosse stata un vocale, con str=' ' inserivo un carattere null al suo posto.

Se il tuo obiettivo iniziale era ottenere una stringa senza le vocali, quindi utilizzando una stringa (s) inizialmente vuota e successivamente riempita, non capisco la necessità di modificare la stringa iniziale (str).

Quindi poi ho pensato che, per eliminare invece le consonanti, potesse bastare verificare che ogni carattere fosse diverso da una vocale. Ma forse la seconda idea è totalmente sbagliata..
È sbagliata perché anche una vocale è DIVERSA da ALMENO (OR) un'altra vocale.
Devi quindi verificare che sia diversa da TUTTE le vocali (AND, && se non sbaglio), e quindi invertire il contentuto dell'if con il contenuto dell'else e viceversa.
 

mark9

Nuovo Utente
45
3
Era proprio l'AND che dovevo usare, ci ho pensato poi ma non ero sicuro. Grazie mille.

Il punto dell'esercizio in questione dice:

"Il server riceve le tre stringhe, elimina le vocali da s1, le consonanti da s2, inverte s3 e concatena le stringhe
risultanti in una nuova stringa s1’+s2’+s3’;"

Per inviare queste 3 stringhe, posso usare dei send/recv consecutivi, o con un solo send/recv posso mandarle tutte e 3? Credo più la prima, ma voi più esperti mi saprete dire meglio ;)
 
Ultima modifica:

Ci sono discussioni simili a riguardo, dai un'occhiata!

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!

Discussioni Simili