Hey!

Writing a simple client/server in C and I'm currently stuck with getting the results after calling system("ls") so I can send it from server to client.

Since system() returns an int I tried popen() -->

char buffer[1000];
	FILE* myPipe = popen(buf, "r");
	if(myPipe==NULL){
		//errorstuff
	}
	while(fgets(buffer, 1000, myPipe)!=NULL){
		(void) printf("%s \n", buffer);
	}	
	pclose(myPipe);

But buffer only contains the last filename listed, not the rest of them. How do I get fgets to copy everything into the buffer?

As always; all help is appreciated! :o)

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;
		}
	}
}

Yes, it is a char*... I tried writing 1000 instead of sizeof(buf), but I got the same result... :o/

Thank you so much for taking a look! :o)

buffer=files[i-1]->d_name;
		rebruk = write(sock, buffer, sizeof(files));

I think this is also wrong. buffer is a pointer to d_name, but attempting to write sizeof(files) number of bytes. If files is an array of pointers to structures, then sizeof(files) will be 4 * number of pointers in that array. You probably mean this, unless files[i-1]->d_name is also a pointer.

buffer=files[i-1]->d_name;
rebruk = write(sock, buffer, sizeof(files[i-1]->d_name));

In that case why use buffer at all

rebruk = write(sock, files[i-1]->d_name, sizeof(files[i-1]->d_name));

If d_name is also a pointer, then you should probably replace the sizeof() with strlen(d_name)+1
In that case why use buffer at all

rebruk = write(sock, files[i-1]->d_name, strlen(files[i-1]->d_name)+1);

Now for the client side:
Since buf is a pointer, did you allocate memory for it, or what does it point to? please post code for this.

Aki, looked into this and I'm now using

rebruk = write(sock, files[i-1]->d_name, strlen(files[i-1]->d_name)+1);

Now for the client side:
Since buf is a pointer, did you allocate memory for it, or what does it point to? please post code for this.

char buf[1000]; --> That's all I have with buf, the rest is done by the les() method...

Should I allocate memory for it? if so, how and more importantly; Where would it be natural to do it? *clueless*

This article has been dead for over six months. Start a new discussion instead.