Tony Hak
05-06-2010, 12:25
Buongiorno a tutti !
Ho il seguente problema: non riesco a far comunicare il server col client (il client dovrebbe visualizzare l'ora e la data inviati dal server). Il client è rappresentato da un altro pc collegato in Lan col server. Tutto avviene in ambiante UNIX. Il codice in questione del server è il seguente :
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <netinet/in.h>
#define SOCKET_NAME "/tmp/my_first_socket"
static int gestisci(int);
int main(int argc, char **argv)
{
int listen_sd, connect_sd; // Socket descriptor
struct sockaddr_un my_addr , client_addr;
socklen_t client_len;
my_addr.sun_family = PF_INET;
strcpy(my_addr.sun_path, SOCKET_NAME);
// Server section: create a local socket
if ( (listen_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
perror("socket"), exit(1);
// remove socket file if present
unlink(SOCKET_NAME);
// bind socket to pathname
//if ( ( bind ( listen_sd, ( struct sockaddr_in ( 3000, inet_aton ( "127.0.0.1" ) ) ) &my_addr, sizeof ( my_addr ) ) < 0 ) )
// perror("bind"), exit(1);
if ( bind(listen_sd, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0)
perror("bind"), exit(1);
// put socket in listen state
if ( listen(listen_sd, 1) < 0)
perror("listen"), exit(1);
while (1) {
client_len = sizeof(client_addr);
fprintf(stderr, " sizeof(client_addr)=%d \n", client_len);
if ( (connect_sd = accept(listen_sd, (struct sockaddr *) &client_addr, &client_len)) < 0)
perror("accept"), exit(1);
fprintf(stderr, " new connection \n");
fprintf(stderr, " client address: %100s\n ", client_addr.sun_path);
// handle the connection
gestisci(connect_sd);
close(connect_sd);
}
return 0;
}
int gestisci(int sd)
{
char buf[100];
int n;
time_t ora;
time(&ora);
printf(" Ora: %s\n", ctime_r(&ora, buf));
printf(" Ora: %d\n", strlen(buf));
write(sd, buf, 26);
return 0;
}
dove sbaglio :confused: ? grazie mille :)
Ho il seguente problema: non riesco a far comunicare il server col client (il client dovrebbe visualizzare l'ora e la data inviati dal server). Il client è rappresentato da un altro pc collegato in Lan col server. Tutto avviene in ambiante UNIX. Il codice in questione del server è il seguente :
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <netinet/in.h>
#define SOCKET_NAME "/tmp/my_first_socket"
static int gestisci(int);
int main(int argc, char **argv)
{
int listen_sd, connect_sd; // Socket descriptor
struct sockaddr_un my_addr , client_addr;
socklen_t client_len;
my_addr.sun_family = PF_INET;
strcpy(my_addr.sun_path, SOCKET_NAME);
// Server section: create a local socket
if ( (listen_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
perror("socket"), exit(1);
// remove socket file if present
unlink(SOCKET_NAME);
// bind socket to pathname
//if ( ( bind ( listen_sd, ( struct sockaddr_in ( 3000, inet_aton ( "127.0.0.1" ) ) ) &my_addr, sizeof ( my_addr ) ) < 0 ) )
// perror("bind"), exit(1);
if ( bind(listen_sd, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0)
perror("bind"), exit(1);
// put socket in listen state
if ( listen(listen_sd, 1) < 0)
perror("listen"), exit(1);
while (1) {
client_len = sizeof(client_addr);
fprintf(stderr, " sizeof(client_addr)=%d \n", client_len);
if ( (connect_sd = accept(listen_sd, (struct sockaddr *) &client_addr, &client_len)) < 0)
perror("accept"), exit(1);
fprintf(stderr, " new connection \n");
fprintf(stderr, " client address: %100s\n ", client_addr.sun_path);
// handle the connection
gestisci(connect_sd);
close(connect_sd);
}
return 0;
}
int gestisci(int sd)
{
char buf[100];
int n;
time_t ora;
time(&ora);
printf(" Ora: %s\n", ctime_r(&ora, buf));
printf(" Ora: %d\n", strlen(buf));
write(sd, buf, 26);
return 0;
}
dove sbaglio :confused: ? grazie mille :)