Stavo provando ad implementare una socket di un server che puņ accettare connessioni da un client, ho errori nell' esecuzione al momento del bind....sembrerebbe che voglia fare eseguire una operazione tipica delle socket a qualcosa che non č una socket...ecco il codice:
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
int main(int argc, char * arv[])
{
int servId, clientId, /*s,*/ 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("Error trying to bind");
exit(-2);
}
listen(servId, CONNECTIONS); //attesa di connessioni...
while((clientId=accept(clientId, (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);
}