Hi, this is my function:
void connection(int sock) // sock is a descriptor of socket
{
char buffer[MAX];
int n; // number of bytes read or write into a socket
int f;
f = fork();
if(write(sock,"HELLO\n", 5) < 0)
{
perror("Error: \n");
}
write(sock, "\n> ",3);
do {
memset(buffer,'0',MAX); //
n = read(sock,buffer,MAX -1 );
if (strncmp("get",buffer,3) == 0)
{
if(f == 0)
{
write(sock, "TOP:\n",4);
dup2(sock,1);
if(execl("/usr/bin/top","/usr/bin/top","-bn1",0) == -1)
write(sock, "ERROR",5);
}
else
{
waitpid(f, NULL, 0);
write(sock, "\n> ",3);
}
}
else if (strncmp("quit",buffer,4) == 0)
{
write(sock, "EXIT\n",4);
close(sock);
exit(0);
}
else
{
write(sock,"Wrong order\n", 12);
write(sock, "> ",2);
}
}
while(n);
}
This function is responsible for information exchange among client and server. The server should sends the results of "top" action.
This function is a part of TCP server program.
When client connect with server with telnet help, he see:
> Hello
> //(and here I can write "get", to see top results)
I' d like to see ">" again, after call "get", so I' ve put: else
{
waitpid(f, NULL, 0);
write(sock, "\n> ",3);
}
But I do not see my ">". I can see it, only when I write in telnet something else. What should I put in my child process in fork, to invoke my parent process?
Regards.