Ho scritto il codice così:
codice:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h> // system defined identifiers.
#include <netinet/in.h> // internet address structure.
#include <sys/socket.h> // Need 4 socket(), bind(),
#include <netdb.h>
#define BUF_SIZE 1024 // buffer size in bytes
#define PORT_NUM 6110 // Port number for a Web server (TCP 5080)
#define TRUE 1
#define FALSE 0
#define COUNTDOWN 120
void clean_stdin(void)
{
int c;
do
{
c = getchar();
}
while (c != '\n' && c != EOF);
}
int readLine(int fd, char data[])
{
size_t len = 0;
while (len < BUF_SIZE)
{
char c;
int ret = read(fd, &c, 1);
if (ret < 0)
{
data[len] = 0;
return len; // EOF reached
}
if (c == '\n')
{
data[len] = 0;
return len; // EOF reached
}
data[len++] = c;
}
}
void* threadFunction(void* args)
{
time_t seconds = *(time_t*) args;
time_t endwait;
time_t start = time(NULL);
endwait = start + seconds;
printf("start time is : %s", ctime(&start));
while (start < endwait)
{
sleep(1); // sleep 1s.
start = time(NULL);
}
printf("end time is %s", ctime(&endwait));
exit(10);
}
int main(void)
{
int clientSocket;
struct sockaddr_in serverAddress;
char in_buf[BUF_SIZE], out_buf[BUF_SIZE];
struct hostent *h;
pthread_t thread;
time_t seconds = COUNTDOWN;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT_NUM);
h=gethostbyname("localhost");
if (h==0)
{
printf("Gethostbyname fallito\n");
exit(1);
}
bcopy(h->h_addr,&serverAddress.sin_addr,h->h_length);
clientSocket = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
if (clientSocket == -1)
{
printf("Could not create socket");
}
puts("Socket created");
if(connect(clientSocket , (struct sockaddr *)&serverAddress , sizeof(serverAddress)) < 0)
{
perror("connect failed. Error");
exit(1);
}
puts("Connected\n");
pthread_create(&thread, NULL, threadFunction, &seconds);
while(strcmp(in_buf, "OK") != 0)
{
sleep(1);
recv(clientSocket , in_buf, BUF_SIZE, 0);
}
puts(in_buf);
while(1)
{
printf("Inserisci il primo carattere: ");
scanf("%c", &out_buf[0]);
clean_stdin();
printf("Inserisci il secondo carattere: ");
scanf("%c", &out_buf[1]);
clean_stdin();
out_buf[2] = '\0';
if(send(clientSocket , out_buf , BUF_SIZE , 0) < 0)
{
puts("Send failed");
exit(3);
}
puts("Sent message: ");
puts(out_buf);
recv(clientSocket , in_buf , BUF_SIZE , 0);
while(strcmp(in_buf, "OC") != 0)
{
sleep(1);
recv(clientSocket , in_buf, BUF_SIZE, 0);
}
puts(out_buf);
recv(clientSocket , in_buf , BUF_SIZE , 0);
while(strlen(in_buf) != 1)
{
sleep(1);
recv(clientSocket , in_buf, BUF_SIZE, 0);
}
puts(out_buf);
printf("La distanza di Hamming tra i caratteri \"%c\" e \"%c\" è %c\n", out_buf[0], out_buf[1], in_buf[0]);
if(out_buf[0] == '0' && out_buf[1] == '0')
break;
else
{
if(recv(clientSocket , in_buf, BUF_SIZE , 0) <= 0 || strcmp(in_buf, "OK") != 0)
{
puts("recv failed");
exit(2);
}
}
}
close(clientSocket);
return TRUE;
}
e il codice del server concorrente utilizzando il thread
codice:
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
/* FOR BSD UNIX/LINUX ---------------------------------------------------- */
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sched.h>
#include <pthread.h>
#include <signal.h>
#include <semaphore.h>
#define BUF_SIZE 1024 // buffer size in bytes
#define PORT_NUM 6110 // Port number for a Web server (TCP 5080)
#define PEND_CONNECTIONS 100 // pending connections to hold
#define TRUE 1
#define FALSE 0
int readLine(int fd, char data[])
{
size_t len = 0;
while (len < BUF_SIZE)
{
char c;
int ret = read(fd, &c, 1);
if (ret < 0)
{
data[len] = 0;
return len; // EOF reached
}
if (c == '\n')
{
data[len] = 0;
return len; // EOF reached
}
data[len++] = c;
}
}
int hammingDistance(int x, int y)
{
int h = 0;
int t = x;
int s = y;
while (t > 0 || s > 0)
{
if ((t % 2) != (s % 2))
h++;
t /= 2;
s /= 2;
}
return h;
}
void* threadFunction(void* args)
{
unsigned int clientSocket; //copy socket
char in_buf[BUF_SIZE]; // Input buffer for GET resquest
char out_buf[BUF_SIZE]; // Output buffer for HTML response
int hamming;
clientSocket = *(unsigned int *)args; // copy the socket
strncpy(out_buf, "OK", BUF_SIZE);
send(clientSocket, out_buf, BUF_SIZE, 0);
puts(out_buf);
//send(clientSocket , out_buf , BUF_SIZE , 0);
while(1)
{
while(strlen(in_buf) != 2)
recv(clientSocket , in_buf , BUF_SIZE , 0);
strncpy(out_buf, "OC", BUF_SIZE);
send(clientSocket, out_buf , BUF_SIZE , 0);
puts(out_buf);
hamming = hammingDistance(in_buf[0] - '0', in_buf[1] - '0');
out_buf[0] = '0' + hamming;
out_buf[1] = '\0';
puts(out_buf);
send(clientSocket, out_buf , BUF_SIZE , 0);
puts(out_buf);
if(in_buf[0] == '0' && in_buf[1] == '0')
break;
else
{
strncpy(out_buf, "OK", BUF_SIZE);
send(clientSocket , out_buf , BUF_SIZE , 0);
puts(out_buf);
in_buf[1] = '\0';
}
}
close(clientSocket);
}
int main(void)
{
unsigned int serverSocket; // Server socket descriptor
struct sockaddr_in serverAddress; // Server Internet address
unsigned int clientSocket; // Client socket descriptor
struct sockaddr_in clientAddress; // Client Internet address
int clientAddressLength; // Internet address length
unsigned int ids; // holds thread args
pthread_t threads; // Thread ID (used by OS)
serverSocket = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP);
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT_NUM);
bind(serverSocket,(struct sockaddr *)&serverAddress , sizeof(struct sockaddr));
listen(serverSocket, PEND_CONNECTIONS);
while(TRUE)
{
printf("my server is ready ...\n");
/* wait for the next client to arrive -------------- */
clientAddressLength = sizeof(clientAddress);
clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddress, &clientAddressLength);
printf("a new client arrives ...\n");
if (clientSocket == FALSE)
{
printf("ERROR - Unable to create socket \n");
exit(FALSE);
}
else
{
ids = clientSocket;
pthread_create(&threads, NULL, threadFunction, &ids);
}
}
close (serverSocket);
return (TRUE);
}
Dov'è l'errore?