codice:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include "ricercauser.h"
#include <malloc.h>
#include "lista.h"
#include <pthread.h>
/* gcc -o main -pthread main.c */
int CreaSocketrecv(int Porta)
{
int sock,errore;
struct sockaddr_in temp;
//Creazione socket
sock=socket(AF_INET,SOCK_STREAM,0);
//Tipo di indirizzo
temp.sin_family=AF_INET;
temp.sin_addr.s_addr=INADDR_ANY;
temp.sin_port=htons(Porta);
//Il socket deve essere non bloccante
errore=fcntl(sock,F_SETFL,O_NONBLOCK);
//Bind del socket
errore=bind(sock,(struct sockaddr*) &temp,sizeof(temp));
//Per esempio, facciamo accettare fino a 7 richieste di servizio
//contemporanee (che finiranno nella coda delle connessioni).
errore=listen(sock,7);
return sock;
}
void ChiudiSocket(int sock)
{
close(sock);
return;
}
void* my_thread_listen(int DescrittoreSocketrecv)
{
char buffer[512];
int NuovoSocket;
int exitCond=0;
int Quanti;
int n=0;
struct timeval tv;
fd_set readfds;
FD_ZERO(&readfds);
printf("Server: Attendo connessioni...\n");
while (!exitCond)
{
FD_SET(DescrittoreSocketrecv,&readfds);
//Test sul socket: accept non blocca, ma il ciclo while continua
//l'esecuzione fino all'arrivo di una connessione.
n=select(99999,&readfds,NULL,NULL,NULL);
if(n>0)
{
if ((NuovoSocket=accept(DescrittoreSocketrecv,0,0))!=-1)
{
//Lettura dei dati dal socket (messaggio ricevuto)
if ((Quanti=read(NuovoSocket,buffer,sizeof(buffer)))<0)
{
printf("Impossibile leggere il messaggio.\n");
ChiudiSocket(NuovoSocket);
}
else
{
//Aggiusto la lunghezza...
buffer[Quanti]=0;
//Elaborazione dati ricevuti
if (strcmp(buffer,"exit")==0)
exitCond=1;
else printf("Client: %s \n",buffer);
}
//Chiusura del socket temporaneo
ChiudiSocket(NuovoSocket);
}
}
}
}
int CreaSocketsend(char* Destinazione, int Porta)
{
struct sockaddr_in temp;
struct hostent *h;
int sock;
int errore;
//Tipo di indirizzo
temp.sin_family=AF_INET;
temp.sin_port=htons(Porta);
h=gethostbyname(Destinazione);
if (h==0)
{
printf("Gethostbyname fallito\n");
exit(1);
}
bcopy(h->h_addr,&temp.sin_addr,h->h_length);
//Creazione socket.
sock=socket(AF_INET,SOCK_STREAM,0);
//Connessione del socket. Esaminare errore per compiere azioni
//opportune in caso di errore.
errore=connect(sock, (struct sockaddr*) &temp, sizeof(temp));
return sock;
}
void SpedisciMessaggio(int sock, char* Messaggio)
{
while(1){
//printf("Il tuo messaggio: %s\n",Messaggio);
//Si puo' notare il semplice utilizzo di write:
//write(socket, messaggio, lunghezza messaggio)
if (write(sock,Messaggio,strlen(Messaggio))<0)
{
printf("Impossibile mandare il messaggio.\n");
//ChiudiSocket(sock);
//exit(1);
}else{
printf("Messaggio spedito con successo.\n");
return;
}
}
}
int main()
{
//N.B. L'esempio non usa la funzione fork per far vedere l'utilizzo di
// socket non bloccanti
int DescrittoreSocketsend;
char* msg;
pthread_t thlisten;
int retcode;
int DescrittoreSocketrecv;
DescrittoreSocketrecv=CreaSocketrecv(1024);
retcode=pthread_create(&thlisten,NULL,my_thread_listen,DescrittoreSocketrecv);
while(1)
{
msg=(char*)calloc(256,sizeof(char));
printf("Inserisci Messagio da inviare\n");
fgets(msg,256,stdin);
DescrittoreSocketsend=CreaSocketsend("127.0.0.1",1025);
sleep(0.4);
SpedisciMessaggio(DescrittoreSocketsend,msg);
ChiudiSocket(DescrittoreSocketsend);
//mi metto in attesa dell'ascolto
}
//Chiusura del socket
ChiudiSocket(DescrittoreSocketrecv);
printf("Client: Terminato.\n");
return 0;
}