Thread: Socket handles

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Socket handles

    hi everybody,I am a beginner in C programming and i am currently learning sockets. I am now able to make my socket listening.My next challenge is to make a server that spawn a program like cmd or a command line program when a telnet client connect to it. I must admit that i need help for this one. I found that the easier way is to make a listening socket, handle the process then create the process. Iwrote a code but i can t make it work.Maybe someone can tell me what s wrong with my code or give me some advices.Here is my code (wich might be full or errors ):

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <winsock.h>
    
    int main() {
    
    
       WORD sockVersion;
       WSADATA wsaData;
    
       sockVersion = MAKEWORD(1, 1);
    
       WSAStartup(sockVersion, &wsaData);
    
       SOCKET mysocket;
       mysocket = socket( AF_INET,
                          SOCK_STREAM,
                          IPPROTO_TCP);
    
       SOCKADDR_IN serverInfo;
       serverInfo.sin_family = AF_INET;
       serverInfo.sin_addr.s_addr = INADDR_ANY;
       serverInfo.sin_port = htons(12235);
       
       bind(mysocket,(LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    
       listen(mysocket, 10);
    
       SOCKET client;
       client = accept(mysocket,
    			   NULL,
    			   NULL);
    
       HANDLE currentProcess; 
       currentProcess=GetCurrentProcess(); 
       HANDLE output; 
       DuplicateHandle(currentProcess, (HANDLE)client, currentProcess, 
       &output, 0, TRUE, DUPLICATE_SAME_ACCESS);
    
       HANDLE input; 
       DuplicateHandle(currentProcess, (HANDLE)client, currentProcess, 
       &input, 0, TRUE, DUPLICATE_SAME_ACCESS); 
    
       
    
       STARTUPINFO          si = { sizeof(si) };
       PROCESS_INFORMATION  pi;
       char                 szExe[] = "cmd.exe";
       si.dwFlags=STARTF_USESTDHANDLES; 
       si.hStdInput=input; 
       si.hStdOutput=output;
    
       if(CreateProcess(0, szExe, 0, 0, TRUE , CREATE_NEW_CONSOLE , 0, 0, &si, &pi))
       {
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
       }
        
      CloseHandle(input); 
      CloseHandle(output);
    
    
      closesocket(client);
      closesocket(mysocket); 
    
    WSACleanup();
    return 0;}
    Thank you for your help. I wish one day i will be able to help you

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    i can t make it work
    Could you be more specific? What errors are you getting? Where are you stuck? What's happening that shouldn't be, or vice-versa?

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    in fact, it compile without any problem, the server is listening on the specified port but when i connect with the windows telnet client i don t get the expected cmd.exe.
    I think the problem come from the process handle
    Last edited by nico; 04-04-2005 at 12:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. socket programming in linux
    By crazeinc in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 07:40 PM
  4. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM