Il programma dovrebbe mandare un messaggio da client al server(multi thread per gestire più clienti) che a sua volta dovrebbe stamparlo.Il problema è che una volta inviato il messaggio la read del server entra nello stato bloccante non arrivando mai al printf("Sono fuori").
La situazione cambia quando avvio un nuovo client mantenendo aperti il server e il vecchio client perchè una volta che si connette la read si "sblocca".


Codice Client
codice:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define N 256
int main(int argc, char *argv[]) {
int c_fd,portno,lung,abba;
struct sockaddr_in sin;
struct hostent *hp;
char buffer[N];
	if (argc != 3) {
		printf("Illegal number of arguments");
		exit(1);
	}
	if ((hp = gethostbyname(argv[1])) == 0) {
		perror("tom: gethostbyname");
		exit(1);
	}
	c_fd = socket(AF_INET, SOCK_STREAM, 0);
	portno = atoi(argv[2]);
	sin.sin_family = AF_INET;
	memcpy((char *)&sin.sin_addr,(char *)hp->h_addr,hp->h_length);
	sin.sin_port = htons(portno);
	if(connect(c_fd, (struct sockaddr *) &sin, sizeof(sin)) < 0){
		printf("connect() failed\n");
		return 1;
	}
	printf("(CLIENT) Scrivere un messaggio: "); 
fgets(buffer,N,stdin);

	write(c_fd, buffer,strlen(buffer));
	printf("Client %d ha inviato il messaggio\n", getpid());
	close(c_fd);
	return 0;
}//end client


codice Server(per compilarlo con il terminale unix per via dei thread)
gcc nomefile.c -o nomeeseguibile -lpthread

codice:
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<pthread.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 256
void *gestisci(void *arg) {
        int a;
	printf("gestisci(): creato il thread\n");
	char buf[N];
	while(a=read((int *) arg, buf, N)!=0)
       {

         if(a==-1)
          perror("\nerrore\n");
 	else
	printf("\nServer %s   finito\n\n", buf);
	}
printf("sono fuori");
	close((int *) arg);
	pthread_exit (0);
}


int main(int argc, char *argv[]){
int s_fd, c_fd, portno, len;
pthread_t tid;
char buffer[N];
struct sockaddr_in sin, client;
	s_fd=socket(AF_INET,SOCK_STREAM,0);
	portno = atoi(argv[1]);
	sin.sin_family = AF_INET;
	sin.sin_port = htons(portno);
	sin.sin_addr.s_addr = htonl(INADDR_ANY);
	len = sizeof(client);
	bind(s_fd, (struct sockaddr *) &sin, sizeof(sin));
	listen(s_fd,5);
	while (1){
		c_fd = accept(s_fd,(struct sockaddr *)&client, &len);
		inet_ntop(AF_INET,&client.sin_addr,buffer,sizeof(buffer));
		printf("Request from client %s\n",buffer);
		pthread_create(&tid,NULL,gestisci,(void *)c_fd);
		pthread_detach(tid);//serve per liberare risorse
	}
	close(s_fd);
	return 0;
}//end server