Ho un problema nella comunicazione tra server e client mediante le socket, praticamente quando avvio il server resta in attesa di connessione e sembrerebbe che non ci siano problemi, infatti stampa l' indirizzo del client, perņ poi non c' č scambio di dati e tutto va in loop, non capisco cosa non vada, riporto i codici del server e del client:
Server:
codice:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#define LOCIP "127.0.0.1"
#define BUFFSIZE 1024
#define PORT 2000
#define CONNECTIONS 512
void main(int argc, char * arv[])
{
int servId, clientId, retcode; //ID Server, Client, contatore byte
struct sockaddr_in servip, clientip; //Strutture per indirizzo ip server e client
socklen_t servipL, clientipL; //lunghezza indirizzi
char *buffer[BUFFSIZE];
char * msg[BUFFSIZE];
if((servId=socket(PF_INET, SOCK_STREAM, 0))==-1){ //apertura socket server
perror("Error...creating socket...");
exit(-1);
}
servip.sin_family=AF_INET; //settaggio dominio e porta server
servip.sin_port=htons((unsigned)PORT);
inet_aton(LOCIP, &servip.sin_addr); //assegnazione indirizzo intero alla struttura mediante conversione
servipL=clientipL=sizeof(struct sockaddr_in); //memorizzazione lunghezza indirizzo
if ( bind(servId, (struct sockaddr *) &servip, servipL) == -1 ) //assegnazione indirizzo server per la rete
{perror("trying to bind"); exit(-2);}
listen(servId, CONNECTIONS); //attesa di connessioni...
printf("My address/port: %s", inet_ntoa(servip.sin_addr));
printf(":%d\n", ntohs(servip.sin_port));
while((clientId=accept(servId, (struct sockaddr *) &clientip, &clientipL)!=-1)){ //accetazione connesioni da parte di client
printf("Client from %s/%d connected\n",
inet_ntoa(clientip.sin_addr),
ntohs(clientip.sin_port)); //stampa indirizzo e porta del client
while((retcode=read(clientId, &buffer, BUFFSIZE))>0){ //ricevo byte e stampo
buffer[retcode-1]='\0';
printf("Client asks: %s", buffer);
buffer[retcode-1]='\n';
}
close(clientId); //chiudo connessione con il client
printf("%s", "Connection with client closed");
}
exit(0);
}
Client:
codice:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFFSIZE 1024
int main(int argc, char * argv[])
{
char buffer[BUFFSIZE]; //un solo buffer per il client
struct sockaddr_in serverIp;
int clientId, retcode;
u_int16_t fport;
printf("prova");
if((clientId=socket(AF_INET, SOCK_STREAM, 0))==-1){
perror("Error creating socket");
exit(-1);
}
if(argc<2 || argc<3){
perror("Insert address or port number"); //il secondo parametro indicherą l' indirizzo socket del client
exit(-2);
}
fport=htons((u_int16_t)atoi(argv[2]));
inet_aton(argv[1], &serverIp.sin_addr);
serverIp.sin_family=AF_INET;
serverIp.sin_port=fport;
//mkaddr(&serverIp, argv[1], fport); //inseirmento indirizzo del server a cui connettersi
// if (connect(clientId, (struct sockaddr *) &serverIp, sizeof(serverIp))
// == -1 )
if(connect(clientId, (struct sockaddr *) &serverIp, sizeof(serverIp))==-1){
perror("Error trying to connect");
exit(-3);
}
if(argc<4){
perror("You have to insert a message!!!");
exit(-4);
}
sprintf(buffer, "%s\n", argv[3]); //il buffer conterrą il messaggio
if (write(clientId, buffer, strlen(buffer))==-1) {
perror("Error sending message");
exit(-5);
}
while((retcode=read(clientId, buffer, BUFFSIZE-1))>0){
printf("\n Read byte\n %d", retcode);
buffer[retcode]='\0';
printf("Server send message \n %s", buffer);
}
printf("Client exiting....server closed the connection");
close(clientId);
exit(0);
}
Ecco rispettivamente cosa succede nei terminali dopo avere eseguito: Rispettivamente il server e il client:
codice:
dario@ubuntu:~$ cd Scrivania/Esercizi\ socket/
dario@ubuntu:~/Scrivania/Esercizi socket$ ./Server
My address/port: 127.0.0.1:2000
Client from 127.0.0.1/47440 connected
codice:
dario@ubuntu:~/Scrivania/Esercizi socket$ ./Client 127.0.0.1 2000 CIAO