verbena1
April 29th, 2005, 09:09 PM
I didn't know who to ask since my instructor isn't much help so here goes.
I need some C/Unix networking help,
I'm working on a assignment in a unix environment that has two parts.
Part one implement a simple "shell" that parses a command and its arguments and then executes it, or if a pipe character is used ("|") parse the left side, parse the right side, and then manually pipe (using close() and dup()) and return this to a command line of my making (in my case my prompt is myShell> )
I have that done but now I need to implement it using networking. I have a client and a server and my setup is that the client connects to the server and the client prints out the prompt "myShell: " I can send a command and it works on the server end but I would like to know how to send the servers output of the command back to the client. I've looked around the internet and through my books (Richard Stevens Unix Networking book) and I can't find a way to do this unless I'm just missing something. I thought about maybe using dup() to make a copy of the input-send to the server-run my parse and execute functions-copy the output and send to to the client but I don't know how to do this. If I can send out this output is there a way to send my "myShell:" prompt directly from the server and save my client the trouble?
I'm not sure on the syntax though,
I know that in client its called sockfd, and in server newsockfd.
and this is where my server executes code after a connection:
for (;;) {
/* accept a connection */
if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1) {
perror("accept call failed");
continue;
}
/* spawn a child to deal with the connection */
if (fork() == 0) {
while (recv(newsockfd, buf, MAXLINE, 0) > 0) {
parse(buf, args1, args2);
execute(args1, args2);
send(newsockfd, buf, MAXLINE, 0);
}
/* when client is no longer sending information
the socket can be closed and the child process
terminated */
close(newsockfd);
exit (0);
}
/* parent doesn't need the newsockfd */
close (newsockfd) ;
}
Any help would be appreciated
I need some C/Unix networking help,
I'm working on a assignment in a unix environment that has two parts.
Part one implement a simple "shell" that parses a command and its arguments and then executes it, or if a pipe character is used ("|") parse the left side, parse the right side, and then manually pipe (using close() and dup()) and return this to a command line of my making (in my case my prompt is myShell> )
I have that done but now I need to implement it using networking. I have a client and a server and my setup is that the client connects to the server and the client prints out the prompt "myShell: " I can send a command and it works on the server end but I would like to know how to send the servers output of the command back to the client. I've looked around the internet and through my books (Richard Stevens Unix Networking book) and I can't find a way to do this unless I'm just missing something. I thought about maybe using dup() to make a copy of the input-send to the server-run my parse and execute functions-copy the output and send to to the client but I don't know how to do this. If I can send out this output is there a way to send my "myShell:" prompt directly from the server and save my client the trouble?
I'm not sure on the syntax though,
I know that in client its called sockfd, and in server newsockfd.
and this is where my server executes code after a connection:
for (;;) {
/* accept a connection */
if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1) {
perror("accept call failed");
continue;
}
/* spawn a child to deal with the connection */
if (fork() == 0) {
while (recv(newsockfd, buf, MAXLINE, 0) > 0) {
parse(buf, args1, args2);
execute(args1, args2);
send(newsockfd, buf, MAXLINE, 0);
}
/* when client is no longer sending information
the socket can be closed and the child process
terminated */
close(newsockfd);
exit (0);
}
/* parent doesn't need the newsockfd */
close (newsockfd) ;
}
Any help would be appreciated