Ciao ragazzi
ho implementato per motivi puramente educativi ho implementato questi due programmi client server!Per comunicare utilizzo i socket, per funzionano pero' non capisco perche' alcune istruzioni le salta!
questo e' il codice del server le righe con # sembrerebbe che non le esegui!
codice:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#define Max 8000
int main ()
{
char buff[Max];
struct sockaddr_in server;
struct sockaddr_in client;
int sd, temp_sd;
int address_size;
unsigned short port = 9000;
if((sd= socket (AF_INET ,SOCK_STREAM,0 ))<0){
printf("Errore creazione del server!\n");
return -1;
}else{
#printf("Prima della struttura");
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr= htons(INADDR_ANY);
if (bind(sd, (struct sockaddr *)&server , sizeof(server))<0){
printf("Errore nella chiamata di bind");
}
printf("sono prima del listen");
listen(sd, 20);
while(1){
if((temp_sd= accept(sd, (struct sockaddr *)&client, &address_size))<0)
printf("Errore nel accept");
#printf("Sono prima del recv");
recv(temp_sd , buff , sizeof(buff),0);
#printf("Dati ricevuti:%s" , buff );
strcpy(buff , "Dati dal server!");
send(temp_sd, buff, strlen(buff), 0);
close(temp_sd);
}
}
return 0;
}
Invece il client riceve i dati e gli stampa a video! comunque questo e' il codice
codice:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#define Max 8000
int main ()
{
char buff[Max];
struct sockaddr_in client;
int sd;
struct hostent *hp;
unsigned short port = 9000;
hp = gethostbyname("127.0.0.1");
//hp = gethostbyname("192.168.2.3");
bzero(&client , sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(port);
client.sin_addr.s_addr= htons(INADDR_ANY);
if((sd= socket (AF_INET ,SOCK_STREAM,0 ))<0){
printf("Errore creazione del server!\n");
return -1;
}else
printf("Socket creato\n");
printf("Sono prima di connect");
if (connect(sd, (struct sockaddr *)&client , sizeof(client))<0)
printf("Errore nella chiamata di connessione");
send(sd, "Dati inviati dal client", strlen("Dati inviati dal client")+1, 0);
recv(sd , buff , sizeof(buff), 0);
printf("Risposta del server:%s\n" , buff);
close(sd);
return 0;
}
Non capisco il perche' non esegue i printf nel server!!
grazie mille per l'aiuto