#include<stdio.h> /* Standard I/O Library */
#include<stdlib.h> /* Standard Library */
#include<sys/types.h> /* Predefined Types */
#include<unistd.h> /* Unix Standard Library */
#include<arpa/inet.h> /* IP Address Conversion Utilities */
#include<sys/socket.h> /* Socket Library */
#include<time.h> /* Date e Time constants */
#include<string.h> /* C strings library */
#include<errno.h> /* error definitions and routines */
#include<syslog.h> /* syslog system functions */
#include<signal.h> /* signal constants, types and functions */
#include<sys/stat.h>
#include<fcntl.h> /* file control options */
#include<sys/wait.h> /* process termination constants and function */
#include<netdb.h>
#include<sys/time.h>
#include<pthread.h>
#include<netinet/in.h>
#include "User.h"
# define MAXLINE 256
ssize_t FullWrite(int fd, const void *buf, size_t count);
int main (int argc, char *argv[])
{
int sock,sock2;
struct sockaddr_in serv_addr;
int port;
char utenti[8000]="";
char *server;
struct hostent *hent;
int code;
/*
* Input Section : decodifica dei parametri passati nella chiamata
* Uso della funzione getopt
*/
// Assegnazione indirizzo
server = argv[1];
// Assegnazione della porta
port = atoi(argv[2]);
hent = gethostbyname(server);
//gethostname(msg.hostname, MAXHOSTNAME);
//msg.pid = getpid();
/* Create Socket */
if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) <0 ) {
perror("Socket creation error");
return 1;
}
//gethostbyname("127.0.0.1");
/* Initialize Address */
memset((void *) &serv_addr, 0, sizeof(serv_addr)); /* Clear Server Address */
serv_addr.sin_family = AF_INET; /* Address Types is INET */
serv_addr.sin_port = htons(port);
serv_addr.sin_addr = *((struct in_addr*) hent->h_addr);
/* Build Address Using inet_pton */
if ( (inet_pton(AF_INET, argv[optind], &serv_addr.sin_addr)) <= 0 ) {
perror("Address creation error");
return 1;
}
/* Extablish Connection */
if ( connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <0 ) {
perror("Connection error");
return 1;
}
char sendbuff[15],recvbuff[40];
int nread;
// Inserimento nickname
printf("\n\n Fornire un nickname : ");
scanf("%s", sendbuff);
FullWrite(sock, sendbuff, sizeof(sendbuff));
read(sock, recvbuff, sizeof(recvbuff));
if ( (strcmp(recvbuff, "Nome non corretto")) == 0 ) {
printf("\n\n %s ... Collegarsi nuovamente ed inserire un nickname valido\n\n", recvbuff);
close(sock);
} else {
char GARBAGE[MAXLINE];
int n_users;
int i;
USERS usr;
char offerta[MAXLINE];
printf("\n\n ");
printf("%s\n\n",recvbuff);
write(sock, GARBAGE, sizeof(GARBAGE));
read(sock, &n_users, sizeof(n_users));
printf("\n\n Numero utenti connessi : %d", n_users);
for (i=0; i<n_users; i++) {
read(sock, &usr, sizeof(usr));
printf("\n\n %s", usr.nickname);
}
write(sock, GARBAGE, sizeof(GARBAGE));
fflush(stdin);
printf("\n\n Postare una offerta di prodotto/servizio : ");
fgets(offerta, sizeof(offerta), stdin); // ***QUESTO NON ME LO FA INSERIRE!!!
// se faccio : fscanf("%s", offerta) ---> legge solo la prima stringa ..
printf("\n\n Postare la seguente offerta ? : \n\n :%s",offerta);
printf("\n\n");
}
close(sock);
/* Normal Exit */
return 0;
}
ssize_t FullWrite(int fd, const void *buf, size_t count)
{
size_t nleft;
ssize_t nwritten;
nleft = count;
while ( nleft > 0 ) { /* Repeat until no left */
if ( (nwritten = write(fd, buf, nleft)) <0 ) {
if ( errno == EINTR ) { /* If interrupted by system call */
continue; /* repeat loop */
} else {
return(nwritten); /* otherwise exit with error */
}
}
nleft -= nwritten; /* set left to write */
buf += nwritten; /* set pointer */
}
return (count);
}