Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <pthread.h> //aggiungi -pthread al comando g++/gcc!
- #include <signal.h>
- using namespace std;
- struct client_info {
- char nick[20];
- int sockno;
- char ip[INET_ADDRSTRLEN];
- };
- int* refServerSocket;
- int clients[100];
- int n = 0;
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- void sendtoall(char *, int);
- void *recvmg(void *sock);
- void exitServer(int);
- int main(int argc,char *argv[])
- {
- struct sockaddr_in my_addr,their_addr;
- int my_sock, their_sock;
- socklen_t their_addr_size;
- int portno;
- pthread_t recvt;
- char msg[500];
- char buffer[50];
- char initmsg[] = "Mi serve il tuo nickname: ";
- int len;
- struct client_info cl;
- char myIP[INET_ADDRSTRLEN];
- char ip[INET_ADDRSTRLEN];
- cout<<"\nINSERISCI L'IP DI UNA TUA INTERFACCIA (ipv4)\n";
- cout<<"(puoi ricavarlo mediante il comando /sbin/ifconfig)\n-->";
- cin.getline(myIP, 16);
- cout<<"\nINSERISCI IL NUMERO DI PORTA (valore intero)\n";
- cout<<"(se non sei amministratore deve essere maggiore di 1023)\n-->";
- cin>>portno;
- my_sock = socket(AF_INET,SOCK_STREAM,0);
- memset(my_addr.sin_zero,'\0',sizeof(my_addr.sin_zero));
- my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(portno);
- my_addr.sin_addr.s_addr = inet_addr(myIP);
- their_addr_size = sizeof(their_addr);
- if(bind(my_sock,(struct sockaddr *)&my_addr,sizeof(my_addr)) != 0) {
- cerr<<"Binding fallita\n";
- exit(1);
- }
- if(listen(my_sock,5) != 0) {
- cerr<<"Listening fallita\n";
- exit(1);
- }
- refServerSocket = &my_sock;
- signal(SIGINT, exitServer);
- signal(SIGKILL, exitServer);
- signal(SIGTERM, exitServer);
- cout<<"\nIl server e' pronto!\n\nPuoi collegarti al server tramite telnet con il seguente comando: telnet "<<myIP<<" "<<portno<<"\n\nAttendo connessioni client...\n";
- while(1) {
- if((their_sock = accept(my_sock,(struct sockaddr *)&their_addr,&their_addr_size)) < 0) {
- cerr<<"Accept fallita\n";
- exit(1);
- }
- pthread_mutex_lock(&mutex);
- inet_ntop(AF_INET, (struct sockaddr *)&their_addr, ip, INET_ADDRSTRLEN);
- strcpy(buffer, initmsg);
- send(their_sock, buffer, strlen(buffer), 0);
- memset(buffer, 0, sizeof buffer);
- recv(their_sock, buffer, 50, 0);
- strcpy(cl.nick,buffer);
- memset(buffer, 0, sizeof buffer);
- cl.nick[strlen(cl.nick)-2] = '\0';
- cout<<cl.nick<<" con indirizzo di riferimento "<<ip<<" si e' connesso!\n";
- cl.sockno = their_sock;
- strcpy(cl.ip,ip);
- clients[n] = their_sock;
- n++;
- pthread_create(&recvt,NULL,recvmg,&cl);
- pthread_mutex_unlock(&mutex);
- }
- return 0;
- }
- void sendtoall(char *msg,int curr)
- {
- int i;
- pthread_mutex_lock(&mutex);
- for(i = 0; i < n; i++) {
- if(clients[i] != curr) {
- if(send(clients[i],msg,strlen(msg),0) < 0) {
- cerr<<"Fallito l'invio messaggio\n";
- continue;
- }
- }
- }
- pthread_mutex_unlock(&mutex);
- }
- void *recvmg(void *sock)
- {
- struct client_info cl = *((struct client_info *)sock);
- char msg[500];
- char tmp[520];
- int len, i, j;
- while((len = recv(cl.sockno,msg,500,0)) > 0) {
- strcpy(tmp,cl.nick);
- strcat(tmp,": ");
- msg[len] = '\0';
- strcat(tmp, msg);
- sendtoall(tmp,cl.sockno);
- memset(msg,'\0',sizeof(msg));
- memset(tmp,'\0',sizeof(tmp));
- }
- pthread_mutex_lock(&mutex);
- cout<<cl.nick<<" con indirizzo ip di riferimento "<<cl.ip<<" si e' disconnesso!\n";
- for(i = 0; i < n; i++) {
- if(clients[i] == cl.sockno) {
- j = i;
- while(j < n-1) {
- clients[j] = clients[j+1];
- j++;
- }
- }
- }
- n--;
- pthread_mutex_unlock(&mutex);
- }
- void exitServer(int segnale)
- {
- cerr<<"\nChat terminata (operazioni conclusive)\n";
- close(*refServerSocket);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement