The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan UNIX for Dummies Questions & Answers 3 08-02-2008 05:21 PM
How to write to stdin of another program (program A -> [stdin]program B) vvaidyan High Level Programming 1 04-30-2008 01:44 PM
redirect STDIN prkfriryce Shell Programming and Scripting 4 01-04-2007 10:11 AM
stdin marquis High Level Programming 2 04-14-2005 01:49 PM
stdin not tty when try to pine or redirect Micz UNIX for Dummies Questions & Answers 1 05-20-2004 02:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-01-2008
gyula gyula is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 3
Redirect stdin and out to sockets

For windows was pretty simple to redirect the std in a and out of a
child process for "cmd.exe " command prompt terminal to a socket using connected pipes passed to a new process in the STARTUPINFO structure.

Code:
 BOOL b = ::CreatePipe((LPHANDLE)h_stdInRead,(LPHANDLE)hsdtInWriteTmp, &SecAttrib, 0);
    b &= ::CreatePipe((LPHANDLE)hsdtOutReadTmp, (LPHANDLE)h_sdtOutWrite, &SecAttrib, 0);
    b &= ::DuplicateHandle(hProcess, hsdtInWriteTmp, hProcess, h_this2Write, 0, 0, DUPLICATE_SAME_ACCESS);
    b &= ::DuplicateHandle(hProcess, hsdtOutReadTmp, hProcess, h_this2Read, 0, 0, DUPLICATE_SAME_ACCESS);

    hsdtInWriteTmp.Close();
    hsdtOutReadTmp.Close();

    si.dwFlags      = 0;
    si.cb           = sizeof(STARTUPINFO);
    si.dwFlags      = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.wShowWindow  = SW_HIDE;
    si.hStdInput    = h_stdInRead;
    si.hStdOutput   = h_sdtOutWrite;
    si.hStdError    = h_sdtOutWrite;
    si.wShowWindow    = SW_HIDE;
    si.lpDesktop    = 0;   
    si.lpTitle        = "blah" // Version();
   CreateProcess(NULL, "cmd.exe", lps, NULL, 1, CREATE_NEW_CONSOLE, NULL, NULL, psi, ppi) ;
/*
...
...
*/
//handles only 1 connection
 while(1)
{
  if(sock >0)
  {
  if(receive(socket, buffer))
    WriteFile(h_this2Write,..)
  if(ReadFile(h_this2Read))
     send(socket);
  }
  else
  {
     sock = wait_4_connection(); //accept and listen
  }
In above code the context (current directory) of the terminal cmd.exe is not lost between connections, and I can run internal shell commands as start, call, cd, and so on ....

For Linux I've tried to mimic the shell using popen(), and to write and read in the handler returned by popen(), but I cannot run /bash shell commands as cd, cp, mkdir and so on because they are internal bin/bash commands. I would like to have a shell up, and to write and read from it trough it's stdin stdout, available in my app (pipes of file handlers).
Something like:
Code:
//handles only 1 connection
   
  execl_and_get_sddin_and_out("bin/bash"); // keep it up
    ...   
  while(1)
  {
   if(sock >0)
  {
    if(receive(socket, buffer))
      fwrite(shell_stdin,..)
   if(fread(she__stdout))
      send(socket);
  }
  else
  {
     sock = wait_4_connection(); //accept and listen
  }
}

I tried these samples (but they don;t hold the shell up, and the command is sent to a shell, then the shell is closed):

Tying Standard Input and Output to a Socket Connection
http://src.gnu-darwin.org/DarwinSour...h/sshconnect.c

Does anyone has any idea how this can be accomplished.




Thank you.
Gyula.

Last edited by gyula; 09-01-2008 at 11:52 PM..
  #2 (permalink)  
Old 09-02-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Sorry, why are you creating a socket connection? Why don't you use pipe the output of the command back to the user?
Quote:
For Linux I've tried to mimic the shell using popen(), and to write and read in the handler returned by popen(), but I cannot run /bash shell commands as cd, cp, mkdir and so on because they are internal bin/bash commands
Hunh? If you use popen, the only limitation is that you cannot both read from and write into the file descriptor. Only if that limitation is a problem do you need sockets. But you say you can't run them because they are "internal commands". But there almost always external versions of these commands available. (For cd, however, it's useless -- you must do this within the context of your program, or prefix all subsequent commands with a "cd".) Regardless, all one needs to do in this case is run bash and provide it the command to parse.

Code:
fd=popen("/bin/bash -c \"cd /tmp; echo *\"","r");
Closed Thread

Bookmarks

Tags
linux commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0