Quindi cosģ? Il problema permane....
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, 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);
}
memset(&servip, 0, sizeof(servip));
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...
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);
}
Comunque provando a compilare a mano da terminale mi spunta un messaggio che lascia intendere che la memset č automatica...
codice:
Server.c: In function main:
Server.c:28:2: warning: incompatible implicit declaration of built-in function memset [enabled by default]
dario@ubuntu:~/Scrivania$ ./Server
trying to bind: Socket operation on non-socket
dario@ubuntu:~/Scrivania$ ^C
dario@ubuntu:~/Scrivania$