-
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 :p ):
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 :D
-
Quote:
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?
-
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