This is a multi-part message in MIME format.
--------------55F9562B3659
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi,
I'm writing a small server program in C. The program listens on a
socket, accepts a connection from a client, and then forks and does
execlp() to start up a program.
What I want is for stdin/stdout from this program to go through the
socket to the client, so that everything the user types in the client
window is sent straight to the program's stdin. I tried using dup2(),
and it works fine for the output from the program (which shows up in the
client's window just as intended), but it doesn't seem to get any of the
input that I send from the client.
I looked through the network programming faq and found something about
the problem being related to the buffering of input/output (it said that
the buffers can only be explicitly flushed using write() or read() ) and
the solution was to use something called a pty, instead of a socket. If
this is the answer to my problem, could anybody please explain this
further, and give me some help on how to use it? And by the way, if this
is the reason my approach doesn't work, how come all the output comes
through fine?
The code for my server is attached...
Thank you!
Jenny Forss
--------------55F9562B3659
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="genserver.c"
/************************************************************************** /******************************** includes ****************************/ #include <stdio.h> /*** defines ***/ #define READ 0 #define GENESIS "/cit/genesis2/genesis" /**************************************/ /******************************************/ /* Create socket for incoming connections */ /* Fill in structure for socket */ /* Bind the socket to the address in the structure */ /*******************************************/ printf("Listening for connections from clients...\n"); /*******************************************************/ if (select(sockfd + 1, &readfds, NULL, NULL, NULL) < 1) { /* now accept the connection */ printf("Connection accepted.\n"); /* /* Redirect stdout+stdin to the socket */ /* case -1: /* Fail! */ } /* end of fork */
*
* Filename: genserver.c
* Project :
* Purpose :
*
***************************************************************************
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define WRITE 1
#define debug 1
#define GENESIS_FLAGS "Neurokit"
/* MAIN */
/**************************************/
void
main(int argc, char **argv)
{
int n, status;
int pid;
int sockfd, client_sockfd, res, len;
struct sockaddr_in serv_addr;
struct sockaddr client_addr;
fd_set readfds; /* for select */
/* SETTING UP... */
/******************************************/
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
perror("Couldn't create socket.");
exit(2);
}
serv_addr.sin_port = htons(9191);
res = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (res < 0) {
printf("Failed to bind socket to port 9191.\n");
exit(2);
}
/* START LISTENING FOR CONNECTIONS... */
/*******************************************/
res = listen(sockfd, 5);
if (res < 0) {
perror("Couldn't listen on port 9191.\n");
exit(1);
}
/* ETERNAL LOOP TO ACCEPT CONNECTIONS & */
/* SPAWN OFF GENESIS PROCESSES */
/*******************************************************/
while (1) {
/* prepare for select */
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
perror("no client connection");
exit(1);
}
len = sizeof(client_addr);
if ((client_sockfd = accept(sockfd, &client_addr, &len)) < 0) {
perror("Couldn't accept connection from client.");
exit(1);
}
* Fork off a Genesis process.
*/
switch( pid = fork() )
{
case 0 :
assert( dup2( client_sockfd, WRITE) == WRITE);
assert( dup2( client_sockfd, READ) == READ);
* Time to execute a Genesis process (flags defined at the top of
* this file...)
*/
execlp(GENESIS,
GENESIS,
GENESIS_FLAGS,
(char *)0);
fprintf(stderr," %s: Starting Genesis: cannot execlp!",argv[0]);
exit(1);
fprintf(stderr," %s: unable to fork Genesis process",argv[0]);
exit(1);
} /* end of main */
--------------55F9562B3659--