Aki, now I'm one step further, using scandir + I've written in the code for sending one filename at the time to the client, who reads in a loop.
The result from this is the server reading the directory and printing it perfectly, while the client writes the name of the first file in the direcory minus the file type. One the new line all the other filenames are put together, but not with complete names, just some letters...! Example -->
! fil (should be fil.txt)
! klieklieold" (this is the files klient, klient.c + directory old)
The server clearly sends a termination to the client as I've tester just printing to the screen when it enters that method... So I know the client should be able to exit the while-loop...
What's causing this? I'm adding my methods for reading a directory, and hoping someone can find an error there, but I'm starting to worry it might be outside these functions...
/*
** SERVER **
*******************/
void skrivLs(int sock){
char *buffer;
int teller,i;
struct direct **files;
int file_select();
teller = scandir(".", &files, file_select, alphasort);
// Dersom ingen filer blir funnet */
if(teller < 0){
perror("Socket");
strcpy(buffer, "No such file or directory\n");
printf("BUF; %s\n", buffer);
rebruk = write(sock, buffer, sizeof(files));
if(rebruk < 0){
perror("write() failed");
close(lytte_sock);
close(sock);
exit(1);
}
skrivSlutt();
exit(0);
}
i=0;
while(i<teller){
i++;
//Test to see this actually works...
//printf("%s ",files[i-1]->d_name);
//printf("\n");
buffer=files[i-1]->d_name;
rebruk = write(sock, buffer, sizeof(files));
if(rebruk < 0){
perror("write() failed");
close(lytte_sock);
close(sock);
exit(1);
}
}
skrivSlutt(); //method that sends "-1" to the client
nullBuf(buffer);
}
/*
** CLIENT **
*******************/
void les(){
nullBuf(buf); //writing a bunch of nulls to buf if it isn't empty
while(buf){
rebruk = read(sock, buf, sizeof(buf));
if(rebruk<0){
perror("Read");
close(sock);
exit(1);
}
if(strcmp(buf, slutt)!=0){ //if server sends "-1"
printf("! %s\n",buf);
}else{
break;
}
}
}